From 00e3020eb8e6cd7c0d0923783c9e8a4e35c55030 Mon Sep 17 00:00:00 2001 From: Maxwell Weru Date: Tue, 12 Dec 2023 10:28:28 +0300 Subject: [PATCH] Cosmetic code changes (#576) --- samples/AmazonSqsAndSns/DoorOpenedConsumer.cs | 9 +---- samples/AmazonSqsAndSns/PublisherService.cs | 9 +---- samples/AzureIotHub/AzureIotEventsConsumer.cs | 9 +---- .../VehicleTelemetryEventsConsumer.cs | 9 +---- .../VehicleTelemetryEventsConsumer.cs | 9 +---- .../ConfigSample/VisualsProducerService.cs | 11 +---- .../ConfigSample/VisualsUploadedConsumer.cs | 9 +---- .../SampleConsumer1.cs | 9 +---- .../SampleConsumer2.cs | 9 +---- .../AzureDevOpsEventSerializer.cs | 6 +-- .../AzureDevOpsEventsConsumer.cs | 11 +---- .../InMemoryBackgroundProcessing/Program.cs | 18 +-------- .../DummyProducerService.cs | 16 +++----- .../MultiEventsConsumer.cs | 11 +---- .../MultipleConsumers/FirstEventConsumer.cs | 9 +---- samples/MultipleConsumers/PublisherService.cs | 9 +---- .../MultipleConsumers/SecondEventConsumer.cs | 9 +---- .../VehicleTelemetryEventsConsumer.cs | 9 +---- samples/MultipleSimilarTransports/Program.cs | 20 +--------- samples/SimpleConsumer/SampleEventConsumer.cs | 11 +---- samples/SimplePublisher/PublisherService.cs | 9 +---- .../AmazonTransportConfigureOptions.cs | 18 ++++----- .../AmazonKinesisConfigureOptions.cs | 18 ++++----- .../BinaryDataExtensions.cs | 2 +- .../AmazonSqsConfigureOptions.cs | 18 ++++----- .../AmazonSqsTransport.cs | 2 +- .../AzureTransportConfigureOptions.cs | 18 ++++----- .../AzureEventHubsConfigureOptions.cs | 20 ++++------ .../AzureEventHubsTransport.cs | 23 +++++------ .../IotHub/IotHubEventSerializer.cs | 8 ++-- .../AzureQueueStorageConfigureOptions.cs | 20 ++++------ .../AzureQueueStorageTransport.cs | 2 +- .../AzureServiceBusConfigureOptions.cs | 20 ++++------ .../Client/BroadcastChannelWriter.cs | 9 +---- .../Client/InMemoryClient.cs | 9 +---- .../Client/InMemoryMessage.cs | 14 ++----- .../Client/InMemoryProcessor.cs | 8 +++- .../Client/InMemorySender.cs | 23 +++++------ .../Client/ProcessErrorEventArgs.cs | 35 ++++++---------- .../Client/ProcessMessageEventArgs.cs | 26 ++++-------- .../InMemoryTransport.cs | 40 ++++++++----------- .../InMemoryTransportConfigureOptions.cs | 18 ++++----- .../KafkaConfigureOptions.cs | 18 ++++----- .../KafkaTransport.cs | 2 +- .../RabbitMqConfigureOptions.cs | 18 ++++----- .../RabbitMqTransport.cs | 23 +++++------ .../EventConsumerRegistration.cs | 19 +++------ .../Configuration/EventRegistration.cs | 18 +++------ .../Transports/EventBusTransport.cs | 9 +---- .../MandatoryEventBusConfiguratorTests.cs | 2 +- .../IotHubEventSerializerTests.cs | 9 +---- .../SampleEventConsumerTests.cs | 9 +---- .../SimplePublisherTests.cs | 9 +---- 53 files changed, 203 insertions(+), 505 deletions(-) diff --git a/samples/AmazonSqsAndSns/DoorOpenedConsumer.cs b/samples/AmazonSqsAndSns/DoorOpenedConsumer.cs index 764de522..9d16f046 100644 --- a/samples/AmazonSqsAndSns/DoorOpenedConsumer.cs +++ b/samples/AmazonSqsAndSns/DoorOpenedConsumer.cs @@ -1,14 +1,7 @@ namespace AmazonSqsAndSns; -public class DoorOpenedConsumer : IEventConsumer +public class DoorOpenedConsumer(ILogger logger) : IEventConsumer { - private readonly ILogger logger; - - public DoorOpenedConsumer(ILogger logger) - { - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - public Task ConsumeAsync(EventContext context, CancellationToken cancellationToken = default) { logger.LogInformation("Received event Id: {Id}", context.Id); diff --git a/samples/AmazonSqsAndSns/PublisherService.cs b/samples/AmazonSqsAndSns/PublisherService.cs index 6818e27c..72973a4b 100644 --- a/samples/AmazonSqsAndSns/PublisherService.cs +++ b/samples/AmazonSqsAndSns/PublisherService.cs @@ -1,14 +1,7 @@ namespace AmazonSqsAndSns; -public class PublisherService : BackgroundService +public class PublisherService(IEventPublisher publisher) : BackgroundService { - private readonly IEventPublisher publisher; - - public PublisherService(IEventPublisher publisher) - { - this.publisher = publisher ?? throw new ArgumentNullException(nameof(publisher)); - } - protected override async Task ExecuteAsync(CancellationToken stoppingToken) { var delay = TimeSpan.FromSeconds(30); diff --git a/samples/AzureIotHub/AzureIotEventsConsumer.cs b/samples/AzureIotHub/AzureIotEventsConsumer.cs index 4a0c6d88..ce613674 100644 --- a/samples/AzureIotHub/AzureIotEventsConsumer.cs +++ b/samples/AzureIotHub/AzureIotEventsConsumer.cs @@ -5,17 +5,10 @@ namespace AzureIotHub; [ConsumerName("$Default")] // or [ConsumerName(EventHubConsumerClient.DefaultConsumerGroupName)] -internal class AzureIotEventsConsumer : IEventConsumer +internal class AzureIotEventsConsumer(ILogger logger) : IEventConsumer { private static readonly JsonSerializerOptions serializerOptions = new(JsonSerializerDefaults.Web) { WriteIndented = true, }; - private readonly ILogger logger; - - public AzureIotEventsConsumer(ILogger logger) - { - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - public Task ConsumeAsync(EventContext context, CancellationToken cancellationToken) { var evt = context.Event; diff --git a/samples/AzureManagedIdentity/VehicleTelemetryEventsConsumer.cs b/samples/AzureManagedIdentity/VehicleTelemetryEventsConsumer.cs index 01203643..37b4c631 100644 --- a/samples/AzureManagedIdentity/VehicleTelemetryEventsConsumer.cs +++ b/samples/AzureManagedIdentity/VehicleTelemetryEventsConsumer.cs @@ -1,14 +1,7 @@ namespace AzureManagedIdentity; -internal class VehicleTelemetryEventsConsumer : IEventConsumer +internal class VehicleTelemetryEventsConsumer(ILogger logger) : IEventConsumer { - private readonly ILogger logger; - - public VehicleTelemetryEventsConsumer(ILogger logger) - { - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - public async Task ConsumeAsync(EventContext context, CancellationToken cancellationToken) { var telemetry = context.Event; diff --git a/samples/ConfigSample/VehicleTelemetryEventsConsumer.cs b/samples/ConfigSample/VehicleTelemetryEventsConsumer.cs index 81e0b88c..24a82b9d 100644 --- a/samples/ConfigSample/VehicleTelemetryEventsConsumer.cs +++ b/samples/ConfigSample/VehicleTelemetryEventsConsumer.cs @@ -1,14 +1,7 @@ namespace ConfigSample; -internal class VehicleTelemetryEventsConsumer : IEventConsumer +internal class VehicleTelemetryEventsConsumer(ILogger logger) : IEventConsumer { - private readonly ILogger logger; - - public VehicleTelemetryEventsConsumer(ILogger logger) - { - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - public async Task ConsumeAsync(EventContext context, CancellationToken cancellationToken) { var telemetry = context.Event; diff --git a/samples/ConfigSample/VisualsProducerService.cs b/samples/ConfigSample/VisualsProducerService.cs index 4c708d98..10422ac5 100644 --- a/samples/ConfigSample/VisualsProducerService.cs +++ b/samples/ConfigSample/VisualsProducerService.cs @@ -1,16 +1,7 @@ namespace ConfigSample; -internal class VisualsProducerService : BackgroundService +internal class VisualsProducerService(IEventPublisher publisher, ILogger logger) : BackgroundService { - private readonly IEventPublisher publisher; - private readonly ILogger logger; - - public VisualsProducerService(IEventPublisher publisher, ILogger logger) - { - this.publisher = publisher ?? throw new ArgumentNullException(nameof(publisher)); - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await Task.Delay(TimeSpan.FromSeconds(2), stoppingToken); // delays a little so that the logs are better visible in a better order (only ended for sample) diff --git a/samples/ConfigSample/VisualsUploadedConsumer.cs b/samples/ConfigSample/VisualsUploadedConsumer.cs index 3f8a31fd..62d2088f 100644 --- a/samples/ConfigSample/VisualsUploadedConsumer.cs +++ b/samples/ConfigSample/VisualsUploadedConsumer.cs @@ -1,16 +1,9 @@ namespace ConfigSample; -internal class VisualsUploadedConsumer : IEventConsumer, IEventConsumer +internal class VisualsUploadedConsumer(ILogger logger) : IEventConsumer, IEventConsumer { private static readonly TimeSpan SimulationDuration = TimeSpan.FromSeconds(1.3f); - private readonly ILogger logger; - - public VisualsUploadedConsumer(ILogger logger) - { - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - public async Task ConsumeAsync(EventContext context, CancellationToken cancellationToken) { var id = context.Event.ImageId; diff --git a/samples/CustomEventConfigurator/SampleConsumer1.cs b/samples/CustomEventConfigurator/SampleConsumer1.cs index 269827eb..4cbaac98 100644 --- a/samples/CustomEventConfigurator/SampleConsumer1.cs +++ b/samples/CustomEventConfigurator/SampleConsumer1.cs @@ -1,14 +1,7 @@ namespace CustomEventConfigurator; -public class SampleConsumer1 : IEventConsumer +public class SampleConsumer1(ILogger logger) : IEventConsumer { - private readonly ILogger logger; - - public SampleConsumer1(ILogger logger) - { - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - public Task ConsumeAsync(EventContext context, CancellationToken cancellationToken = default) { logger.LogInformation("Received event Id: {Id}", context.Id); diff --git a/samples/CustomEventConfigurator/SampleConsumer2.cs b/samples/CustomEventConfigurator/SampleConsumer2.cs index 58d4dc2a..a68d77c6 100644 --- a/samples/CustomEventConfigurator/SampleConsumer2.cs +++ b/samples/CustomEventConfigurator/SampleConsumer2.cs @@ -1,14 +1,7 @@ namespace CustomEventConfigurator; -public class SampleConsumer2 : IEventConsumer +public class SampleConsumer2(ILogger logger) : IEventConsumer { - private readonly ILogger logger; - - public SampleConsumer2(ILogger logger) - { - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - public Task ConsumeAsync(EventContext context, CancellationToken cancellationToken = default) { logger.LogInformation("Received event Id: {Id}", context.Id); diff --git a/samples/CustomEventSerializer/AzureDevOpsEventSerializer.cs b/samples/CustomEventSerializer/AzureDevOpsEventSerializer.cs index 37b21523..78ed6ee1 100644 --- a/samples/CustomEventSerializer/AzureDevOpsEventSerializer.cs +++ b/samples/CustomEventSerializer/AzureDevOpsEventSerializer.cs @@ -5,14 +5,10 @@ namespace CustomEventSerializer; -public class AzureDevOpsEventSerializer : AbstractEventSerializer +public class AzureDevOpsEventSerializer(IOptionsMonitor optionsAccessor, ILoggerFactory loggerFactory) : AbstractEventSerializer(optionsAccessor, loggerFactory) { private readonly JsonSerializer serializer = JsonSerializer.CreateDefault(); - public AzureDevOpsEventSerializer(IOptionsMonitor optionsAccessor, - ILoggerFactory loggerFactory) - : base(optionsAccessor, loggerFactory) { } - /// protected override IList SupportedMediaTypes => JsonContentTypes; diff --git a/samples/CustomEventSerializer/AzureDevOpsEventsConsumer.cs b/samples/CustomEventSerializer/AzureDevOpsEventsConsumer.cs index 661fa038..bb181494 100644 --- a/samples/CustomEventSerializer/AzureDevOpsEventsConsumer.cs +++ b/samples/CustomEventSerializer/AzureDevOpsEventsConsumer.cs @@ -1,14 +1,7 @@ namespace CustomEventSerializer; -internal class AzureDevOpsEventsConsumer : IEventConsumer +internal class AzureDevOpsEventsConsumer(ILogger logger) : IEventConsumer { - private readonly ILogger logger; - - public AzureDevOpsEventsConsumer(ILogger logger) - { - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - public Task ConsumeAsync(EventContext context, CancellationToken cancellationToken = default) { var @event = context.Event; @@ -17,7 +10,7 @@ public Task ConsumeAsync(EventContext context, Cancellati var defaultBranch = repository?.DefaultBranch; // get the updated branches (refs) - var updatedReferences = resource?.RefUpdates?.Select(ru => ru.Name).ToList() ?? new List(); + var updatedReferences = resource?.RefUpdates?.Select(ru => ru.Name).ToList() ?? []; logger.LogInformation("Default branch: ({DefaultBranch})", defaultBranch); logger.LogInformation("Updated branches (references):\r\n- {ChangedReferences}", string.Join("\r\n- ", updatedReferences)); diff --git a/samples/InMemoryBackgroundProcessing/Program.cs b/samples/InMemoryBackgroundProcessing/Program.cs index e6635aa0..04c96784 100644 --- a/samples/InMemoryBackgroundProcessing/Program.cs +++ b/samples/InMemoryBackgroundProcessing/Program.cs @@ -21,15 +21,8 @@ await host.RunAsync(); -class ProducerService : BackgroundService +class ProducerService(IEventPublisher publisher) : BackgroundService { - private readonly IEventPublisher publisher; - - public ProducerService(IEventPublisher publisher) - { - this.publisher = publisher ?? throw new ArgumentNullException(nameof(publisher)); - } - protected override async Task ExecuteAsync(CancellationToken stoppingToken) { var delay = TimeSpan.FromSeconds(25); @@ -54,17 +47,10 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) } } -class VideoUploadedConsumer : IEventConsumer +class VideoUploadedConsumer(ILogger logger) : IEventConsumer { private static readonly TimeSpan SimulationDuration = TimeSpan.FromSeconds(3); - private readonly ILogger logger; - - public VideoUploadedConsumer(ILogger logger) - { - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - public async Task ConsumeAsync(EventContext context, CancellationToken cancellationToken = default) { var evt = context.Event; diff --git a/samples/MultiEventsConsumer/DummyProducerService.cs b/samples/MultiEventsConsumer/DummyProducerService.cs index 00481b7d..1965fc25 100644 --- a/samples/MultiEventsConsumer/DummyProducerService.cs +++ b/samples/MultiEventsConsumer/DummyProducerService.cs @@ -1,16 +1,7 @@ namespace MultiEventsConsumer; -internal class DummyProducerService : BackgroundService +internal partial class DummyProducerService(IEventPublisher publisher, ILogger logger) : BackgroundService { - private readonly IEventPublisher publisher; - private readonly ILogger logger; - - public DummyProducerService(IEventPublisher publisher, ILogger logger) - { - this.publisher = publisher ?? throw new ArgumentNullException(nameof(publisher)); - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - protected override async Task ExecuteAsync(CancellationToken stoppingToken) { // Wait for bus to be ready @@ -73,6 +64,9 @@ private static string GenerateRandomString(Random random) var bys = new byte[20]; random.NextBytes(bys); var result = Convert.ToBase64String(bys); - return System.Text.RegularExpressions.Regex.Replace(result, "[^A-Za-z0-9]", ""); + return AlphaNumeric().Replace(result, ""); } + + [System.Text.RegularExpressions.GeneratedRegex("[^A-Za-z0-9]")] + private static partial System.Text.RegularExpressions.Regex AlphaNumeric(); } diff --git a/samples/MultiEventsConsumer/MultiEventsConsumer.cs b/samples/MultiEventsConsumer/MultiEventsConsumer.cs index ac131c3f..a8094645 100644 --- a/samples/MultiEventsConsumer/MultiEventsConsumer.cs +++ b/samples/MultiEventsConsumer/MultiEventsConsumer.cs @@ -2,19 +2,10 @@ namespace MultiEventsConsumer; -public class MultiEventsConsumer : IEventConsumer, IEventConsumer +public class MultiEventsConsumer(IDistributedCache cache, ILogger logger) : IEventConsumer, IEventConsumer { private static readonly TimeSpan SimulationDuration = TimeSpan.FromSeconds(3); - private readonly IDistributedCache cache; - private readonly ILogger logger; - - public MultiEventsConsumer(IDistributedCache cache, ILogger logger) - { - this.cache = cache ?? throw new ArgumentNullException(nameof(cache)); - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - public async Task ConsumeAsync(EventContext context, CancellationToken cancellationToken = default) { var evt = context.Event; diff --git a/samples/MultipleConsumers/FirstEventConsumer.cs b/samples/MultipleConsumers/FirstEventConsumer.cs index 089f6c80..36527f11 100644 --- a/samples/MultipleConsumers/FirstEventConsumer.cs +++ b/samples/MultipleConsumers/FirstEventConsumer.cs @@ -1,14 +1,7 @@ namespace MultipleConsumers; -public class FirstEventConsumer : IEventConsumer +public class FirstEventConsumer(ILogger logger) : IEventConsumer { - private readonly ILogger logger; - - public FirstEventConsumer(ILogger logger) - { - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - public Task ConsumeAsync(EventContext context, CancellationToken cancellationToken = default) { var evt = context.Event; diff --git a/samples/MultipleConsumers/PublisherService.cs b/samples/MultipleConsumers/PublisherService.cs index bef71bfb..f29d612a 100644 --- a/samples/MultipleConsumers/PublisherService.cs +++ b/samples/MultipleConsumers/PublisherService.cs @@ -1,14 +1,7 @@ namespace MultipleConsumers; -public class PublisherService : BackgroundService +public class PublisherService(IEventPublisher publisher) : BackgroundService { - private readonly IEventPublisher publisher; - - public PublisherService(IEventPublisher publisher) - { - this.publisher = publisher ?? throw new ArgumentNullException(nameof(publisher)); - } - protected override async Task ExecuteAsync(CancellationToken stoppingToken) { var delay = TimeSpan.FromSeconds(30); diff --git a/samples/MultipleConsumers/SecondEventConsumer.cs b/samples/MultipleConsumers/SecondEventConsumer.cs index 59f9b62a..18707647 100644 --- a/samples/MultipleConsumers/SecondEventConsumer.cs +++ b/samples/MultipleConsumers/SecondEventConsumer.cs @@ -1,14 +1,7 @@ namespace MultipleConsumers; -public class SecondEventConsumer : IEventConsumer +public class SecondEventConsumer(ILogger logger) : IEventConsumer { - private readonly ILogger logger; - - public SecondEventConsumer(ILogger logger) - { - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - public Task ConsumeAsync(EventContext context, CancellationToken cancellationToken = default) { var evt = context.Event; diff --git a/samples/MultipleDifferentTransports/VehicleTelemetryEventsConsumer.cs b/samples/MultipleDifferentTransports/VehicleTelemetryEventsConsumer.cs index 34d5112d..1a8456e0 100644 --- a/samples/MultipleDifferentTransports/VehicleTelemetryEventsConsumer.cs +++ b/samples/MultipleDifferentTransports/VehicleTelemetryEventsConsumer.cs @@ -4,15 +4,8 @@ namespace MultipleDifferentTransports; [ConsumerName("$Default")] // or [ConsumerName(EventHubConsumerClient.DefaultConsumerGroupName)] -internal class VehicleTelemetryEventsConsumer : IEventConsumer +internal class VehicleTelemetryEventsConsumer(ILogger logger) : IEventConsumer { - private readonly ILogger logger; - - public VehicleTelemetryEventsConsumer(ILogger logger) - { - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - public async Task ConsumeAsync(EventContext context, CancellationToken cancellationToken) { var evt = context.Event; diff --git a/samples/MultipleSimilarTransports/Program.cs b/samples/MultipleSimilarTransports/Program.cs index f38f4168..e0e59ec6 100644 --- a/samples/MultipleSimilarTransports/Program.cs +++ b/samples/MultipleSimilarTransports/Program.cs @@ -21,17 +21,8 @@ await host.RunAsync(); -class VisualsProducerService : BackgroundService +class VisualsProducerService(IEventPublisher publisher, ILogger logger) : BackgroundService { - private readonly IEventPublisher publisher; - private readonly ILogger logger; - - public VisualsProducerService(IEventPublisher publisher, ILogger logger) - { - this.publisher = publisher ?? throw new ArgumentNullException(nameof(publisher)); - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await Task.Delay(TimeSpan.FromSeconds(2), stoppingToken); // delays a little so that the logs are better visible in a better order (only ended for sample) @@ -62,17 +53,10 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) => await publisher.PublishAsync(@event, cancellationToken: cancellationToken); } -class VisualsUploadedConsumer : IEventConsumer, IEventConsumer +class VisualsUploadedConsumer(ILogger logger) : IEventConsumer, IEventConsumer { private static readonly TimeSpan SimulationDuration = TimeSpan.FromSeconds(1.3f); - private readonly ILogger logger; - - public VisualsUploadedConsumer(ILogger logger) - { - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - public async Task ConsumeAsync(EventContext context, CancellationToken cancellationToken) { var id = context.Event.ImageId; diff --git a/samples/SimpleConsumer/SampleEventConsumer.cs b/samples/SimpleConsumer/SampleEventConsumer.cs index 17f4d940..8d9ce30b 100644 --- a/samples/SimpleConsumer/SampleEventConsumer.cs +++ b/samples/SimpleConsumer/SampleEventConsumer.cs @@ -1,16 +1,7 @@ namespace SimpleConsumer; -public class SampleEventConsumer : IEventConsumer +public class SampleEventConsumer(EventCounter counter, ILogger logger) : IEventConsumer { - private readonly EventCounter counter; - private readonly ILogger logger; - - public SampleEventConsumer(EventCounter counter, ILogger logger) - { - this.counter = counter ?? throw new ArgumentNullException(nameof(counter)); - this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - public Task ConsumeAsync(EventContext context, CancellationToken cancellationToken = default) { logger.LogInformation("Received event Id: {Id}", context.Id); diff --git a/samples/SimplePublisher/PublisherService.cs b/samples/SimplePublisher/PublisherService.cs index 6b1c34f6..0b62f94d 100644 --- a/samples/SimplePublisher/PublisherService.cs +++ b/samples/SimplePublisher/PublisherService.cs @@ -1,14 +1,7 @@ namespace SimplePublisher; -public class PublisherService : BackgroundService +public class PublisherService(IEventPublisher publisher) : BackgroundService { - private readonly IEventPublisher publisher; - - public PublisherService(IEventPublisher publisher) - { - this.publisher = publisher ?? throw new ArgumentNullException(nameof(publisher)); - } - protected override async Task ExecuteAsync(CancellationToken stoppingToken) { var delay = TimeSpan.FromSeconds(30); diff --git a/src/Tingle.EventBus.Transports.Amazon.Abstractions/AmazonTransportConfigureOptions.cs b/src/Tingle.EventBus.Transports.Amazon.Abstractions/AmazonTransportConfigureOptions.cs index 638cc193..2d3e51f7 100644 --- a/src/Tingle.EventBus.Transports.Amazon.Abstractions/AmazonTransportConfigureOptions.cs +++ b/src/Tingle.EventBus.Transports.Amazon.Abstractions/AmazonTransportConfigureOptions.cs @@ -8,18 +8,14 @@ namespace Microsoft.Extensions.DependencyInjection; /// /// A class to finish the configuration of instances of derivatives. /// -public abstract class AmazonTransportConfigureOptions : EventBusTransportConfigureOptions where TOptions : AmazonTransportOptions +/// An instance.\ +/// A list of to use when configuring options. +/// An for bus configuration.\ +public abstract class AmazonTransportConfigureOptions(IEventBusConfigurationProvider configurationProvider, + IEnumerable configurators, + IOptions busOptionsAccessor) + : EventBusTransportConfigureOptions(configurationProvider, configurators, busOptionsAccessor) where TOptions : AmazonTransportOptions { - /// - /// Initializes a new given the configuration - /// provided by the . - /// - /// An instance.\ - /// A list of to use when configuring options. - /// An for bus configuration.\ - public AmazonTransportConfigureOptions(IEventBusConfigurationProvider configurationProvider, IEnumerable configurators, IOptions busOptionsAccessor) - : base(configurationProvider, configurators, busOptionsAccessor) { } - /// public override void PostConfigure(string? name, TOptions options) { diff --git a/src/Tingle.EventBus.Transports.Amazon.Kinesis/AmazonKinesisConfigureOptions.cs b/src/Tingle.EventBus.Transports.Amazon.Kinesis/AmazonKinesisConfigureOptions.cs index 2a14e24b..24306e73 100644 --- a/src/Tingle.EventBus.Transports.Amazon.Kinesis/AmazonKinesisConfigureOptions.cs +++ b/src/Tingle.EventBus.Transports.Amazon.Kinesis/AmazonKinesisConfigureOptions.cs @@ -7,18 +7,14 @@ namespace Microsoft.Extensions.DependencyInjection; /// /// A class to finish the configuration of instances of . /// -internal class AmazonKinesisConfigureOptions : AmazonTransportConfigureOptions +/// An instance.\ +/// A list of to use when configuring options. +/// An for bus configuration.\ +internal class AmazonKinesisConfigureOptions(IEventBusConfigurationProvider configurationProvider, + IEnumerable configurators, + IOptions busOptionsAccessor) + : AmazonTransportConfigureOptions(configurationProvider, configurators, busOptionsAccessor) { - /// - /// Initializes a new given the configuration - /// provided by the . - /// - /// An instance.\ - /// A list of to use when configuring options. - /// An for bus configuration.\ - public AmazonKinesisConfigureOptions(IEventBusConfigurationProvider configurationProvider, IEnumerable configurators, IOptions busOptionsAccessor) - : base(configurationProvider, configurators, busOptionsAccessor) { } - /// public override void PostConfigure(string? name, AmazonKinesisTransportOptions options) { diff --git a/src/Tingle.EventBus.Transports.Amazon.Kinesis/BinaryDataExtensions.cs b/src/Tingle.EventBus.Transports.Amazon.Kinesis/BinaryDataExtensions.cs index 4cdc6202..42c7fad8 100644 --- a/src/Tingle.EventBus.Transports.Amazon.Kinesis/BinaryDataExtensions.cs +++ b/src/Tingle.EventBus.Transports.Amazon.Kinesis/BinaryDataExtensions.cs @@ -10,5 +10,5 @@ internal static class BinaryDataExtensions /// /// The to be converted. /// A representing the data. - public static MemoryStream ToMemoryStream(this BinaryData data) => new MemoryStream(data.ToArray()); + public static MemoryStream ToMemoryStream(this BinaryData data) => new(data.ToArray()); } diff --git a/src/Tingle.EventBus.Transports.Amazon.Sqs/AmazonSqsConfigureOptions.cs b/src/Tingle.EventBus.Transports.Amazon.Sqs/AmazonSqsConfigureOptions.cs index e966026f..2591a469 100644 --- a/src/Tingle.EventBus.Transports.Amazon.Sqs/AmazonSqsConfigureOptions.cs +++ b/src/Tingle.EventBus.Transports.Amazon.Sqs/AmazonSqsConfigureOptions.cs @@ -8,18 +8,14 @@ namespace Microsoft.Extensions.DependencyInjection; /// /// A class to finish the configuration of instances of . /// -internal class AmazonSqsConfigureOptions : AmazonTransportConfigureOptions +/// An instance.\ +/// A list of to use when configuring options. +/// An for bus configuration.\ +internal class AmazonSqsConfigureOptions(IEventBusConfigurationProvider configurationProvider, + IEnumerable configurators, + IOptions busOptionsAccessor) + : AmazonTransportConfigureOptions(configurationProvider, configurators, busOptionsAccessor) { - /// - /// Initializes a new given the configuration - /// provided by the . - /// - /// An instance.\ - /// A list of to use when configuring options. - /// An for bus configuration.\ - public AmazonSqsConfigureOptions(IEventBusConfigurationProvider configurationProvider, IEnumerable configurators, IOptions busOptionsAccessor) - : base(configurationProvider, configurators, busOptionsAccessor) { } - /// public override void PostConfigure(string? name, AmazonSqsTransportOptions options) { diff --git a/src/Tingle.EventBus.Transports.Amazon.Sqs/AmazonSqsTransport.cs b/src/Tingle.EventBus.Transports.Amazon.Sqs/AmazonSqsTransport.cs index 361a0346..ee80072c 100644 --- a/src/Tingle.EventBus.Transports.Amazon.Sqs/AmazonSqsTransport.cs +++ b/src/Tingle.EventBus.Transports.Amazon.Sqs/AmazonSqsTransport.cs @@ -24,7 +24,7 @@ public class AmazonSqsTransport : EventBusTransport, private readonly EventBusConcurrentDictionary topicArnsCache = new(); private readonly EventBusConcurrentDictionary queueUrlsCache = new(); private readonly CancellationTokenSource stoppingCts = new(); - private readonly List receiverTasks = new(); + private readonly List receiverTasks = []; private readonly Lazy snsClient; private readonly Lazy sqsClient; private bool disposedValue; diff --git a/src/Tingle.EventBus.Transports.Azure.Abstractions/AzureTransportConfigureOptions.cs b/src/Tingle.EventBus.Transports.Azure.Abstractions/AzureTransportConfigureOptions.cs index 8f1d04b1..7f5a1a92 100644 --- a/src/Tingle.EventBus.Transports.Azure.Abstractions/AzureTransportConfigureOptions.cs +++ b/src/Tingle.EventBus.Transports.Azure.Abstractions/AzureTransportConfigureOptions.cs @@ -6,20 +6,16 @@ namespace Microsoft.Extensions.DependencyInjection; /// /// A class to finish the configuration of instances of derivatives. /// -public abstract class AzureTransportConfigureOptions : EventBusTransportConfigureOptions +/// An instance.\ +/// A list of to use when configuring options. +/// An for bus configuration.\ +public abstract class AzureTransportConfigureOptions(IEventBusConfigurationProvider configurationProvider, + IEnumerable configurators, + IOptions busOptionsAccessor) + : EventBusTransportConfigureOptions(configurationProvider, configurators, busOptionsAccessor) where TCredential : AzureTransportCredentials where TOptions : AzureTransportOptions { - /// - /// Initializes a new given the configuration - /// provided by the . - /// - /// An instance.\ - /// A list of to use when configuring options. - /// An for bus configuration.\ - public AzureTransportConfigureOptions(IEventBusConfigurationProvider configurationProvider, IEnumerable configurators, IOptions busOptionsAccessor) - : base(configurationProvider, configurators, busOptionsAccessor) { } - /// public override ValidateOptionsResult Validate(string? name, TOptions options) { diff --git a/src/Tingle.EventBus.Transports.Azure.EventHubs/AzureEventHubsConfigureOptions.cs b/src/Tingle.EventBus.Transports.Azure.EventHubs/AzureEventHubsConfigureOptions.cs index 0baa1b4e..1bf9b96b 100644 --- a/src/Tingle.EventBus.Transports.Azure.EventHubs/AzureEventHubsConfigureOptions.cs +++ b/src/Tingle.EventBus.Transports.Azure.EventHubs/AzureEventHubsConfigureOptions.cs @@ -7,19 +7,15 @@ namespace Microsoft.Extensions.DependencyInjection; /// /// A class to finish the configuration of instances of . /// -internal class AzureEventHubsConfigureOptions : AzureTransportConfigureOptions, - IConfigureNamedOptions +/// An instance.\ +/// A list of to use when configuring options. +/// An for bus configuration.\ +internal class AzureEventHubsConfigureOptions(IEventBusConfigurationProvider configurationProvider, + IEnumerable configurators, + IOptions busOptionsAccessor) + : AzureTransportConfigureOptions(configurationProvider, configurators, busOptionsAccessor), + IConfigureNamedOptions { - /// - /// Initializes a new given the configuration - /// provided by the . - /// - /// An instance.\ - /// A list of to use when configuring options. - /// An for bus configuration.\ - public AzureEventHubsConfigureOptions(IEventBusConfigurationProvider configurationProvider, IEnumerable configurators, IOptions busOptionsAccessor) - : base(configurationProvider, configurators, busOptionsAccessor) { } - /// protected override void Configure(IConfiguration configuration, AzureEventHubsTransportOptions options) { diff --git a/src/Tingle.EventBus.Transports.Azure.EventHubs/AzureEventHubsTransport.cs b/src/Tingle.EventBus.Transports.Azure.EventHubs/AzureEventHubsTransport.cs index 698ee541..2931dc45 100644 --- a/src/Tingle.EventBus.Transports.Azure.EventHubs/AzureEventHubsTransport.cs +++ b/src/Tingle.EventBus.Transports.Azure.EventHubs/AzureEventHubsTransport.cs @@ -19,7 +19,15 @@ namespace Tingle.EventBus.Transports.Azure.EventHubs; /// /// Implementation of using Azure Event Hubs. /// -public class AzureEventHubsTransport : EventBusTransport +/// +/// +/// +/// +public class AzureEventHubsTransport(IServiceScopeFactory serviceScopeFactory, + IOptions busOptionsAccessor, + IOptionsMonitor optionsMonitor, + ILoggerFactory loggerFactory) + : EventBusTransport(serviceScopeFactory, busOptionsAccessor, optionsMonitor, loggerFactory) { private readonly EventBusConcurrentDictionary<(Type, bool), EventHubProducerClient> producersCache = new(); private readonly EventBusConcurrentDictionary processorsCache = new(); @@ -27,19 +35,6 @@ public class AzureEventHubsTransport : EventBusTransport checkpointingCounter = new(); private BlobContainerClient? blobContainerClient; - /// - /// - /// - /// - /// - /// - /// - public AzureEventHubsTransport(IServiceScopeFactory serviceScopeFactory, - IOptions busOptionsAccessor, - IOptionsMonitor optionsMonitor, - ILoggerFactory loggerFactory) - : base(serviceScopeFactory, busOptionsAccessor, optionsMonitor, loggerFactory) { } - /// protected override async Task StartCoreAsync(CancellationToken cancellationToken) { diff --git a/src/Tingle.EventBus.Transports.Azure.EventHubs/IotHub/IotHubEventSerializer.cs b/src/Tingle.EventBus.Transports.Azure.EventHubs/IotHub/IotHubEventSerializer.cs index ea72b65d..39af6ec7 100644 --- a/src/Tingle.EventBus.Transports.Azure.EventHubs/IotHub/IotHubEventSerializer.cs +++ b/src/Tingle.EventBus.Transports.Azure.EventHubs/IotHub/IotHubEventSerializer.cs @@ -9,14 +9,12 @@ namespace Tingle.EventBus.Transports.Azure.EventHubs.IotHub; -internal class IotHubEventSerializer : AbstractEventSerializer +internal class IotHubEventSerializer(IOptionsMonitor optionsAccessor, + ILoggerFactory loggerFactory) + : AbstractEventSerializer(optionsAccessor, loggerFactory) { private static readonly Type BaseType = typeof(IotHubEvent); - public IotHubEventSerializer(IOptionsMonitor optionsAccessor, - ILoggerFactory loggerFactory) - : base(optionsAccessor, loggerFactory) { } - /// protected override IList SupportedMediaTypes => JsonContentTypes; diff --git a/src/Tingle.EventBus.Transports.Azure.QueueStorage/AzureQueueStorageConfigureOptions.cs b/src/Tingle.EventBus.Transports.Azure.QueueStorage/AzureQueueStorageConfigureOptions.cs index 5a62b2b7..7e105082 100644 --- a/src/Tingle.EventBus.Transports.Azure.QueueStorage/AzureQueueStorageConfigureOptions.cs +++ b/src/Tingle.EventBus.Transports.Azure.QueueStorage/AzureQueueStorageConfigureOptions.cs @@ -7,19 +7,15 @@ namespace Microsoft.Extensions.DependencyInjection; /// /// A class to finish the configuration of instances of . /// -internal class AzureQueueStorageConfigureOptions : AzureTransportConfigureOptions, - IConfigureNamedOptions +/// An instance.\ +/// A list of to use when configuring options. +/// An for bus configuration.\ +internal class AzureQueueStorageConfigureOptions(IEventBusConfigurationProvider configurationProvider, + IEnumerable configurators, + IOptions busOptionsAccessor) + : AzureTransportConfigureOptions(configurationProvider, configurators, busOptionsAccessor), + IConfigureNamedOptions { - /// - /// Initializes a new given the configuration - /// provided by the . - /// - /// An instance.\ - /// A list of to use when configuring options. - /// An for bus configuration.\ - public AzureQueueStorageConfigureOptions(IEventBusConfigurationProvider configurationProvider, IEnumerable configurators, IOptions busOptionsAccessor) - : base(configurationProvider, configurators, busOptionsAccessor) { } - /// protected override void Configure(IConfiguration configuration, AzureQueueStorageTransportOptions options) { diff --git a/src/Tingle.EventBus.Transports.Azure.QueueStorage/AzureQueueStorageTransport.cs b/src/Tingle.EventBus.Transports.Azure.QueueStorage/AzureQueueStorageTransport.cs index 35791a92..f7240ed4 100644 --- a/src/Tingle.EventBus.Transports.Azure.QueueStorage/AzureQueueStorageTransport.cs +++ b/src/Tingle.EventBus.Transports.Azure.QueueStorage/AzureQueueStorageTransport.cs @@ -18,7 +18,7 @@ public class AzureQueueStorageTransport : EventBusTransport queueClientsCache = new(); private readonly CancellationTokenSource stoppingCts = new(); - private readonly List receiverTasks = new(); + private readonly List receiverTasks = []; private readonly Lazy serviceClient; private bool disposedValue; diff --git a/src/Tingle.EventBus.Transports.Azure.ServiceBus/AzureServiceBusConfigureOptions.cs b/src/Tingle.EventBus.Transports.Azure.ServiceBus/AzureServiceBusConfigureOptions.cs index 49a87e4a..38a9b47f 100644 --- a/src/Tingle.EventBus.Transports.Azure.ServiceBus/AzureServiceBusConfigureOptions.cs +++ b/src/Tingle.EventBus.Transports.Azure.ServiceBus/AzureServiceBusConfigureOptions.cs @@ -7,19 +7,15 @@ namespace Microsoft.Extensions.DependencyInjection; /// /// A class to finish the configuration of instances of . /// -internal class AzureServiceBusConfigureOptions : AzureTransportConfigureOptions, - IConfigureNamedOptions +/// An instance.\ +/// A list of to use when configuring options. +/// An for bus configuration.\ +internal class AzureServiceBusConfigureOptions(IEventBusConfigurationProvider configurationProvider, + IEnumerable configurators, + IOptions busOptionsAccessor) + : AzureTransportConfigureOptions(configurationProvider, configurators, busOptionsAccessor), + IConfigureNamedOptions { - /// - /// Initializes a new given the configuration - /// provided by the . - /// - /// An instance.\ - /// A list of to use when configuring options. - /// An for bus configuration.\ - public AzureServiceBusConfigureOptions(IEventBusConfigurationProvider configurationProvider, IEnumerable configurators, IOptions busOptionsAccessor) - : base(configurationProvider, configurators, busOptionsAccessor) { } - /// protected override void Configure(IConfiguration configuration, AzureServiceBusTransportOptions options) { diff --git a/src/Tingle.EventBus.Transports.InMemory/Client/BroadcastChannelWriter.cs b/src/Tingle.EventBus.Transports.InMemory/Client/BroadcastChannelWriter.cs index 3fa672b0..befd4af2 100644 --- a/src/Tingle.EventBus.Transports.InMemory/Client/BroadcastChannelWriter.cs +++ b/src/Tingle.EventBus.Transports.InMemory/Client/BroadcastChannelWriter.cs @@ -2,14 +2,9 @@ namespace Tingle.EventBus.Transports.InMemory.Client; -internal class BroadcastChannelWriter : ChannelWriter +internal class BroadcastChannelWriter(ICollection>? children = null) : ChannelWriter { - public BroadcastChannelWriter(ICollection>? children = null) - { - Children = children ?? new List>(); - } - - public ICollection> Children { get; } + public ICollection> Children { get; } = children ?? new List>(); /// public override bool TryWrite(T item) => Children.All(w => w.TryWrite(item)); diff --git a/src/Tingle.EventBus.Transports.InMemory/Client/InMemoryClient.cs b/src/Tingle.EventBus.Transports.InMemory/Client/InMemoryClient.cs index f1a3ebb0..f4634beb 100644 --- a/src/Tingle.EventBus.Transports.InMemory/Client/InMemoryClient.cs +++ b/src/Tingle.EventBus.Transports.InMemory/Client/InMemoryClient.cs @@ -3,15 +3,10 @@ namespace Tingle.EventBus.Transports.InMemory.Client; -internal class InMemoryClient +internal class InMemoryClient(SequenceNumberGenerator sng) { private readonly ConcurrentDictionary> channels = new(); - private readonly SequenceNumberGenerator sng; - - public InMemoryClient(SequenceNumberGenerator sng) - { - this.sng = sng ?? throw new ArgumentNullException(nameof(sng)); - } + private readonly SequenceNumberGenerator sng = sng ?? throw new ArgumentNullException(nameof(sng)); /// /// Creates an instance that can be used to process messages diff --git a/src/Tingle.EventBus.Transports.InMemory/Client/InMemoryMessage.cs b/src/Tingle.EventBus.Transports.InMemory/Client/InMemoryMessage.cs index 7253ba58..04913da9 100644 --- a/src/Tingle.EventBus.Transports.InMemory/Client/InMemoryMessage.cs +++ b/src/Tingle.EventBus.Transports.InMemory/Client/InMemoryMessage.cs @@ -4,7 +4,8 @@ /// The is used to receive /// and send data from and to InMemory entities. /// -public class InMemoryMessage +/// The payload of the message. +public class InMemoryMessage(BinaryData body) { /// /// Creates a new message from the specified string, using UTF-8 encoding. @@ -18,15 +19,6 @@ public InMemoryMessage(string body) : this(BinaryData.FromString(body)) { } /// The payload of the message in bytes. public InMemoryMessage(ReadOnlyMemory body) : this(BinaryData.FromBytes(body)) { } - /// - /// Creates a new message from specified instance. - /// - /// The payload of the message. - public InMemoryMessage(BinaryData body) - { - Body = body ?? throw new ArgumentNullException(nameof(body)); - } - internal InMemoryMessage(InMemoryReceivedMessage message) : this(message?.Body ?? throw new ArgumentNullException(nameof(message))) { ContentType = message.ContentType; @@ -72,7 +64,7 @@ public InMemoryMessage(BinaryData body) /// /// Gets or sets the body of the message. /// - public BinaryData Body { get; set; } + public BinaryData Body { get; set; } = body ?? throw new ArgumentNullException(nameof(body)); /// /// Gets the unique number assigned to a message by the transport. diff --git a/src/Tingle.EventBus.Transports.InMemory/Client/InMemoryProcessor.cs b/src/Tingle.EventBus.Transports.InMemory/Client/InMemoryProcessor.cs index 66d7f569..99feb7f3 100644 --- a/src/Tingle.EventBus.Transports.InMemory/Client/InMemoryProcessor.cs +++ b/src/Tingle.EventBus.Transports.InMemory/Client/InMemoryProcessor.cs @@ -2,7 +2,13 @@ namespace Tingle.EventBus.Transports.InMemory.Client; -internal class InMemoryProcessor : IDisposable +internal interface IInMemoryProcessor : IDisposable +{ + Task StartProcessingAsync(CancellationToken cancellationToken = default); + Task StopProcessingAsync(CancellationToken cancellationToken = default); +} + +internal class InMemoryProcessor : IInMemoryProcessor { private readonly ChannelReader reader; private CancellationTokenSource stoppingCts = new(); diff --git a/src/Tingle.EventBus.Transports.InMemory/Client/InMemorySender.cs b/src/Tingle.EventBus.Transports.InMemory/Client/InMemorySender.cs index ef2f740e..ea04a856 100644 --- a/src/Tingle.EventBus.Transports.InMemory/Client/InMemorySender.cs +++ b/src/Tingle.EventBus.Transports.InMemory/Client/InMemorySender.cs @@ -2,7 +2,12 @@ namespace Tingle.EventBus.Transports.InMemory.Client; -internal sealed class InMemorySender : IDisposable +internal interface IInMemorySender : IDisposable +{ + Task CloseAsync(CancellationToken cancellationToken = default); +} + +internal sealed class InMemorySender : IInMemorySender { private static readonly TimeSpan waitTimeout = TimeSpan.FromSeconds(1); @@ -12,7 +17,7 @@ internal sealed class InMemorySender : IDisposable private readonly SemaphoreSlim updateLock = new(1); private readonly CancellationTokenSource stoppingCts = new(); - private List items = new(); + private List items = []; public InMemorySender(string entityPath, ChannelWriter writer, SequenceNumberGenerator sng) { @@ -148,11 +153,8 @@ public async Task CancelScheduledMessageAsync(long sequenceNumber, CancellationT try { // get matching - var matching = items.SingleOrDefault(m => m.SequenceNumber == sequenceNumber); - if (matching is null) - { - throw new ArgumentException($"An item with the sequence number {sequenceNumber} does not exist.", nameof(sequenceNumber)); - } + var matching = items.SingleOrDefault(m => m.SequenceNumber == sequenceNumber) + ?? throw new ArgumentException($"An item with the sequence number {sequenceNumber} does not exist.", nameof(sequenceNumber)); // make new items and replace items = items.AsEnumerable().Except(new[] { matching }).ToList(); @@ -180,11 +182,8 @@ public async Task CancelScheduledMessagesAsync(IEnumerable sequenceNumbers var matching = new List(); foreach (var sn in sequenceNumbers) { - var item = items.SingleOrDefault(m => m.SequenceNumber == sn); - if (item is null) - { - throw new ArgumentException($"An item with the sequence number {sn} does not exist.", nameof(sn)); - } + var item = items.SingleOrDefault(m => m.SequenceNumber == sn) + ?? throw new ArgumentException($"An item with the sequence number {sn} does not exist.", nameof(sn)); } // make new items and replace diff --git a/src/Tingle.EventBus.Transports.InMemory/Client/ProcessErrorEventArgs.cs b/src/Tingle.EventBus.Transports.InMemory/Client/ProcessErrorEventArgs.cs index 12438d5d..3a0297f7 100644 --- a/src/Tingle.EventBus.Transports.InMemory/Client/ProcessErrorEventArgs.cs +++ b/src/Tingle.EventBus.Transports.InMemory/Client/ProcessErrorEventArgs.cs @@ -4,45 +4,34 @@ /// Contains information about the entity whose processing threw an exception, as /// well as the exception that has been thrown. /// -internal class ProcessErrorEventArgs : EventArgs +/// The exception that triggered the call to the error event handler. +/// The source associated with the error. +/// The entity path used when this exception occurred. +/// +/// The processor's instance which will be cancelled +/// in the event that is called. +/// +internal class ProcessErrorEventArgs(Exception exception, InMemoryErrorSource errorSource, string entityPath, CancellationToken cancellationToken) : EventArgs { - /// - /// Initializes a new instance of the class. - /// - /// The exception that triggered the call to the error event handler. - /// The source associated with the error. - /// The entity path used when this exception occurred. - /// - /// The processor's instance which will be cancelled - /// in the event that is called. - /// - public ProcessErrorEventArgs(Exception exception, InMemoryErrorSource errorSource, string entityPath, CancellationToken cancellationToken) - { - Exception = exception; - ErrorSource = errorSource; - EntityPath = entityPath; - CancellationToken = cancellationToken; - } - /// /// Gets the exception that triggered the call to the error event handler. /// - public Exception Exception { get; } + public Exception Exception { get; } = exception; /// /// Gets the source associated with the error. /// - public InMemoryErrorSource ErrorSource { get; } + public InMemoryErrorSource ErrorSource { get; } = errorSource; /// /// Gets the entity path associated with the error event. /// - public string EntityPath { get; } + public string EntityPath { get; } = entityPath; /// /// Gets the processor's instance which will be /// cancelled when /// is called. /// - public CancellationToken CancellationToken { get; } + public CancellationToken CancellationToken { get; } = cancellationToken; } diff --git a/src/Tingle.EventBus.Transports.InMemory/Client/ProcessMessageEventArgs.cs b/src/Tingle.EventBus.Transports.InMemory/Client/ProcessMessageEventArgs.cs index b84f497d..60363eca 100644 --- a/src/Tingle.EventBus.Transports.InMemory/Client/ProcessMessageEventArgs.cs +++ b/src/Tingle.EventBus.Transports.InMemory/Client/ProcessMessageEventArgs.cs @@ -4,29 +4,19 @@ /// The contain event args that are specific /// to the that is being processed. /// -internal class ProcessMessageEventArgs : EventArgs +/// The message to be processed. +/// +/// The processor's which will be cancelled +/// in the event that is called. +/// +internal class ProcessMessageEventArgs(InMemoryReceivedMessage message, CancellationToken cancellationToken) : EventArgs { - /// - /// Initializes a new instance of the class. - /// - /// The message to be processed. - /// - /// The processor's which will be cancelled - /// in the event that is called. - /// - /// - public ProcessMessageEventArgs(InMemoryReceivedMessage message, CancellationToken cancellationToken) - { - Message = message; - CancellationToken = cancellationToken; - } - /// The received message to be processed. - public InMemoryReceivedMessage Message { get; } + public InMemoryReceivedMessage Message { get; } = message; /// /// The processor's instance which will be cancelled /// when is called. /// - public CancellationToken CancellationToken { get; } + public CancellationToken CancellationToken { get; } = cancellationToken; } diff --git a/src/Tingle.EventBus.Transports.InMemory/InMemoryTransport.cs b/src/Tingle.EventBus.Transports.InMemory/InMemoryTransport.cs index 9f58dfa6..fd8988c7 100644 --- a/src/Tingle.EventBus.Transports.InMemory/InMemoryTransport.cs +++ b/src/Tingle.EventBus.Transports.InMemory/InMemoryTransport.cs @@ -16,34 +16,26 @@ namespace Tingle.EventBus.Transports.InMemory; /// Implementation of using an in-memory transport. /// This implementation should only be used for unit testing or similar scenarios as it does not offer persistence. /// -public class InMemoryTransport : EventBusTransport +/// +/// +/// +/// +/// +public class InMemoryTransport(IServiceScopeFactory serviceScopeFactory, + IOptions busOptionsAccessor, + IOptionsMonitor optionsMonitor, + ILoggerFactory loggerFactory, + SequenceNumberGenerator sng) + : EventBusTransport(serviceScopeFactory, busOptionsAccessor, optionsMonitor, loggerFactory) { private readonly EventBusConcurrentDictionary<(Type, bool), InMemorySender> sendersCache = new(); private readonly EventBusConcurrentDictionary processorsCache = new(); - private readonly InMemoryClient client; + private readonly InMemoryClient client = new(sng); - private readonly ConcurrentBag published = new(); - private readonly ConcurrentBag cancelled = new(); - private readonly ConcurrentBag consumed = new(); - private readonly ConcurrentBag failed = new(); - - /// - /// - /// - /// - /// - /// - /// - /// - public InMemoryTransport(IServiceScopeFactory serviceScopeFactory, - IOptions busOptionsAccessor, - IOptionsMonitor optionsMonitor, - ILoggerFactory loggerFactory, - SequenceNumberGenerator sng) - : base(serviceScopeFactory, busOptionsAccessor, optionsMonitor, loggerFactory) - { - client = new InMemoryClient(sng); - } + private readonly ConcurrentBag published = []; + private readonly ConcurrentBag cancelled = []; + private readonly ConcurrentBag consumed = []; + private readonly ConcurrentBag failed = []; /// /// The published events. diff --git a/src/Tingle.EventBus.Transports.InMemory/InMemoryTransportConfigureOptions.cs b/src/Tingle.EventBus.Transports.InMemory/InMemoryTransportConfigureOptions.cs index fcaec591..cf0e4e16 100644 --- a/src/Tingle.EventBus.Transports.InMemory/InMemoryTransportConfigureOptions.cs +++ b/src/Tingle.EventBus.Transports.InMemory/InMemoryTransportConfigureOptions.cs @@ -6,18 +6,14 @@ namespace Microsoft.Extensions.DependencyInjection; /// /// A class to finish the configuration of instances of . /// -internal class InMemoryTransportConfigureOptions : EventBusTransportConfigureOptions +/// An instance.\ +/// A list of to use when configuring options. +/// An for bus configuration.\ +internal class InMemoryTransportConfigureOptions(IEventBusConfigurationProvider configurationProvider, + IEnumerable configurators, + IOptions busOptionsAccessor) + : EventBusTransportConfigureOptions(configurationProvider, configurators, busOptionsAccessor) { - /// - /// Initializes a new given the configuration - /// provided by the . - /// - /// An instance.\ - /// A list of to use when configuring options. - /// An for bus configuration.\ - public InMemoryTransportConfigureOptions(IEventBusConfigurationProvider configurationProvider, IEnumerable configurators, IOptions busOptionsAccessor) - : base(configurationProvider, configurators, busOptionsAccessor) { } - /// public override void PostConfigure(string? name, InMemoryTransportOptions options) { diff --git a/src/Tingle.EventBus.Transports.Kafka/KafkaConfigureOptions.cs b/src/Tingle.EventBus.Transports.Kafka/KafkaConfigureOptions.cs index f6d92241..5f1bf8c5 100644 --- a/src/Tingle.EventBus.Transports.Kafka/KafkaConfigureOptions.cs +++ b/src/Tingle.EventBus.Transports.Kafka/KafkaConfigureOptions.cs @@ -7,18 +7,14 @@ namespace Microsoft.Extensions.DependencyInjection; /// /// A class to finish the configuration of instances of . /// -internal class KafkaConfigureOptions : EventBusTransportConfigureOptions +/// An instance. +/// A list of to use when configuring options. +/// An for bus configuration.\ +internal class KafkaConfigureOptions(IEventBusConfigurationProvider configurationProvider, + IEnumerable configurators, + IOptions busOptionsAccessor) + : EventBusTransportConfigureOptions(configurationProvider, configurators, busOptionsAccessor) { - /// - /// Initializes a new given the configuration - /// provided by the . - /// - /// An instance. - /// A list of to use when configuring options. - /// An for bus configuration.\ - public KafkaConfigureOptions(IEventBusConfigurationProvider configurationProvider, IEnumerable configurators, IOptions busOptionsAccessor) - : base(configurationProvider, configurators, busOptionsAccessor) { } - /// public override void PostConfigure(string? name, KafkaTransportOptions options) { diff --git a/src/Tingle.EventBus.Transports.Kafka/KafkaTransport.cs b/src/Tingle.EventBus.Transports.Kafka/KafkaTransport.cs index 8b81888d..54d3d239 100644 --- a/src/Tingle.EventBus.Transports.Kafka/KafkaTransport.cs +++ b/src/Tingle.EventBus.Transports.Kafka/KafkaTransport.cs @@ -23,7 +23,7 @@ public class KafkaTransport : EventBusTransport, IDisposa private readonly Lazy> producer; // producer instance is thread safe thus can be shared, and across topics private readonly Lazy> consumer; // consumer instance is thread safe thus can be shared, and across topics private readonly CancellationTokenSource stoppingCts = new(); - private readonly List receiverTasks = new(); + private readonly List receiverTasks = []; private readonly Lazy adminClient; private int checkpointingCounter = 0; private bool disposedValue; diff --git a/src/Tingle.EventBus.Transports.RabbitMQ/RabbitMqConfigureOptions.cs b/src/Tingle.EventBus.Transports.RabbitMQ/RabbitMqConfigureOptions.cs index 3ab4b9f1..05732e01 100644 --- a/src/Tingle.EventBus.Transports.RabbitMQ/RabbitMqConfigureOptions.cs +++ b/src/Tingle.EventBus.Transports.RabbitMQ/RabbitMqConfigureOptions.cs @@ -7,18 +7,14 @@ namespace Microsoft.Extensions.DependencyInjection; /// /// A class to finish the configuration of instances of . /// -internal class RabbitMqConfigureOptions : EventBusTransportConfigureOptions +/// An instance. +/// A list of to use when configuring options. +/// An for bus configuration.\ +internal class RabbitMqConfigureOptions(IEventBusConfigurationProvider configurationProvider, + IEnumerable configurators, + IOptions busOptionsAccessor) + : EventBusTransportConfigureOptions(configurationProvider, configurators, busOptionsAccessor) { - /// - /// Initializes a new given the configuration - /// provided by the . - /// - /// An instance. - /// A list of to use when configuring options. - /// An for bus configuration.\ - public RabbitMqConfigureOptions(IEventBusConfigurationProvider configurationProvider, IEnumerable configurators, IOptions busOptionsAccessor) - : base(configurationProvider, configurators, busOptionsAccessor) { } - /// public override void PostConfigure(string? name, RabbitMqTransportOptions options) { diff --git a/src/Tingle.EventBus.Transports.RabbitMQ/RabbitMqTransport.cs b/src/Tingle.EventBus.Transports.RabbitMQ/RabbitMqTransport.cs index 78215ef9..092ddb79 100644 --- a/src/Tingle.EventBus.Transports.RabbitMQ/RabbitMqTransport.cs +++ b/src/Tingle.EventBus.Transports.RabbitMQ/RabbitMqTransport.cs @@ -19,7 +19,15 @@ namespace Tingle.EventBus.Transports.RabbitMQ; /// /// Implementation of using RabbitMQ. /// -public class RabbitMqTransport : EventBusTransport, IDisposable +/// +/// +/// +/// +public class RabbitMqTransport(IServiceScopeFactory serviceScopeFactory, + IOptions busOptionsAccessor, + IOptionsMonitor optionsMonitor, + ILoggerFactory loggerFactory) + : EventBusTransport(serviceScopeFactory, busOptionsAccessor, optionsMonitor, loggerFactory), IDisposable { private readonly SemaphoreSlim connectionLock = new(1, 1); private readonly EventBusConcurrentDictionary subscriptionChannelsCache = new(); @@ -28,19 +36,6 @@ public class RabbitMqTransport : EventBusTransport, ID private IConnection? connection; private bool disposed; - /// - /// - /// - /// - /// - /// - /// - public RabbitMqTransport(IServiceScopeFactory serviceScopeFactory, - IOptions busOptionsAccessor, - IOptionsMonitor optionsMonitor, - ILoggerFactory loggerFactory) - : base(serviceScopeFactory, busOptionsAccessor, optionsMonitor, loggerFactory) { } - /// protected override async Task StartCoreAsync(CancellationToken cancellationToken) { diff --git a/src/Tingle.EventBus/Configuration/EventConsumerRegistration.cs b/src/Tingle.EventBus/Configuration/EventConsumerRegistration.cs index 68e13ec9..d018a4c9 100644 --- a/src/Tingle.EventBus/Configuration/EventConsumerRegistration.cs +++ b/src/Tingle.EventBus/Configuration/EventConsumerRegistration.cs @@ -6,24 +6,15 @@ namespace Tingle.EventBus.Configuration; /// /// Represents a registration for a consumer of an event. /// -public class EventConsumerRegistration : IEquatable +/// The type of consumer handling the event. +/// Whether the consumer should be connected to the dead-letter entity. +public class EventConsumerRegistration(Type consumerType, bool deadletter) : IEquatable { - /// - /// Creates an instance of . - /// - /// The type of consumer handling the event. - /// Whether the consumer should be connected to the dead-letter entity. - public EventConsumerRegistration(Type consumerType, bool deadletter) - { - ConsumerType = consumerType ?? throw new ArgumentNullException(nameof(consumerType)); - Deadletter = deadletter; - } - /// /// The type of consumer handling the event. /// [DynamicallyAccessedMembers(TrimmingHelper.Consumer)] - public Type ConsumerType { get; } + public Type ConsumerType { get; } = consumerType ?? throw new ArgumentNullException(nameof(consumerType)); /// /// Gets or sets a value indicating if the consumer should be connected to the dead-letter entity. @@ -31,7 +22,7 @@ public EventConsumerRegistration(Type consumerType, bool deadletter) /// When set to , you must use /// to consume events. /// - public bool Deadletter { get; } + public bool Deadletter { get; } = deadletter; /// /// The name generated for the consumer. diff --git a/src/Tingle.EventBus/Configuration/EventRegistration.cs b/src/Tingle.EventBus/Configuration/EventRegistration.cs index 8128c819..ff28cea9 100644 --- a/src/Tingle.EventBus/Configuration/EventRegistration.cs +++ b/src/Tingle.EventBus/Configuration/EventRegistration.cs @@ -8,22 +8,14 @@ namespace Tingle.EventBus.Configuration; /// /// Represents a registration for an event. /// -public class EventRegistration : IEquatable +/// The type of event handled. +public class EventRegistration(Type eventType) : IEquatable { - /// - /// Creates an instance of . - /// - /// The type of event handled. - public EventRegistration(Type eventType) - { - EventType = eventType ?? throw new ArgumentNullException(nameof(eventType)); - } - /// /// The type of event handled. /// - [DynamicallyAccessedMembers(TrimmingHelper.Event)] - public Type EventType { get; } + [DynamicallyAccessedMembers(TrimmingHelper.Event)] + public Type EventType { get; } = eventType ?? throw new ArgumentNullException(nameof(eventType)); /// /// The name generated for the event. @@ -85,7 +77,7 @@ public EventRegistration(Type eventType) /// /// This is backed by a to ensure no duplicates. /// - public HashSet Consumers { get; } = new(); + public HashSet Consumers { get; } = []; /// /// Gets a key/value collection that can be used to organize and share data across components diff --git a/src/Tingle.EventBus/Transports/EventBusTransport.cs b/src/Tingle.EventBus/Transports/EventBusTransport.cs index 344a6888..64e24d6f 100644 --- a/src/Tingle.EventBus/Transports/EventBusTransport.cs +++ b/src/Tingle.EventBus/Transports/EventBusTransport.cs @@ -274,13 +274,8 @@ protected async Task DeserializeAsync(IServiceScope scope, ContentType = contentType, RawTransportData = raw, }; - var envelope = await serializer.DeserializeAsync(ctx, cancellationToken).ConfigureAwait(false); - - // Ensure we are not null (throwing helps track the error) - if (envelope is null) - { - throw new InvalidOperationException($"Deserialization from '{typeof(TEvent).Name}' resulted in null which is not allowed."); - } + var envelope = await serializer.DeserializeAsync(ctx, cancellationToken).ConfigureAwait(false) + ?? throw new InvalidOperationException($"Deserialization from '{typeof(TEvent).Name}' resulted in null which is not allowed."); // throwing helps track the error // Create the context var publisher = provider.GetRequiredService(); diff --git a/tests/Tingle.EventBus.Tests/Configurator/MandatoryEventBusConfiguratorTests.cs b/tests/Tingle.EventBus.Tests/Configurator/MandatoryEventBusConfiguratorTests.cs index b1f9d198..1efe0acf 100644 --- a/tests/Tingle.EventBus.Tests/Configurator/MandatoryEventBusConfiguratorTests.cs +++ b/tests/Tingle.EventBus.Tests/Configurator/MandatoryEventBusConfiguratorTests.cs @@ -127,7 +127,7 @@ public void ConfigureEventName_Works(Type eventType, bool useFullTypeNames, stri public void SetConsumerName_Works(Type eventType, Type consumerType, bool useFullTypeNames, - string prefix, + string? prefix, ConsumerNameSource consumerNameSource, NamingConvention namingConvention, string expected) diff --git a/tests/Tingle.EventBus.Transports.Azure.EventHubs.Tests/IotHubEventSerializerTests.cs b/tests/Tingle.EventBus.Transports.Azure.EventHubs.Tests/IotHubEventSerializerTests.cs index b44b4650..420e2b00 100644 --- a/tests/Tingle.EventBus.Transports.Azure.EventHubs.Tests/IotHubEventSerializerTests.cs +++ b/tests/Tingle.EventBus.Transports.Azure.EventHubs.Tests/IotHubEventSerializerTests.cs @@ -13,20 +13,13 @@ namespace Tingle.EventBus.Transports.Azure.EventHubs.Tests; -public class IotHubEventSerializerTests +public class IotHubEventSerializerTests(ITestOutputHelper outputHelper) { private static readonly JsonSerializerOptions serializerOptions = new(JsonSerializerDefaults.Web); private const string DeviceId = "1234567890"; private const string HubName = "iothub-route-test-weu-ih"; - private readonly ITestOutputHelper outputHelper; - - public IotHubEventSerializerTests(ITestOutputHelper outputHelper) - { - this.outputHelper = outputHelper ?? throw new ArgumentNullException(nameof(outputHelper)); - } - private static (EventData, DeserializationContext) CreateData(EventRegistration ereg, BinaryData body, string source, IDictionary? properties = null) { var ed = new EventData(body); diff --git a/tests/Tingle.EventBus.Transports.InMemory.Tests/SampleEventConsumerTests.cs b/tests/Tingle.EventBus.Transports.InMemory.Tests/SampleEventConsumerTests.cs index 9a63a1e8..36d0f6fe 100644 --- a/tests/Tingle.EventBus.Transports.InMemory.Tests/SampleEventConsumerTests.cs +++ b/tests/Tingle.EventBus.Transports.InMemory.Tests/SampleEventConsumerTests.cs @@ -7,15 +7,8 @@ namespace Tingle.EventBus.Tests.InMemory; -public class SampleEventConsumerTests +public class SampleEventConsumerTests(ITestOutputHelper outputHelper) { - private readonly ITestOutputHelper outputHelper; - - public SampleEventConsumerTests(ITestOutputHelper outputHelper) - { - this.outputHelper = outputHelper ?? throw new ArgumentNullException(nameof(outputHelper)); - } - [Fact] public async Task ConsumerWorksAsync() { diff --git a/tests/Tingle.EventBus.Transports.InMemory.Tests/SimplePublisherTests.cs b/tests/Tingle.EventBus.Transports.InMemory.Tests/SimplePublisherTests.cs index 4c20a610..2a479a35 100644 --- a/tests/Tingle.EventBus.Transports.InMemory.Tests/SimplePublisherTests.cs +++ b/tests/Tingle.EventBus.Transports.InMemory.Tests/SimplePublisherTests.cs @@ -58,15 +58,8 @@ public async Task EventIsPublishedOnBusAsync(int orderNumber) class OrderProcessedEvent : SimpleConsumer.SampleEvent { } - class RandomOrderProcessor + class RandomOrderProcessor(IEventPublisher publisher) { - private readonly IEventPublisher publisher; - - public RandomOrderProcessor(IEventPublisher publisher) - { - this.publisher = publisher ?? throw new ArgumentNullException(nameof(publisher)); - } - public async Task ProcessAsync(int orderNumber) { // only publish if the order number is even (can be any other condition)