Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use file scoped namespaces #377

Merged
merged 1 commit into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ indent_size = 4

[*.cs]
indent_size = 4
csharp_style_namespace_declarations = file_scoped:warning

[*.json]
indent_size = 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace ConsumeAndMultiOutputPublisherWithRabbitMQ.Model
namespace ConsumeAndMultiOutputPublisherWithRabbitMQ.Model;

public record InputMessage
{
public record InputMessage
{
public string FancyText { get; set; } = "FooBar";
public int FancyNumber { get; set; } = 42;
}
public string FancyText { get; set; } = "FooBar";
public int FancyNumber { get; set; } = 42;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace ConsumeAndMultiOutputPublisherWithRabbitMQ.Model
namespace ConsumeAndMultiOutputPublisherWithRabbitMQ.Model;

public record OutputMessage
{
public record OutputMessage
{
public string NotSoFancyText { get; set; }
public int NotSoFancyNumber { get; set; }
}
public string NotSoFancyText { get; set; }
public int NotSoFancyNumber { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,47 @@
using Motor.Extensions.Hosting.Abstractions;
using Motor.Extensions.Hosting.CloudEvents;

namespace ConsumeAndMultiOutputPublisherWithRabbitMQ
namespace ConsumeAndMultiOutputPublisherWithRabbitMQ;

public class MultiOutputService : IMultiOutputService<InputMessage, OutputMessage>
{
public class MultiOutputService : IMultiOutputService<InputMessage, OutputMessage>
// Handle incoming messages
public async IAsyncEnumerable<MotorCloudEvent<OutputMessage>> ConvertMessageAsync(
MotorCloudEvent<InputMessage> inputEvent, [EnumeratorCancellation] CancellationToken token = default)
{
// Handle incoming messages
public async IAsyncEnumerable<MotorCloudEvent<OutputMessage>> ConvertMessageAsync(
MotorCloudEvent<InputMessage> inputEvent, [EnumeratorCancellation] CancellationToken token = default)
{
// Get the input message from the cloud event
var input = inputEvent.TypedData;
// Get the input message from the cloud event
var input = inputEvent.TypedData;

// Do your magic here .....
var output = MagicFuncAsync(input);
// Do your magic here .....
var output = MagicFuncAsync(input);

// Create a new cloud event from your output message which is automatically published and return a new task.
await foreach (var outputMessage in output.WithCancellation(token))
{
yield return inputEvent.CreateNew(outputMessage);
}
// Create a new cloud event from your output message which is automatically published and return a new task.
await foreach (var outputMessage in output.WithCancellation(token))
{
yield return inputEvent.CreateNew(outputMessage);
}
}

private static async IAsyncEnumerable<OutputMessage> MagicFuncAsync(InputMessage input)
private static async IAsyncEnumerable<OutputMessage> MagicFuncAsync(InputMessage input)
{
if (string.IsNullOrEmpty(input.FancyText))
{
if (string.IsNullOrEmpty(input.FancyText))
{
// Reject message in RabbitMQ queue (Any ArgumentException can be used to reject to messages.).
throw new ArgumentNullException("FancyText is empty");
}
// Reject message in RabbitMQ queue (Any ArgumentException can be used to reject to messages.).
throw new ArgumentNullException("FancyText is empty");
}

// Magic async function.
await Task.Delay(10).ConfigureAwait(false);
// Magic async function.
await Task.Delay(10).ConfigureAwait(false);

yield return new()
{
NotSoFancyText = input.FancyText.Reverse().ToString(),
NotSoFancyNumber = input.FancyNumber * -1,
};
yield return new()
{
NotSoFancyText = input.FancyText,
NotSoFancyNumber = input.FancyNumber * -2,
};
}
yield return new()
{
NotSoFancyText = input.FancyText.Reverse().ToString(),
NotSoFancyNumber = input.FancyNumber * -1,
};
yield return new()
{
NotSoFancyText = input.FancyText,
NotSoFancyNumber = input.FancyNumber * -2,
};
}
}
11 changes: 5 additions & 6 deletions examples/ConsumeAndPublishWithKafka/Model/InputMessage.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace ConsumeAndPublishWithKafka.Model
namespace ConsumeAndPublishWithKafka.Model;

public record InputMessage
{
public record InputMessage
{
public string FancyText { get; set; } = "FooBar";
public int FancyNumber { get; set; } = 42;
}
public string FancyText { get; set; } = "FooBar";
public int FancyNumber { get; set; } = 42;
}
11 changes: 5 additions & 6 deletions examples/ConsumeAndPublishWithKafka/Model/OutputMessage.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace ConsumeAndPublishWithKafka.Model
namespace ConsumeAndPublishWithKafka.Model;

public record OutputMessage
{
public record OutputMessage
{
public string NotSoFancyText { get; set; }
public int NotSoFancyNumber { get; set; }
}
public string NotSoFancyText { get; set; }
public int NotSoFancyNumber { get; set; }
}
55 changes: 27 additions & 28 deletions examples/ConsumeAndPublishWithKafka/SingleOutputService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,39 @@
using Motor.Extensions.Hosting.Abstractions;
using Motor.Extensions.Hosting.CloudEvents;

namespace ConsumeAndPublishWithKafka
namespace ConsumeAndPublishWithKafka;

public class SingleOutputService : ISingleOutputService<InputMessage, OutputMessage>
{
public class SingleOutputService : ISingleOutputService<InputMessage, OutputMessage>
// Handle incoming messages
public Task<MotorCloudEvent<OutputMessage>> ConvertMessageAsync(
MotorCloudEvent<InputMessage> inputEvent,
CancellationToken token = default)
{
// Handle incoming messages
public Task<MotorCloudEvent<OutputMessage>> ConvertMessageAsync(
MotorCloudEvent<InputMessage> inputEvent,
CancellationToken token = default)
{
// Get the input message from the cloud event
var input = inputEvent.TypedData;
// Get the input message from the cloud event
var input = inputEvent.TypedData;

// Do your magic here .....
var output = MagicFunc(input);
// Do your magic here .....
var output = MagicFunc(input);

// Create a new cloud event from your output message which is automatically published and return a new task.
var outputEvent = inputEvent.CreateNew(output);
return Task.FromResult(outputEvent);
}
// Create a new cloud event from your output message which is automatically published and return a new task.
var outputEvent = inputEvent.CreateNew(output);
return Task.FromResult(outputEvent);
}

private static OutputMessage MagicFunc(InputMessage input)
private static OutputMessage MagicFunc(InputMessage input)
{
if (string.IsNullOrEmpty(input.FancyText))
{
if (string.IsNullOrEmpty(input.FancyText))
{
// Reject message in RabbitMQ queue (Any ArgumentException can be used to reject to messages.).
throw new ArgumentNullException("FancyText is empty");
}

var output = new OutputMessage
{
NotSoFancyText = input.FancyText.Reverse().ToString(),
NotSoFancyNumber = input.FancyNumber * -1,
};
return output;
// Reject message in RabbitMQ queue (Any ArgumentException can be used to reject to messages.).
throw new ArgumentNullException("FancyText is empty");
}

var output = new OutputMessage
{
NotSoFancyText = input.FancyText.Reverse().ToString(),
NotSoFancyNumber = input.FancyNumber * -1,
};
return output;
}
}
11 changes: 5 additions & 6 deletions examples/ConsumeAndPublishWithRabbitMQ/Model/InputMessage.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace ConsumeAndPublishWithRabbitMQ.Model
namespace ConsumeAndPublishWithRabbitMQ.Model;

public record InputMessage
{
public record InputMessage
{
public string FancyText { get; set; } = "FooBar";
public int FancyNumber { get; set; } = 42;
}
public string FancyText { get; set; } = "FooBar";
public int FancyNumber { get; set; } = 42;
}
11 changes: 5 additions & 6 deletions examples/ConsumeAndPublishWithRabbitMQ/Model/OutputMessage.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace ConsumeAndPublishWithRabbitMQ.Model
namespace ConsumeAndPublishWithRabbitMQ.Model;

public record OutputMessage
{
public record OutputMessage
{
public string NotSoFancyText { get; set; }
public int NotSoFancyNumber { get; set; }
}
public string NotSoFancyText { get; set; }
public int NotSoFancyNumber { get; set; }
}
55 changes: 27 additions & 28 deletions examples/ConsumeAndPublishWithRabbitMQ/SingleOutputService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,39 @@
using Motor.Extensions.Hosting.Abstractions;
using Motor.Extensions.Hosting.CloudEvents;

namespace ConsumeAndPublishWithRabbitMQ
namespace ConsumeAndPublishWithRabbitMQ;

public class SingleOutputService : ISingleOutputService<InputMessage, OutputMessage>
{
public class SingleOutputService : ISingleOutputService<InputMessage, OutputMessage>
// Handle incoming messages
public Task<MotorCloudEvent<OutputMessage>> ConvertMessageAsync(
MotorCloudEvent<InputMessage> inputEvent,
CancellationToken token = default)
{
// Handle incoming messages
public Task<MotorCloudEvent<OutputMessage>> ConvertMessageAsync(
MotorCloudEvent<InputMessage> inputEvent,
CancellationToken token = default)
{
// Get the input message from the cloud event
var input = inputEvent.TypedData;
// Get the input message from the cloud event
var input = inputEvent.TypedData;

// Do your magic here .....
var output = MagicFunc(input);
// Do your magic here .....
var output = MagicFunc(input);

// Create a new cloud event from your output message which is automatically published and return a new task.
var outputEvent = inputEvent.CreateNew(output);
return Task.FromResult(outputEvent);
}
// Create a new cloud event from your output message which is automatically published and return a new task.
var outputEvent = inputEvent.CreateNew(output);
return Task.FromResult(outputEvent);
}

private static OutputMessage MagicFunc(InputMessage input)
private static OutputMessage MagicFunc(InputMessage input)
{
if (string.IsNullOrEmpty(input.FancyText))
{
if (string.IsNullOrEmpty(input.FancyText))
{
// Reject message in RabbitMQ queue (Any ArgumentException can be used to reject to messages.).
throw new ArgumentNullException("FancyText is empty");
}

var output = new OutputMessage
{
NotSoFancyText = input.FancyText.Reverse().ToString(),
NotSoFancyNumber = input.FancyNumber * -1,
};
return output;
// Reject message in RabbitMQ queue (Any ArgumentException can be used to reject to messages.).
throw new ArgumentNullException("FancyText is empty");
}

var output = new OutputMessage
{
NotSoFancyText = input.FancyText.Reverse().ToString(),
NotSoFancyNumber = input.FancyNumber * -1,
};
return output;
}
}
11 changes: 5 additions & 6 deletions examples/ConsumeNATS/Model/NatsMessage.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Text.Json.Serialization;

namespace ConsumeNATS.Model
namespace ConsumeNATS.Model;

public class NatsMessage
{
public class NatsMessage
{
[JsonPropertyName("SomeProperty")] public string SomeProperty { get; set; }
[JsonPropertyName("Time")] public DateTime Time { get; set; }
}
[JsonPropertyName("SomeProperty")] public string SomeProperty { get; set; }
[JsonPropertyName("Time")] public DateTime Time { get; set; }
}
13 changes: 6 additions & 7 deletions examples/ConsumeNATS/NoOutputService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
using Motor.Extensions.Hosting.Abstractions;
using Motor.Extensions.Hosting.CloudEvents;

namespace ConsumeNATS
namespace ConsumeNATS;

public class NoOutputService : INoOutputService<NatsMessage>
{
public class NoOutputService : INoOutputService<NatsMessage>
public Task<ProcessedMessageStatus> HandleMessageAsync(MotorCloudEvent<NatsMessage> dataCloudEvent, CancellationToken token = new())
{
public Task<ProcessedMessageStatus> HandleMessageAsync(MotorCloudEvent<NatsMessage> dataCloudEvent, CancellationToken token = new())
{
System.Console.WriteLine(dataCloudEvent.TypedData.SomeProperty);
return Task.FromResult(ProcessedMessageStatus.Success);
}
System.Console.WriteLine(dataCloudEvent.TypedData.SomeProperty);
return Task.FromResult(ProcessedMessageStatus.Success);
}
}
11 changes: 5 additions & 6 deletions examples/ConsumeSQS/Model/InputMessage.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace ConsumeSQS.Model
namespace ConsumeSQS.Model;

public record InputMessage
{
public record InputMessage
{
public string FancyText { get; set; } = "FooBar";
public int FancyNumber { get; set; } = 42;
}
public string FancyText { get; set; } = "FooBar";
public int FancyNumber { get; set; } = 42;
}
17 changes: 8 additions & 9 deletions examples/ConsumeSQS/NoOutputService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
using Motor.Extensions.Hosting.Abstractions;
using Motor.Extensions.Hosting.CloudEvents;

namespace ConsumeSQS
namespace ConsumeSQS;

public class NoOutputService : INoOutputService<InputMessage>
{
public class NoOutputService : INoOutputService<InputMessage>
{

public Task<ProcessedMessageStatus> HandleMessageAsync(MotorCloudEvent<InputMessage> dataCloudEvent, CancellationToken token = default)
{
var msg = dataCloudEvent?.TypedData;
Console.WriteLine(msg);
return Task.FromResult(ProcessedMessageStatus.Success);
}
public Task<ProcessedMessageStatus> HandleMessageAsync(MotorCloudEvent<InputMessage> dataCloudEvent, CancellationToken token = default)
{
var msg = dataCloudEvent?.TypedData;
Console.WriteLine(msg);
return Task.FromResult(ProcessedMessageStatus.Success);
}
}
Loading