Last active
December 29, 2016 21:18
-
-
Save mahizsas/9fa68e8983e64262f8f1 to your computer and use it in GitHub Desktop.
Autofac implementation for mediator pipeline
This file contains 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 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