From a28a3e6126c2dda55a3a56d78b3f4c938160a846 Mon Sep 17 00:00:00 2001 From: Oskar Dudycz Date: Sat, 5 Mar 2022 16:13:22 +0100 Subject: [PATCH] Replaced MediatR event bus with the custom one to enable scoping, tracing etc. Plugged that fully into Marten and EventStoreDB --- .../Projections/ElasticSearchProjection.cs | 5 +- .../Repository/ElasticSearchRepository.cs | 5 +- ...tStoreDBEventMetadataJsonConverterTests.cs | 2 +- Core.EventStoreDB/Config.cs | 2 - .../EventStoreDBEventMetadataJsonConverter.cs | 2 +- .../EventStoreDBSubscriptionToAll.cs | 5 +- Core.Marten/Config.cs | 6 +- .../MartenExternalProjection.cs | 13 +-- .../Subscriptions/MartenSubscription.cs | 21 ++++- Core.Testing/ApiFixture.cs | 4 +- Core.Testing/EventListener.cs | 7 +- Core.WebApi/Tracing/TracingMiddleware.cs | 3 - Core/Config.cs | 4 +- Core/Events/Config.cs | 7 +- Core/Events/EventBus.cs | 79 ++++++++++++++---- Core/Events/IEventHandler.cs | 6 +- Core/Events/Mediator/IMediatorEventHandler.cs | 23 +++++ Core/Events/Mediator/MediatorEventBus.cs | 41 +++++++++ Core/Events/NoMediator/EventBus.cs | 83 ------------------- .../NoMediator/INoMediatorEventHandler.cs | 6 -- .../Orders/Orders/Orders/OrderSaga.cs | 2 +- .../ClassesWithBuilderTest.cs | 2 +- .../ClassesWithIoCTest.cs | 2 +- .../ClassesWithMediatRTest.cs | 2 +- .../EventPipelines/EventPipelines/Builder.cs | 2 +- .../EventPipelines/Configuration.cs | 2 +- .../EventPipelines/EventPipelines/EventBus.cs | 2 +- .../Carts/Carts/ShoppingCarts/Config.cs | 9 -- .../Simple/ECommerce.Core/Configuration.cs | 2 +- .../Projections/EntityFrameworkProjection.cs | 9 +- .../Notifications/Config.cs | 4 +- .../NotifyingByEvent/EmailNotifier.cs | 2 +- 32 files changed, 188 insertions(+), 176 deletions(-) create mode 100644 Core/Events/Mediator/IMediatorEventHandler.cs create mode 100644 Core/Events/Mediator/MediatorEventBus.cs delete mode 100644 Core/Events/NoMediator/EventBus.cs delete mode 100644 Core/Events/NoMediator/INoMediatorEventHandler.cs diff --git a/Core.ElasticSearch/Projections/ElasticSearchProjection.cs b/Core.ElasticSearch/Projections/ElasticSearchProjection.cs index 2c24834f3..0e0b011ed 100644 --- a/Core.ElasticSearch/Projections/ElasticSearchProjection.cs +++ b/Core.ElasticSearch/Projections/ElasticSearchProjection.cs @@ -1,6 +1,5 @@ using Core.ElasticSearch.Indices; using Core.Events; -using Core.Events.NoMediator; using Core.Projections; using Elasticsearch.Net; using Microsoft.Extensions.DependencyInjection; @@ -8,7 +7,7 @@ namespace Core.ElasticSearch.Projections; -public class ElasticSearchProjection : INoMediatorEventHandler> +public class ElasticSearchProjection : IEventHandler> where TView : class, IProjection where TEvent : IEvent { @@ -49,7 +48,7 @@ public static IServiceCollection Project(this IServiceCollection where TView : class, IProjection where TEvent : IEvent { - services.AddTransient>>(sp => + services.AddTransient>>(sp => { var session = sp.GetRequiredService(); diff --git a/Core.ElasticSearch/Repository/ElasticSearchRepository.cs b/Core.ElasticSearch/Repository/ElasticSearchRepository.cs index 96b769d01..f4c33af6f 100644 --- a/Core.ElasticSearch/Repository/ElasticSearchRepository.cs +++ b/Core.ElasticSearch/Repository/ElasticSearchRepository.cs @@ -17,15 +17,12 @@ namespace Core.ElasticSearch.Repository; public class ElasticSearchRepository: IElasticSearchRepository where T : class, IAggregate, new() { private readonly IElasticClient elasticClient; - private readonly IEventBus eventBus; public ElasticSearchRepository( - IElasticClient elasticClient, - IEventBus eventBus + IElasticClient elasticClient ) { this.elasticClient = elasticClient ?? throw new ArgumentNullException(nameof(elasticClient)); - this.eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus)); } public async Task Find(Guid id, CancellationToken cancellationToken) diff --git a/Core.EventStoreDB.Tests/Events/EventStoreDBEventMetadataJsonConverterTests.cs b/Core.EventStoreDB.Tests/Events/EventStoreDBEventMetadataJsonConverterTests.cs index d0de95845..b444fa7ea 100644 --- a/Core.EventStoreDB.Tests/Events/EventStoreDBEventMetadataJsonConverterTests.cs +++ b/Core.EventStoreDB.Tests/Events/EventStoreDBEventMetadataJsonConverterTests.cs @@ -39,7 +39,7 @@ public void DeSerialize_ForNonEmptyEventMetadata_ShouldSucceed() var json = $"{{\"$correlationId\":\"{correlationId.Value}\",\"$causationId\":\"{causationId.Value}\"}}"; // When - var eventMetadata = JsonConvert.DeserializeObject(json, jsonConverter); + var eventMetadata = JsonConvert.DeserializeObject(json, jsonConverter); eventMetadata.Should().Be(expectedEventMetadata); diff --git a/Core.EventStoreDB/Config.cs b/Core.EventStoreDB/Config.cs index e5c5df7ad..fd8317915 100644 --- a/Core.EventStoreDB/Config.cs +++ b/Core.EventStoreDB/Config.cs @@ -1,5 +1,4 @@ using Core.BackgroundWorkers; -using Core.Events.NoMediator; using Core.EventStoreDB.OptimisticConcurrency; using Core.EventStoreDB.Subscriptions; using EventStore.Client; @@ -27,7 +26,6 @@ public static IServiceCollection AddEventStoreDB(this IServiceCollection service var eventStoreDBConfig = config.GetSection(DefaultConfigKey).Get(); services - .AddEventBus() .AddSingleton(new EventStoreClient(EventStoreClientSettings.Create(eventStoreDBConfig.ConnectionString))) .AddEventStoreDBAppendScope() .AddTransient(); diff --git a/Core.EventStoreDB/Events/EventStoreDBEventMetadataJsonConverter.cs b/Core.EventStoreDB/Events/EventStoreDBEventMetadataJsonConverter.cs index 558629e5b..cdf2525c3 100644 --- a/Core.EventStoreDB/Events/EventStoreDBEventMetadataJsonConverter.cs +++ b/Core.EventStoreDB/Events/EventStoreDBEventMetadataJsonConverter.cs @@ -13,7 +13,7 @@ public class EventStoreDBEventMetadataJsonConverter : JsonConverter public override bool CanConvert(Type objectType) { - return objectType == typeof(EventMetadata); + return objectType == typeof(TraceMetadata); } public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) diff --git a/Core.EventStoreDB/Subscriptions/EventStoreDBSubscriptionToAll.cs b/Core.EventStoreDB/Subscriptions/EventStoreDBSubscriptionToAll.cs index cf5d7f620..2df38691b 100644 --- a/Core.EventStoreDB/Subscriptions/EventStoreDBSubscriptionToAll.cs +++ b/Core.EventStoreDB/Subscriptions/EventStoreDBSubscriptionToAll.cs @@ -1,5 +1,4 @@ using Core.Events; -using Core.Events.NoMediator; using Core.EventStoreDB.Events; using Core.Threading; using EventStore.Client; @@ -23,7 +22,7 @@ public class EventStoreDBSubscriptionToAllOptions public class EventStoreDBSubscriptionToAll { - private readonly INoMediatorEventBus eventBus; + private readonly IEventBus eventBus; private readonly EventStoreClient eventStoreClient; private readonly ISubscriptionCheckpointRepository checkpointRepository; private readonly ILogger logger; @@ -34,7 +33,7 @@ public class EventStoreDBSubscriptionToAll public EventStoreDBSubscriptionToAll( EventStoreClient eventStoreClient, - INoMediatorEventBus eventBus, + IEventBus eventBus, ISubscriptionCheckpointRepository checkpointRepository, ILogger logger ) diff --git a/Core.Marten/Config.cs b/Core.Marten/Config.cs index 5f1e30f5a..4979f1b76 100644 --- a/Core.Marten/Config.cs +++ b/Core.Marten/Config.cs @@ -7,6 +7,7 @@ using Marten.Events.Projections; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using Weasel.Core; namespace Core.Marten; @@ -71,7 +72,10 @@ private static StoreOptions SetStoreOptions( ); options.Projections.Add( - new MartenSubscription(new[] { new MartenEventPublisher(serviceProvider) }), + new MartenSubscription( + new[] { new MartenEventPublisher(serviceProvider) }, + serviceProvider.GetRequiredService>() + ), ProjectionLifecycle.Async, "MartenSubscription" ); diff --git a/Core.Marten/ExternalProjections/MartenExternalProjection.cs b/Core.Marten/ExternalProjections/MartenExternalProjection.cs index 335ed92ea..1c20f1461 100644 --- a/Core.Marten/ExternalProjections/MartenExternalProjection.cs +++ b/Core.Marten/ExternalProjections/MartenExternalProjection.cs @@ -1,12 +1,11 @@ using Core.Events; -using Core.Events.NoMediator; using Core.Projections; using Marten; using Microsoft.Extensions.DependencyInjection; namespace Core.Marten.ExternalProjections; -public class MartenExternalProjection: INoMediatorEventHandler> +public class MartenExternalProjection: IEventHandler> where TView : IVersionedProjection where TEvent : notnull { @@ -24,15 +23,17 @@ Func getId public async Task Handle(EventEnvelope eventEnvelope, CancellationToken ct) { - var entity = await session.LoadAsync(getId(eventEnvelope.Data), ct) ?? + var (@event, eventMetadata) = eventEnvelope; + + var entity = await session.LoadAsync(getId(@event), ct) ?? (TView)Activator.CreateInstance(typeof(TView), true)!; - var eventLogPosition = eventEnvelope.Metadata.LogPosition; + var eventLogPosition = eventMetadata.LogPosition; if (entity.LastProcessedPosition >= eventLogPosition) return; - entity.When(eventEnvelope.Data); + entity.When(@event); entity.LastProcessedPosition = eventLogPosition; @@ -59,7 +60,7 @@ public static IServiceCollection Project(this IServiceCollection where TEvent : notnull { services - .AddTransient>>(sp => + .AddTransient>>(sp => { var session = sp.GetRequiredService(); diff --git a/Core.Marten/Subscriptions/MartenSubscription.cs b/Core.Marten/Subscriptions/MartenSubscription.cs index 38f5d85e0..2d4c529fa 100644 --- a/Core.Marten/Subscriptions/MartenSubscription.cs +++ b/Core.Marten/Subscriptions/MartenSubscription.cs @@ -1,16 +1,22 @@ using Marten; using Marten.Events; using Marten.Events.Projections; +using Microsoft.Extensions.Logging; namespace Core.Marten.Subscriptions; public class MartenSubscription: IProjection { private readonly IEnumerable consumers; + private readonly ILogger logger; - public MartenSubscription(IEnumerable consumers) + public MartenSubscription( + IEnumerable consumers, + ILogger logger + ) { this.consumers = consumers; + this.logger = logger; } public void Apply( @@ -25,14 +31,21 @@ public async Task ApplyAsync( CancellationToken ct ) { - foreach (var consumer in consumers) + try + { + foreach (var consumer in consumers) + { + await consumer.ConsumeAsync(operations, streams, ct); + } + } + catch (Exception exc) { - await consumer.ConsumeAsync(operations, streams, ct); + logger.LogError("Error while processing Marten Subscription: {ExceptionMessage}", exc.Message); + throw; } } } - public interface IMartenEventsConsumer { Task ConsumeAsync( diff --git a/Core.Testing/ApiFixture.cs b/Core.Testing/ApiFixture.cs index 6a52c6f7e..c0e06f40d 100644 --- a/Core.Testing/ApiFixture.cs +++ b/Core.Testing/ApiFixture.cs @@ -5,9 +5,7 @@ using Core.Events.External; using Core.Requests; using FluentAssertions; -using MediatR; using Microsoft.Extensions.DependencyInjection; -using IEventBus = Core.Events.IEventBus; namespace Core.Testing; @@ -22,7 +20,7 @@ public override TestContext CreateTestContext() => { SetupServices?.Invoke(services); services.AddSingleton(eventsLog); - services.AddSingleton(typeof(INotificationHandler<>), typeof(EventListener<>)); + services.AddSingleton(typeof(IEventHandler<>), typeof(EventListener<>)); services.AddSingleton(externalEventProducer); services.AddSingleton(externalCommandBus); services.AddSingleton(); diff --git a/Core.Testing/EventListener.cs b/Core.Testing/EventListener.cs index f08321a88..d7faf36ad 100644 --- a/Core.Testing/EventListener.cs +++ b/Core.Testing/EventListener.cs @@ -1,14 +1,13 @@ using Core.Events; -using MediatR; namespace Core.Testing; public class EventsLog { - public List PublishedEvents { get; } = new List(); + public List PublishedEvents { get; } = new(); } -public class EventListener: INotificationHandler +public class EventListener: IEventHandler where TEvent : IEvent { private readonly EventsLog eventsLog; @@ -24,4 +23,4 @@ public Task Handle(TEvent @event, CancellationToken cancellationToken) return Task.CompletedTask; } -} \ No newline at end of file +} diff --git a/Core.WebApi/Tracing/TracingMiddleware.cs b/Core.WebApi/Tracing/TracingMiddleware.cs index d6bb5cd10..9e23e690f 100644 --- a/Core.WebApi/Tracing/TracingMiddleware.cs +++ b/Core.WebApi/Tracing/TracingMiddleware.cs @@ -1,10 +1,7 @@ using Core.Tracing; -using Core.Tracing.Causation; -using Core.Tracing.Correlation; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging; namespace Core.WebApi.Tracing; diff --git a/Core/Config.cs b/Core/Config.cs index 4cc91bc48..88aa14d59 100644 --- a/Core/Config.cs +++ b/Core/Config.cs @@ -20,9 +20,9 @@ public static IServiceCollection AddCoreServices(this IServiceCollection service services.AddMediatR() .AddScoped() .AddScoped() - .AddTracing(); + .AddTracing() + .AddEventBus(); - services.TryAddScoped(); services.TryAddScoped(); services.TryAddScoped(); diff --git a/Core/Events/Config.cs b/Core/Events/Config.cs index 502cc0eac..2080bf166 100644 --- a/Core/Events/Config.cs +++ b/Core/Events/Config.cs @@ -1,5 +1,4 @@ -using MediatR; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; namespace Core.Events; @@ -11,8 +10,8 @@ this IServiceCollection services where TEvent : IEvent where TEventHandler : class, IEventHandler { - return services.AddTransient() - .AddTransient>(sp => sp.GetRequiredService()) + return services + .AddTransient() .AddTransient>(sp => sp.GetRequiredService()); } } diff --git a/Core/Events/EventBus.cs b/Core/Events/EventBus.cs index 1e88706a9..35baf3a11 100644 --- a/Core/Events/EventBus.cs +++ b/Core/Events/EventBus.cs @@ -1,41 +1,86 @@ -using Core.Events.External; -using MediatR; +using System.Collections.Concurrent; +using System.Reflection; +using Core.Tracing; +using Microsoft.Extensions.DependencyInjection; +using Polly; namespace Core.Events; public interface IEventBus { - Task Publish(IEvent @event, CancellationToken ct); - Task Publish(IEvent[] events, CancellationToken ct); + Task Publish(object @event, CancellationToken ct); } public class EventBus: IEventBus { - private readonly IMediator mediator; - private readonly IExternalEventProducer externalEventProducer; + private readonly IServiceProvider serviceProvider; + private readonly Func createTracingScope; + private readonly AsyncPolicy retryPolicy; + private static readonly ConcurrentDictionary PublishMethods = new(); public EventBus( - IMediator mediator, - IExternalEventProducer externalEventProducer + IServiceProvider serviceProvider, + Func createTracingScope, + AsyncPolicy retryPolicy ) { - this.mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); - this.externalEventProducer = externalEventProducer?? throw new ArgumentNullException(nameof(externalEventProducer)); + this.serviceProvider = serviceProvider; + this.createTracingScope = createTracingScope; + this.retryPolicy = retryPolicy; } - public async Task Publish(IEvent[] events, CancellationToken ct) + private async Task Publish(TEvent @event, CancellationToken ct) { - foreach (var @event in events) + var eventEnvelope = @event as EventEnvelope; + // You can consider adding here a retry policy for event handling + using var scope = serviceProvider.CreateScope(); + using var tracingScope = createTracingScope(serviceProvider, eventEnvelope); + + var eventHandlers = + scope.ServiceProvider.GetServices>(); + + foreach (var eventHandler in eventHandlers) { - await Publish(@event, ct); + await retryPolicy.ExecuteAsync(async token => + { + await eventHandler.Handle(@event, token); + }, ct); } } - public async Task Publish(IEvent @event, CancellationToken ct) + public async Task Publish(object @event, CancellationToken ct) + { + // if it's an event envelope, publish also just event data + // thanks to that both handlers with envelope and without will be called + if (@event is EventEnvelope(var data, _)) + await (Task)GetGenericPublishFor(data) + .Invoke(this, new[] { data, ct })!; + + await (Task)GetGenericPublishFor(@event) + .Invoke(this, new[] { @event, ct })!; + } + + private static MethodInfo GetGenericPublishFor(object @event) => + PublishMethods.GetOrAdd(@event.GetType(), eventType => + typeof(EventBus) + .GetMethods(BindingFlags.Instance | BindingFlags.NonPublic) + .Single(m => m.Name == nameof(Publish) && m.GetGenericArguments().Any()) + .MakeGenericMethod(eventType) + ); +} + +public static class EventBusExtensions +{ + public static IServiceCollection AddEventBus(this IServiceCollection services, AsyncPolicy? asyncPolicy = null) { - await mediator.Publish(@event, ct); + services.AddScoped(sp => + new EventBus( + sp, + sp.GetRequiredService().CreateTraceScope, + asyncPolicy ?? Policy.NoOpAsync() + ) + ); - if (@event is IExternalEvent externalEvent) - await externalEventProducer.Publish(externalEvent, ct); + return services; } } diff --git a/Core/Events/IEventHandler.cs b/Core/Events/IEventHandler.cs index 4fc218e03..a1e51574d 100644 --- a/Core/Events/IEventHandler.cs +++ b/Core/Events/IEventHandler.cs @@ -1,8 +1,6 @@ -using MediatR; - namespace Core.Events; -public interface IEventHandler: INotificationHandler - where TEvent : IEvent +public interface IEventHandler { + Task Handle(TEvent @event, CancellationToken ct); } diff --git a/Core/Events/Mediator/IMediatorEventHandler.cs b/Core/Events/Mediator/IMediatorEventHandler.cs new file mode 100644 index 000000000..0a01dcb17 --- /dev/null +++ b/Core/Events/Mediator/IMediatorEventHandler.cs @@ -0,0 +1,23 @@ +using MediatR; +using Microsoft.Extensions.DependencyInjection; + +namespace Core.Events.Mediator; + +public interface IMediatorEventHandler: INotificationHandler + where TEvent : IEvent +{ +} + +public static class MediatorEventHandlerExtensions +{ + public static IServiceCollection AddMediatorEventHandler( + this IServiceCollection services + ) + where TEvent : IEvent + where TEventHandler : class, IMediatorEventHandler + { + return services.AddTransient() + .AddTransient>(sp => sp.GetRequiredService()) + .AddTransient>(sp => sp.GetRequiredService()); + } +} diff --git a/Core/Events/Mediator/MediatorEventBus.cs b/Core/Events/Mediator/MediatorEventBus.cs new file mode 100644 index 000000000..2ca06fca8 --- /dev/null +++ b/Core/Events/Mediator/MediatorEventBus.cs @@ -0,0 +1,41 @@ +using Core.Events.External; +using MediatR; + +namespace Core.Events.Mediator; + +public interface IEventBus +{ + Task Publish(IEvent @event, CancellationToken ct); + Task Publish(IEvent[] events, CancellationToken ct); +} + +public class MediatorEventBus: IEventBus +{ + private readonly IMediator mediator; + private readonly IExternalEventProducer externalEventProducer; + + public MediatorEventBus( + IMediator mediator, + IExternalEventProducer externalEventProducer + ) + { + this.mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); + this.externalEventProducer = externalEventProducer?? throw new ArgumentNullException(nameof(externalEventProducer)); + } + + public async Task Publish(IEvent[] events, CancellationToken ct) + { + foreach (var @event in events) + { + await Publish(@event, ct); + } + } + + public async Task Publish(IEvent @event, CancellationToken ct) + { + await mediator.Publish(@event, ct); + + if (@event is IExternalEvent externalEvent) + await externalEventProducer.Publish(externalEvent, ct); + } +} diff --git a/Core/Events/NoMediator/EventBus.cs b/Core/Events/NoMediator/EventBus.cs deleted file mode 100644 index 98ce908e8..000000000 --- a/Core/Events/NoMediator/EventBus.cs +++ /dev/null @@ -1,83 +0,0 @@ -using System.Collections.Concurrent; -using System.Reflection; -using Core.Events.External; -using Core.Tracing; -using Microsoft.Extensions.DependencyInjection; -using Polly; - -namespace Core.Events.NoMediator; - -public interface INoMediatorEventBus -{ - Task Publish(object @event, CancellationToken ct); -} - -public class NoMediatorEventBus: INoMediatorEventBus -{ - private readonly IServiceProvider serviceProvider; - private readonly Func createTracingScope; - private readonly AsyncPolicy retryPolicy; - private static readonly ConcurrentDictionary PublishMethods = new(); - - public NoMediatorEventBus( - IServiceProvider serviceProvider, - Func createTracingScope, - AsyncPolicy retryPolicy - ) - { - this.serviceProvider = serviceProvider; - this.createTracingScope = createTracingScope; - this.retryPolicy = retryPolicy; - } - - private async Task Publish(TEvent @event, CancellationToken ct) - { - var eventEnvelope = @event as EventEnvelope; - // You can consider adding here a retry policy for event handling - using var scope = serviceProvider.CreateScope(); - using var tracingScope = createTracingScope(serviceProvider, eventEnvelope); - - var eventHandlers = - scope.ServiceProvider.GetServices>(); - - foreach (var eventHandler in eventHandlers) - { - await retryPolicy.ExecuteAsync(async token => - { - await eventHandler.Handle(@event, token); - }, ct); - } - } - - public async Task Publish(object @event, CancellationToken ct) - { - // if it's an event envelope, publish also just event data - // thanks to that both handlers with envelope and without will be called - if (@event is EventEnvelope(var data, _)) - await (Task)GetGenericPublishFor(data) - .Invoke(this, new[] { data, ct })!; - - await (Task)GetGenericPublishFor(@event) - .Invoke(this, new[] { @event, ct })!; - } - - private static MethodInfo GetGenericPublishFor(object @event) => - PublishMethods.GetOrAdd(@event.GetType(), eventType => - typeof(NoMediatorEventBus) - .GetMethods(BindingFlags.Instance | BindingFlags.NonPublic) - .Single(m => m.Name == nameof(Publish) && m.GetGenericArguments().Any()) - .MakeGenericMethod(eventType) - ); -} - -public static class EventBusExtensions -{ - public static IServiceCollection AddEventBus(this IServiceCollection services, AsyncPolicy? asyncPolicy = null) => - services.AddSingleton(sp => - new NoMediatorEventBus( - sp, - sp.GetRequiredService().CreateTraceScope, - asyncPolicy ?? Policy.NoOpAsync() - ) - ); -} diff --git a/Core/Events/NoMediator/INoMediatorEventHandler.cs b/Core/Events/NoMediator/INoMediatorEventHandler.cs deleted file mode 100644 index 8896da107..000000000 --- a/Core/Events/NoMediator/INoMediatorEventHandler.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Core.Events.NoMediator; - -public interface INoMediatorEventHandler -{ - Task Handle(TEvent @event, CancellationToken ct); -} diff --git a/Sample/ECommerce/Orders/Orders/Orders/OrderSaga.cs b/Sample/ECommerce/Orders/Orders/Orders/OrderSaga.cs index 50a7ec985..aa043bcd9 100644 --- a/Sample/ECommerce/Orders/Orders/Orders/OrderSaga.cs +++ b/Sample/ECommerce/Orders/Orders/Orders/OrderSaga.cs @@ -79,4 +79,4 @@ public Task Handle(OrderCancelled @event, CancellationToken cancellationToken) return commandBus.Send(DiscardPayment.Create(@event.PaymentId.Value)); } -} \ No newline at end of file +} diff --git a/Sample/EventPipelines/EventPipelines.Tests/ClassesWithBuilderTest.cs b/Sample/EventPipelines/EventPipelines.Tests/ClassesWithBuilderTest.cs index ba6f803ad..f366ece2f 100644 --- a/Sample/EventPipelines/EventPipelines.Tests/ClassesWithBuilderTest.cs +++ b/Sample/EventPipelines/EventPipelines.Tests/ClassesWithBuilderTest.cs @@ -115,4 +115,4 @@ public async Task ShouldWork() adminStorage.GlobalAdmins.Single().FirstName.Should().Be("Big"); adminStorage.GlobalAdmins.Single().LastName.Should().Be("Bird"); } -} \ No newline at end of file +} diff --git a/Sample/EventPipelines/EventPipelines.Tests/ClassesWithIoCTest.cs b/Sample/EventPipelines/EventPipelines.Tests/ClassesWithIoCTest.cs index e27be4578..df7953f86 100644 --- a/Sample/EventPipelines/EventPipelines.Tests/ClassesWithIoCTest.cs +++ b/Sample/EventPipelines/EventPipelines.Tests/ClassesWithIoCTest.cs @@ -121,4 +121,4 @@ public async Task ShouldWork() adminStorage.GlobalAdmins.Single().FirstName.Should().Be("Big"); adminStorage.GlobalAdmins.Single().LastName.Should().Be("Bird"); } -} \ No newline at end of file +} diff --git a/Sample/EventPipelines/EventPipelines.Tests/ClassesWithMediatRTest.cs b/Sample/EventPipelines/EventPipelines.Tests/ClassesWithMediatRTest.cs index e7c257168..9e3a6feb1 100644 --- a/Sample/EventPipelines/EventPipelines.Tests/ClassesWithMediatRTest.cs +++ b/Sample/EventPipelines/EventPipelines.Tests/ClassesWithMediatRTest.cs @@ -124,4 +124,4 @@ public async Task ShouldWork() adminStorage.GlobalAdmins.Single().FirstName.Should().Be("Big"); adminStorage.GlobalAdmins.Single().LastName.Should().Be("Bird"); } -} \ No newline at end of file +} diff --git a/Sample/EventPipelines/EventPipelines/Builder.cs b/Sample/EventPipelines/EventPipelines/Builder.cs index d606309a2..7abdcea87 100644 --- a/Sample/EventPipelines/EventPipelines/Builder.cs +++ b/Sample/EventPipelines/EventPipelines/Builder.cs @@ -65,4 +65,4 @@ public EventHandlersBuilder Filter( Handle(handler); public IEnumerable Build() => eventHandlers; -} \ No newline at end of file +} diff --git a/Sample/EventPipelines/EventPipelines/Configuration.cs b/Sample/EventPipelines/EventPipelines/Configuration.cs index 23df94a95..28549282c 100644 --- a/Sample/EventPipelines/EventPipelines/Configuration.cs +++ b/Sample/EventPipelines/EventPipelines/Configuration.cs @@ -71,4 +71,4 @@ public static IServiceCollection Filter( this IServiceCollection services, IEventFilter handler) => services.AddEventHandler(handler); -} \ No newline at end of file +} diff --git a/Sample/EventPipelines/EventPipelines/EventBus.cs b/Sample/EventPipelines/EventPipelines/EventBus.cs index e753e5731..df3c84627 100644 --- a/Sample/EventPipelines/EventPipelines/EventBus.cs +++ b/Sample/EventPipelines/EventPipelines/EventBus.cs @@ -172,4 +172,4 @@ public async Task Publish(object @event, CancellationToken ct) await Publish(result, ct); } } -} \ No newline at end of file +} diff --git a/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/Config.cs b/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/Config.cs index 7ec98b068..34086d7e4 100644 --- a/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/Config.cs +++ b/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/Config.cs @@ -8,7 +8,6 @@ using Carts.ShoppingCarts.InitializingCart; using Carts.ShoppingCarts.RemovingProduct; using Core.Commands; -using Core.Events.NoMediator; using Core.EventStoreDB.Repository; using Core.Marten.ExternalProjections; using Core.Queries; @@ -37,14 +36,6 @@ private static IServiceCollection AddCommandHandlers(this IServiceCollection ser .AddCommandHandler(); } - public class MyClass: INoMediatorEventHandler - { - public Task Handle(ShoppingCartInitialized @event, CancellationToken ct) - { - return Task.CompletedTask; - } - } - private static IServiceCollection AddProjections(this IServiceCollection services) { services diff --git a/Sample/EventStoreDB/Simple/ECommerce.Core/Configuration.cs b/Sample/EventStoreDB/Simple/ECommerce.Core/Configuration.cs index 16eb2c097..f64b3fcaa 100644 --- a/Sample/EventStoreDB/Simple/ECommerce.Core/Configuration.cs +++ b/Sample/EventStoreDB/Simple/ECommerce.Core/Configuration.cs @@ -1,4 +1,4 @@ -using Core.Events.NoMediator; +using Core.Events; using Core.EventStoreDB; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; diff --git a/Sample/EventStoreDB/Simple/ECommerce.Core/Projections/EntityFrameworkProjection.cs b/Sample/EventStoreDB/Simple/ECommerce.Core/Projections/EntityFrameworkProjection.cs index 6cfbb18ae..3a5c27a81 100644 --- a/Sample/EventStoreDB/Simple/ECommerce.Core/Projections/EntityFrameworkProjection.cs +++ b/Sample/EventStoreDB/Simple/ECommerce.Core/Projections/EntityFrameworkProjection.cs @@ -1,5 +1,4 @@ using Core.Events; -using Core.Events.NoMediator; using ECommerce.Core.Queries; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; @@ -35,7 +34,7 @@ public EntityFrameworkProjectionBuilder(IServiceCollection services) public EntityFrameworkProjectionBuilder AddOn(Func, TView> handler) where TEvent : notnull { services.AddSingleton(handler); - services.AddTransient>, AddProjection>(); + services.AddTransient>, AddProjection>(); return this; } @@ -48,7 +47,7 @@ public EntityFrameworkProjectionBuilder UpdateOn( { services.AddSingleton(getViewId); services.AddSingleton(handler); - services.AddTransient>, UpdateProjection>(); + services.AddTransient>, UpdateProjection>(); if (prepare != null) { @@ -77,7 +76,7 @@ Func, TQuery, CancellationToken, Task>> h } } -public class AddProjection: INoMediatorEventHandler> +public class AddProjection: IEventHandler> where TView: class where TDbContext: DbContext where TEvent : notnull @@ -103,7 +102,7 @@ public async Task Handle(EventEnvelope eventEnvelope, CancellationToken } } -public class UpdateProjection: INoMediatorEventHandler> +public class UpdateProjection: IEventHandler> where TView: class where TDbContext: DbContext where TEvent : notnull diff --git a/Sample/MeetingsManagement/MeetingsManagement/Notifications/Config.cs b/Sample/MeetingsManagement/MeetingsManagement/Notifications/Config.cs index e90b097ef..9464a0ef8 100644 --- a/Sample/MeetingsManagement/MeetingsManagement/Notifications/Config.cs +++ b/Sample/MeetingsManagement/MeetingsManagement/Notifications/Config.cs @@ -1,4 +1,4 @@ -using MediatR; +using Core.Events; using MeetingsManagement.Meetings.CreatingMeeting; using MeetingsManagement.Notifications.NotifyingByEvent; using Microsoft.Extensions.DependencyInjection; @@ -8,5 +8,5 @@ namespace MeetingsManagement.Notifications; public static class Config { public static IServiceCollection AddNotifications(this IServiceCollection services) => - services.AddScoped, EmailNotifier>(); + services.AddEventHandler(); } diff --git a/Sample/MeetingsManagement/MeetingsManagement/Notifications/NotifyingByEvent/EmailNotifier.cs b/Sample/MeetingsManagement/MeetingsManagement/Notifications/NotifyingByEvent/EmailNotifier.cs index ce49f7d4b..43684f9ba 100644 --- a/Sample/MeetingsManagement/MeetingsManagement/Notifications/NotifyingByEvent/EmailNotifier.cs +++ b/Sample/MeetingsManagement/MeetingsManagement/Notifications/NotifyingByEvent/EmailNotifier.cs @@ -12,4 +12,4 @@ public Task Handle(MeetingCreated @event, CancellationToken cancellationToken) return Task.CompletedTask; } -} \ No newline at end of file +}