Created
August 8, 2016 09:22
-
-
Save jrgcubano/c4852e2d1779fec37c2eb97d4a7a7dec to your computer and use it in GitHub Desktop.
AsyncMediatorPipeline and Autofac Registration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AsyncMediatorPipeline<TRequest, TResponse> : IAsyncRequestHandler<TRequest, TResponse> where TRequest : IAsyncRequest<TResponse> | |
{ | |
private readonly IAsyncRequestHandler<TRequest, TResponse> inner; | |
private readonly IAsyncPreRequestHandler<TRequest>[] preRequestHandlers; | |
private readonly IAsyncPostRequestHandler<TRequest, TResponse>[] postRequestHandlers; | |
public AsyncMediatorPipeline(IAsyncRequestHandler<TRequest, TResponse> inner, IAsyncPreRequestHandler<TRequest>[] preRequestHandlers, IAsyncPostRequestHandler<TRequest, TResponse>[] postRequestHandlers) | |
{ | |
this.inner = inner; | |
this.preRequestHandlers = preRequestHandlers; | |
this.postRequestHandlers = postRequestHandlers; | |
} | |
public async Task<TResponse> Handle(TRequest message) | |
{ | |
foreach (var preRequestHandler in preRequestHandlers) | |
{ | |
await preRequestHandler.Handle(message); | |
} | |
var result = await inner.Handle(message); | |
foreach (var postRequestHandler in postRequestHandlers) | |
{ | |
await postRequestHandler.Handle(message, result); | |
} | |
return result; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MediatorModule : Module | |
{ | |
protected override void Load(ContainerBuilder builder) | |
{ | |
builder.RegisterType<Mediator>() | |
.AsImplementedInterfaces() | |
.AsSelf() | |
.InstancePerLifetimeScope(); | |
//register all pre handlers | |
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) | |
.As(type => type.GetInterfaces() | |
.Where(interfacetype => interfacetype.IsClosedTypeOf(typeof (IAsyncPreRequestHandler<>)))) | |
.InstancePerLifetimeScope(); | |
//register all post handlers | |
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) | |
.As(type => type.GetInterfaces() | |
.Where(interfacetype => interfacetype.IsClosedTypeOf(typeof(IAsyncPostRequestHandler<,>)))) | |
.InstancePerLifetimeScope(); | |
//register all asynchandlers | |
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) | |
.As(type => type.GetInterfaces() | |
.Where(interfaceType => interfaceType.IsClosedTypeOf(typeof(IAsyncRequestHandler<,>))) | |
.Select(interfaceType => new KeyedService("asyncRequestHandler", interfaceType))) | |
.InstancePerLifetimeScope(); | |
//register pipeline decorators | |
builder.RegisterGenericDecorator(typeof(AsyncMediatorPipeline<,>), typeof(IAsyncRequestHandler<,>), "asyncRequestHandler") | |
.InstancePerLifetimeScope(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment