Unverified Commit 470e52a2 authored by Eric Fleming's avatar Eric Fleming Committed by GitHub

Merge pull request #88 from ardalis/fix-domainEventDispatcher

Fixing the domain event dispatcher
parents ad0c7d36 004fa015
using Autofac;
using Autofac.Extensions.DependencyInjection;
using CleanArchitecture.Core.SharedKernel;
using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Infrastructure.Data;
using Microsoft.Extensions.DependencyInjection;
using System;
......@@ -8,7 +8,7 @@ using System.Reflection;
namespace CleanArchitecture.Infrastructure
{
public static class ContainerSetup
public static class ContainerSetup
{
public static IServiceProvider InitializeWeb(Assembly webAssembly, IServiceCollection services) =>
new AutofacServiceProvider(BaseAutofacInitialization(setupAction =>
......@@ -21,7 +21,7 @@ namespace CleanArchitecture.Infrastructure
{
var builder = new ContainerBuilder();
var coreAssembly = Assembly.GetAssembly(typeof(BaseEntity));
var coreAssembly = Assembly.GetAssembly(typeof(IRepository));
var infrastructureAssembly = Assembly.GetAssembly(typeof(EfRepository));
builder.RegisterAssemblyTypes(coreAssembly, infrastructureAssembly).AsImplementedInterfaces();
......
......@@ -21,6 +21,16 @@ namespace CleanArchitecture.Infrastructure.DomainEvents
}
public async Task Dispatch(BaseDomainEvent domainEvent)
{
var wrappedHandlers = GetWrappedHandlers(domainEvent);
foreach (DomainEventHandler handler in wrappedHandlers)
{
await handler.Handle(domainEvent).ConfigureAwait(false);
}
}
public IEnumerable<DomainEventHandler> GetWrappedHandlers(BaseDomainEvent domainEvent)
{
Type handlerType = typeof(IHandle<>).MakeGenericType(domainEvent.GetType());
Type wrapperType = typeof(DomainEventHandler<>).MakeGenericType(domainEvent.GetType());
......@@ -28,18 +38,15 @@ namespace CleanArchitecture.Infrastructure.DomainEvents
IEnumerable<DomainEventHandler> wrappedHandlers = handlers.Cast<object>()
.Select(handler => (DomainEventHandler)Activator.CreateInstance(wrapperType, handler));
foreach (DomainEventHandler handler in wrappedHandlers)
{
await handler.Handle(domainEvent).ConfigureAwait(false);
}
return wrappedHandlers;
}
private abstract class DomainEventHandler
public abstract class DomainEventHandler
{
public abstract Task Handle(BaseDomainEvent domainEvent);
}
private class DomainEventHandler<T> : DomainEventHandler
public class DomainEventHandler<T> : DomainEventHandler
where T : BaseDomainEvent
{
private readonly IHandle<T> _handler;
......
using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Events;
using CleanArchitecture.Infrastructure;
using CleanArchitecture.Infrastructure.DomainEvents;
using Xunit;
namespace CleanArchitecture.UnitTests.Core.DomainEvents
{
public class DomainEventDispatcherShould
{
[Fact]
public void NotReturnAnEmptyListOfAvailableHandlers()
{
var container = ContainerSetup.BaseAutofacInitialization();
var domainEventDispatcher = new DomainEventDispatcher(container);
var toDoItemCompletedEvent = new ToDoItemCompletedEvent(new ToDoItem());
var handlersForEvent = domainEventDispatcher.GetWrappedHandlers(toDoItemCompletedEvent);
Assert.NotEmpty(handlersForEvent);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment