Skip to content

Commit

Permalink
Cosmetic code changes (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell authored Dec 12, 2023
1 parent 33be58f commit 00e3020
Show file tree
Hide file tree
Showing 53 changed files with 203 additions and 505 deletions.
9 changes: 1 addition & 8 deletions samples/AmazonSqsAndSns/DoorOpenedConsumer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
namespace AmazonSqsAndSns;

public class DoorOpenedConsumer : IEventConsumer<DoorOpened>
public class DoorOpenedConsumer(ILogger<DoorOpenedConsumer> logger) : IEventConsumer<DoorOpened>
{
private readonly ILogger logger;

public DoorOpenedConsumer(ILogger<DoorOpenedConsumer> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public Task ConsumeAsync(EventContext<DoorOpened> context, CancellationToken cancellationToken = default)
{
logger.LogInformation("Received event Id: {Id}", context.Id);
Expand Down
9 changes: 1 addition & 8 deletions samples/AmazonSqsAndSns/PublisherService.cs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
9 changes: 1 addition & 8 deletions samples/AzureIotHub/AzureIotEventsConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@
namespace AzureIotHub;

[ConsumerName("$Default")] // or [ConsumerName(EventHubConsumerClient.DefaultConsumerGroupName)]
internal class AzureIotEventsConsumer : IEventConsumer<MyIotHubEvent>
internal class AzureIotEventsConsumer(ILogger<AzureIotEventsConsumer> logger) : IEventConsumer<MyIotHubEvent>
{
private static readonly JsonSerializerOptions serializerOptions = new(JsonSerializerDefaults.Web) { WriteIndented = true, };

private readonly ILogger logger;

public AzureIotEventsConsumer(ILogger<AzureIotEventsConsumer> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public Task ConsumeAsync(EventContext<MyIotHubEvent> context, CancellationToken cancellationToken)
{
var evt = context.Event;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
namespace AzureManagedIdentity;

internal class VehicleTelemetryEventsConsumer : IEventConsumer<VehicleTelemetryEvent>
internal class VehicleTelemetryEventsConsumer(ILogger<VehicleTelemetryEventsConsumer> logger) : IEventConsumer<VehicleTelemetryEvent>
{
private readonly ILogger logger;

public VehicleTelemetryEventsConsumer(ILogger<VehicleTelemetryEventsConsumer> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public async Task ConsumeAsync(EventContext<VehicleTelemetryEvent> context, CancellationToken cancellationToken)
{
var telemetry = context.Event;
Expand Down
9 changes: 1 addition & 8 deletions samples/ConfigSample/VehicleTelemetryEventsConsumer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
namespace ConfigSample;

internal class VehicleTelemetryEventsConsumer : IEventConsumer<VehicleTelemetryEvent>
internal class VehicleTelemetryEventsConsumer(ILogger<VehicleTelemetryEventsConsumer> logger) : IEventConsumer<VehicleTelemetryEvent>
{
private readonly ILogger logger;

public VehicleTelemetryEventsConsumer(ILogger<VehicleTelemetryEventsConsumer> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public async Task ConsumeAsync(EventContext<VehicleTelemetryEvent> context, CancellationToken cancellationToken)
{
var telemetry = context.Event;
Expand Down
11 changes: 1 addition & 10 deletions samples/ConfigSample/VisualsProducerService.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
namespace ConfigSample;

internal class VisualsProducerService : BackgroundService
internal class VisualsProducerService(IEventPublisher publisher, ILogger<VisualsProducerService> logger) : BackgroundService
{
private readonly IEventPublisher publisher;
private readonly ILogger logger;

public VisualsProducerService(IEventPublisher publisher, ILogger<VisualsProducerService> 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)
Expand Down
9 changes: 1 addition & 8 deletions samples/ConfigSample/VisualsUploadedConsumer.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
namespace ConfigSample;

internal class VisualsUploadedConsumer : IEventConsumer<ImageUploaded>, IEventConsumer<VideoUploaded>
internal class VisualsUploadedConsumer(ILogger<VisualsUploadedConsumer> logger) : IEventConsumer<ImageUploaded>, IEventConsumer<VideoUploaded>
{
private static readonly TimeSpan SimulationDuration = TimeSpan.FromSeconds(1.3f);

private readonly ILogger logger;

public VisualsUploadedConsumer(ILogger<VisualsUploadedConsumer> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public async Task ConsumeAsync(EventContext<ImageUploaded> context, CancellationToken cancellationToken)
{
var id = context.Event.ImageId;
Expand Down
9 changes: 1 addition & 8 deletions samples/CustomEventConfigurator/SampleConsumer1.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
namespace CustomEventConfigurator;

public class SampleConsumer1 : IEventConsumer<SampleEvent1>
public class SampleConsumer1(ILogger<SampleConsumer1> logger) : IEventConsumer<SampleEvent1>
{
private readonly ILogger logger;

public SampleConsumer1(ILogger<SampleConsumer1> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public Task ConsumeAsync(EventContext<SampleEvent1> context, CancellationToken cancellationToken = default)
{
logger.LogInformation("Received event Id: {Id}", context.Id);
Expand Down
9 changes: 1 addition & 8 deletions samples/CustomEventConfigurator/SampleConsumer2.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
namespace CustomEventConfigurator;

public class SampleConsumer2 : IEventConsumer<SampleEvent2>
public class SampleConsumer2(ILogger<SampleConsumer2> logger) : IEventConsumer<SampleEvent2>
{
private readonly ILogger logger;

public SampleConsumer2(ILogger<SampleConsumer2> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public Task ConsumeAsync(EventContext<SampleEvent2> context, CancellationToken cancellationToken = default)
{
logger.LogInformation("Received event Id: {Id}", context.Id);
Expand Down
6 changes: 1 addition & 5 deletions samples/CustomEventSerializer/AzureDevOpsEventSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@

namespace CustomEventSerializer;

public class AzureDevOpsEventSerializer : AbstractEventSerializer
public class AzureDevOpsEventSerializer(IOptionsMonitor<EventBusSerializationOptions> optionsAccessor, ILoggerFactory loggerFactory) : AbstractEventSerializer(optionsAccessor, loggerFactory)
{
private readonly JsonSerializer serializer = JsonSerializer.CreateDefault();

public AzureDevOpsEventSerializer(IOptionsMonitor<EventBusSerializationOptions> optionsAccessor,
ILoggerFactory loggerFactory)
: base(optionsAccessor, loggerFactory) { }

/// <inheritdoc/>
protected override IList<string> SupportedMediaTypes => JsonContentTypes;

Expand Down
11 changes: 2 additions & 9 deletions samples/CustomEventSerializer/AzureDevOpsEventsConsumer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
namespace CustomEventSerializer;

internal class AzureDevOpsEventsConsumer : IEventConsumer<AzureDevOpsCodePushed>
internal class AzureDevOpsEventsConsumer(ILogger<AzureDevOpsEventsConsumer> logger) : IEventConsumer<AzureDevOpsCodePushed>
{
private readonly ILogger logger;

public AzureDevOpsEventsConsumer(ILogger<AzureDevOpsEventsConsumer> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public Task ConsumeAsync(EventContext<AzureDevOpsCodePushed> context, CancellationToken cancellationToken = default)
{
var @event = context.Event;
Expand All @@ -17,7 +10,7 @@ public Task ConsumeAsync(EventContext<AzureDevOpsCodePushed> context, Cancellati
var defaultBranch = repository?.DefaultBranch;

// get the updated branches (refs)
var updatedReferences = resource?.RefUpdates?.Select(ru => ru.Name).ToList() ?? new List<string?>();
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));
Expand Down
18 changes: 2 additions & 16 deletions samples/InMemoryBackgroundProcessing/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -54,17 +47,10 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
}
}

class VideoUploadedConsumer : IEventConsumer<VideoUploaded>
class VideoUploadedConsumer(ILogger<VideoUploadedConsumer> logger) : IEventConsumer<VideoUploaded>
{
private static readonly TimeSpan SimulationDuration = TimeSpan.FromSeconds(3);

private readonly ILogger logger;

public VideoUploadedConsumer(ILogger<VideoUploadedConsumer> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public async Task ConsumeAsync(EventContext<VideoUploaded> context, CancellationToken cancellationToken = default)
{
var evt = context.Event;
Expand Down
16 changes: 5 additions & 11 deletions samples/MultiEventsConsumer/DummyProducerService.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
namespace MultiEventsConsumer;

internal class DummyProducerService : BackgroundService
internal partial class DummyProducerService(IEventPublisher publisher, ILogger<DummyProducerService> logger) : BackgroundService
{
private readonly IEventPublisher publisher;
private readonly ILogger logger;

public DummyProducerService(IEventPublisher publisher, ILogger<DummyProducerService> 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
Expand Down Expand Up @@ -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();
}
11 changes: 1 addition & 10 deletions samples/MultiEventsConsumer/MultiEventsConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@

namespace MultiEventsConsumer;

public class MultiEventsConsumer : IEventConsumer<DoorClosed>, IEventConsumer<DoorOpened>
public class MultiEventsConsumer(IDistributedCache cache, ILogger<MultiEventsConsumer> logger) : IEventConsumer<DoorClosed>, IEventConsumer<DoorOpened>
{
private static readonly TimeSpan SimulationDuration = TimeSpan.FromSeconds(3);

private readonly IDistributedCache cache;
private readonly ILogger logger;

public MultiEventsConsumer(IDistributedCache cache, ILogger<MultiEventsConsumer> logger)
{
this.cache = cache ?? throw new ArgumentNullException(nameof(cache));
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public async Task ConsumeAsync(EventContext<DoorOpened> context, CancellationToken cancellationToken = default)
{
var evt = context.Event;
Expand Down
9 changes: 1 addition & 8 deletions samples/MultipleConsumers/FirstEventConsumer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
namespace MultipleConsumers;

public class FirstEventConsumer : IEventConsumer<DoorOpened>
public class FirstEventConsumer(ILogger<FirstEventConsumer> logger) : IEventConsumer<DoorOpened>
{
private readonly ILogger logger;

public FirstEventConsumer(ILogger<FirstEventConsumer> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public Task ConsumeAsync(EventContext<DoorOpened> context, CancellationToken cancellationToken = default)
{
var evt = context.Event;
Expand Down
9 changes: 1 addition & 8 deletions samples/MultipleConsumers/PublisherService.cs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
9 changes: 1 addition & 8 deletions samples/MultipleConsumers/SecondEventConsumer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
namespace MultipleConsumers;

public class SecondEventConsumer : IEventConsumer<DoorOpened>
public class SecondEventConsumer(ILogger<SecondEventConsumer> logger) : IEventConsumer<DoorOpened>
{
private readonly ILogger logger;

public SecondEventConsumer(ILogger<SecondEventConsumer> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public Task ConsumeAsync(EventContext<DoorOpened> context, CancellationToken cancellationToken = default)
{
var evt = context.Event;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@
namespace MultipleDifferentTransports;

[ConsumerName("$Default")] // or [ConsumerName(EventHubConsumerClient.DefaultConsumerGroupName)]
internal class VehicleTelemetryEventsConsumer : IEventConsumer<VehicleTelemetryEvent>
internal class VehicleTelemetryEventsConsumer(ILogger<VehicleTelemetryEventsConsumer> logger) : IEventConsumer<VehicleTelemetryEvent>
{
private readonly ILogger logger;

public VehicleTelemetryEventsConsumer(ILogger<VehicleTelemetryEventsConsumer> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public async Task ConsumeAsync(EventContext<VehicleTelemetryEvent> context, CancellationToken cancellationToken)
{
var evt = context.Event;
Expand Down
20 changes: 2 additions & 18 deletions samples/MultipleSimilarTransports/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,8 @@

await host.RunAsync();

class VisualsProducerService : BackgroundService
class VisualsProducerService(IEventPublisher publisher, ILogger<VisualsProducerService> logger) : BackgroundService
{
private readonly IEventPublisher publisher;
private readonly ILogger logger;

public VisualsProducerService(IEventPublisher publisher, ILogger<VisualsProducerService> 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)
Expand Down Expand Up @@ -62,17 +53,10 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
=> await publisher.PublishAsync(@event, cancellationToken: cancellationToken);
}

class VisualsUploadedConsumer : IEventConsumer<ImageUploaded>, IEventConsumer<VideoUploaded>
class VisualsUploadedConsumer(ILogger<VisualsUploadedConsumer> logger) : IEventConsumer<ImageUploaded>, IEventConsumer<VideoUploaded>
{
private static readonly TimeSpan SimulationDuration = TimeSpan.FromSeconds(1.3f);

private readonly ILogger logger;

public VisualsUploadedConsumer(ILogger<VisualsUploadedConsumer> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public async Task ConsumeAsync(EventContext<ImageUploaded> context, CancellationToken cancellationToken)
{
var id = context.Event.ImageId;
Expand Down
11 changes: 1 addition & 10 deletions samples/SimpleConsumer/SampleEventConsumer.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
namespace SimpleConsumer;

public class SampleEventConsumer : IEventConsumer<SampleEvent>
public class SampleEventConsumer(EventCounter counter, ILogger<SampleEventConsumer> logger) : IEventConsumer<SampleEvent>
{
private readonly EventCounter counter;
private readonly ILogger logger;

public SampleEventConsumer(EventCounter counter, ILogger<SampleEventConsumer> logger)
{
this.counter = counter ?? throw new ArgumentNullException(nameof(counter));
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public Task ConsumeAsync(EventContext<SampleEvent> context, CancellationToken cancellationToken = default)
{
logger.LogInformation("Received event Id: {Id}", context.Id);
Expand Down
Loading

0 comments on commit 00e3020

Please sign in to comment.