Business rule validators and command handler in CQRS

It’s important to know that commands can be rejected after they are sent to the handler. At the very least, you could encounter a concurrency violation that cannot be detected until the aggregate root is touched. But also, the validation that can occur outside of the entity is simple validation. Not only string lengths, numeric … Read more

In CQRS, should my read side return DTOs or ViewModels?

The general advice is one projection per screen (Greg Young) or even one projection per widget (if I understand Udi Dahan correctly). To consolidate read models into DTOs that once again have to be mapped into separate views contradicts the whole purpose of an optimized read model. It adds complexity and mapping steps that we … Read more

Add validation to a MediatR behavior pipeline?

The process is exactly the same, you just have to change the interface to use the new IPipelineBehavior<TRequest, TResponse> interface. public class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse> { private readonly IEnumerable<IValidator<TRequest>> _validators; public ValidationBehavior(IEnumerable<IValidator<TRequest>> validators) { _validators = validators; } public Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next) { var context = new … Read more

Communicating between two Bounded Contexts in DDD

When integrating BCs, you have a few options. The reason that calling out to an external BC is discouraged is because it requires for both BCs to be operational at the same time. However, this is often quite acceptable and is simpler than the alternative. An alternative is to have the Game BC subscribe to … Read more

Alternatives to many-to-many relationships with CQRS

Supposing that both are aggregates, copy whatever author data you need into the Book aggregate upon adding the new book so that any subsequent commands have enough author data to work with. Now if the Author aggregate needs information about the books written by the author, then it could “subscribe” to the NewBookAdded event (technically … Read more

Value Objects in CQRS – where to use

Ok I’ve changed my mind. I have been trying to deal with VOs a bunch lately and after watching this http://www.infoq.com/presentations/Value-Objects-Dan-Bergh-Johnsson it clarified a couple of things for me. Commands and Event are messages (and not objects, objects are data + behavior), in some respects much like DTOs, they communicate data about an event and … Read more

Relation between command handlers, aggregates, the repository and the event store in CQRS

The following is based on my own experience and my experiments with various frameworks like Lokad.CQRS, NCQRS, etc. I’m sure there are multiple ways to handle this. I’ll post what makes most sense to me. 1. Aggregate Creation: Every time a command handler needs an aggregate, it uses a repository. The repository retrieves the respective … Read more

IRequestHandler return void

Generally speaking, if a Task based method does not return anything you can return a completed Task public Task Handle(CreatePersonCommand message, CancellationToken cancellationToken) { return Task.CompletedTask; } Now, in MediatR terms a value needs te be returned. In case of no value you can use Unit: public Task<Unit> Handle(CreatePersonCommand message, CancellationToken cancellationToken) { return Task.FromResult(Unit.Value); … Read more