I'm trying to follow this post by Jimmy Bogard to implement a mediator pipeline so I can use pre/post request handlers to do some work. From the comments on that article I come to this github gist. I don't quite understand how to hook all of this up yet, so here is my first go. FYI - I'm using Autofac for DI and Web Api 2. Following CQRS, here is a query.
public class GetAccountRequest : IAsyncRequest<GetAccountResponse>
{
public int Id { get; set; }
}
//try using fluent validation
public class GetAccountRequestValidationHandler
: AbstractValidator<GetAccountRequest>, IAsyncPreRequestHandler<GetAccountRequest>
{
public GetAccountRequestValidationHandler() {
RuleFor(m => m.Id).GreaterThan(0).WithMessage("Please specify an id.");
}
public Task Handle(GetAccountRequest request) {
Debug.WriteLine("GetAccountPreProcessor Handler");
return Task.FromResult(true);
}
}
public class GetAccountResponse
{
public int AccountId { get; set; }
public string Name { get; set; }
public string AccountNumber { get; set; }
public string Nickname { get; set; }
public string PhoneNumber { get; set; }
public List<OrderAckNotification> OrderAckNotifications { get; set; }
public class OrderAckNotification {
public int Id { get; set; }
public bool IsDefault { get; set; }
public string Description { get; set; }
public string Type { get; set; }
}
}
GetAccountRequestHandler:
public class GetAccountRequestHandler
: IAsyncRequestHandler<GetAccountRequest, GetAccountResponse>
{
private readonly IRedStripeDbContext _dbContext;
public GetAccountRequestHandler(IRedStripeDbContext redStripeDbContext)
{
_dbContext = redStripeDbContext;
}
public async Task<GetAccountResponse> Handle(GetAccountRequest message)
{
//some mapping code here.. omitted for brevity
Mapper.AssertConfigurationIsValid();
return await _dbContext.Accounts.Where(a => a.AccountId == message.Id)
.ProjectToSingleOrDefaultAsync<GetAccountResponse>();
}
Here is the current web api 2 controller showing the HttpGet.
[RoutePrefix("api/Accounts")]
public class AccountsController : ApiController
{
private readonly IMediator _mediator;
public AccountsController(IMediator mediator)
{
_mediator = mediator;
}
// GET: api/Accounts/2
[Route("{id:int}")]
[HttpGet]
public async Task<IHttpActionResult> GetById([FromUri] GetAccountRequest request)
{
var model = await _mediator.SendAsync<GetAccountResponse>(request);
return Ok(model);
}
}
Finally here is the dependency resolution code:
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
ConfigureDependencyInjection(app, config);
WebApiConfig.Register(config);
app.UseWebApi(config);
}
private static void ConfigureDependencyInjection(IAppBuilder app,
HttpConfiguration config)
{
var builder = new ContainerBuilder();
builder.RegisterSource(new ContravariantRegistrationSource());
builder.RegisterAssemblyTypes(typeof(IMediator).Assembly).AsImplementedInterfaces();
builder.Register<SingleInstanceFactory>(ctx =>
{
var c = ctx.Resolve<IComponentContext>();
return t => c.Resolve(t);
});
builder.Register<MultiInstanceFactory>(ctx =>
{
var c = ctx.Resolve<IComponentContext>();
return t => (IEnumerable<object>)c.Resolve(
typeof(IEnumerable<>).MakeGenericType(t));
});
//register all pre handlers
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.As(type => type.GetInterfaces()
.Where(t => t.IsClosedTypeOf(typeof(IAsyncPreRequestHandler<>))));
//register all post handlers
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.As(type => type.GetInterfaces()
.Where(t => t.IsClosedTypeOf(typeof(IAsyncPostRequestHandler<,>))));
//register all handlers
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.As(type => type.GetInterfaces()
.Where(t => t.IsClosedTypeOf(typeof(IAsyncRequestHandler<,>)))
.Select(t => new KeyedService("asyncRequestHandler", t)));
//register pipeline decorator
builder.RegisterGenericDecorator(typeof(AsyncMediatorPipeline<,>),
typeof(IAsyncRequestHandler<,>), "asyncRequestHandler");
// Register Web API controller in executing assembly.
builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).InstancePerRequest();
//register RedStripeDbContext
builder.RegisterType<RedStripeDbContext>().As<IRedStripeDbContext>()
.InstancePerRequest();
builder.RegisterType<AutofacServiceLocator>().AsImplementedInterfaces();
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
// This should be the first middleware added to the IAppBuilder.
app.UseAutofacMiddleware(container);
// Make sure the Autofac lifetime scope is passed to Web API.
app.UseAutofacWebApi(config);
}
I am getting into the GetAccountRequestValidationHandler. However, when the validation fails (an id of 0 was passed), how do I throw an exception or stop the execution of the pipeline? How do I return the .WithMessage?
CQRS simply means you completely separate writes and reads, make the separate concerns. You have CQRS as long as you do that. Your commands do not even need to be asynchronous. If synchronous commands with error reporting work for you, do that. – Andy.
Source from: Microsoft — Command and Query Responsibility Segregation (CQRS) pattern. MediatR is an open source implementation of the mediator pattern that doesn't try to do too much and performs no magic. It allows you to compose messages, create and listen for events using synchronous or asynchronous patterns.
CQRS stands for “Command Query Responsibility Segregation”. As the acronym suggests, it's all about splitting the responsibility of commands (saves) and queries (reads) into different models.
CQRS stands for Command and Query Responsibility Segregation, a pattern that separates read and update operations for a data store. Implementing CQRS in your application can maximize its performance, scalability, and security.
I've semi-struggling with this, too. It seems there are two/three options:
Using a pre-handler...
1) you can either load errors into the request and have the main handler check for errors before it processes the command/query
OR
2) Have the pre-handler throw an exception. It seems there's a fair bit of disagreement around this practice. On one hand it feels like managing control-flow with exceptions, but the "pro" camp argues that the client should be responsible for sending a valid command to begin with. Ie. It can send ajax queries to confirm that a user name is available prior to letting the user click "Create Account". In this case an exception breaking this rule would be due to a race condition.
Put the validation handler directly into the pipeline.
I believe this is more along the lines of what @jbogard was thinking. I am not currently using this, but I've sketched up what this might look like -- there are probably better examples out there, and of course exactly how you want to define and handle things may vary. The gist of it is that with it being part of the pipeline, the validation-runner can return to the caller without the main handler ever being called.
public class AsyncValidationPipeline<TRequest, TResponse> : IAsyncRequestHandler<TRequest, TResponse>
where TRequest : IAsyncRequest<TResponse>
{
private IAsyncRequestHandler<TRequest, TResponse> _inner;
private IValidator<TRequest>[] _validators;
public AsyncValidationPipeline(IAsyncRequestHandler<TRequest, TResponse> inner,
IValidator<TRequest>[] validators)
{
_inner = inner;
_validators = validators;
}
public Task<TResponse> Handle(TRequest message)
{
List<string> errors = new List<string>();
if (_validators != null && _validators.Any())
{
errors = _validators.Where(v => v.Fails(message))
.Select(v => v.ErrorMessage);
}
if (errors.Any())
{
throw new ValidationException(errors);
}
return _inner.Handle(message);
}
}
Here's the code for hooking that up with AutoFac:
//register all pre handlers
builder.RegisterAssemblyTypes(assembliesToScan)
.AsClosedTypesOf(typeof(IAsyncPreRequestHandler<>));
//register all post handlers
builder.RegisterAssemblyTypes(assembliesToScan)
.AsClosedTypesOf(typeof(IAsyncPostRequestHandler<,>));
const string handlerKey = "async-service-handlers";
const string pipelineKey = "async-service-pipelines";
// Request/Response for Query
builder.RegisterAssemblyTypes(assembliesToScan)
.AsKeyedClosedTypesOf(typeof(IAsyncRequestHandler<,>), handlerKey)
;
// Decorate All Services with our Pipeline
//builder.RegisterGenericDecorator(typeof(MediatorPipeline<,>), typeof(IRequestHandler<,>), fromKey: "service-handlers", toKey: "pipeline-handlers");
builder.RegisterGenericDecorator(typeof(AsyncMediatorPipeline<,>), typeof(IAsyncRequestHandler<,>), fromKey: handlerKey, toKey: pipelineKey);
// Decorate All Pipelines with our Validator
builder.RegisterGenericDecorator(typeof(AsyncValidationHandler<,>), typeof(IAsyncRequestHandler<,>), fromKey: pipelineKey);//, toKey: "async-validator-handlers");
// Add as many pipelines as you want, but the to/from keys must be kept in order and unique
Hope this helps....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With