-
Notifications
You must be signed in to change notification settings - Fork 649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should we persist this to the database? #16
Comments
Hi @lisandropacheco, All command handlers are decorated by UnitOfWork decorator: public class UnitOfWorkCommandHandlerWithResultDecorator<T, TResult> : ICommandHandler<T, TResult> where T : ICommand<TResult>
{
private readonly ICommandHandler<T, TResult> _decorated;
private readonly IUnitOfWork _unitOfWork;
public UnitOfWorkCommandHandlerWithResultDecorator(
ICommandHandler<T, TResult> decorated,
IUnitOfWork unitOfWork)
{
_decorated = decorated;
_unitOfWork = unitOfWork;
}
public async Task<TResult> Handle(T command, CancellationToken cancellationToken)
{
var result = await this._decorated.Handle(command, cancellationToken);
await this._unitOfWork.CommitAsync(cancellationToken);
return result;
}
} After the command is handled by CommandHandler, this decorator invokes UnitOfWork.CommitAsync method which publishes all events and saves changes to the database: public class UnitOfWork : IUnitOfWork
{
private readonly OrdersContext _ordersContext;
private readonly IDomainEventsDispatcher _domainEventsDispatcher;
public UnitOfWork(
OrdersContext ordersContext,
IDomainEventsDispatcher domainEventsDispatcher)
{
this._ordersContext = ordersContext;
this._domainEventsDispatcher = domainEventsDispatcher;
}
public async Task<int> CommitAsync(CancellationToken cancellationToken = default(CancellationToken))
{
await this._domainEventsDispatcher.DispatchEventsAsync();
return await this._ordersContext.SaveChangesAsync(cancellationToken);
}
} This is the implementation of the Chain Of Responsibility pattern when one object is processed by several handlers. Each handler has own responsibility (logging, validating, saving, processing) and a pointer to the next handler. |
Hey Kamil, thanks for the quick answer. I'm the same lisandropacheco. The initial question was linked to an older git hub work account that now has been deleted. That's why I didn't get the notification to our answer before! |
Hey Kamil,
First thanks for the great example! I'm learning a lot from it!
While creating a new order I can't see where those are persisted on the database and consequently can't see any processing of outbox item.
Is it the case that we should persist the order on the db after the line bellow?
sample-dotnet-core-cqrs-api/src/SampleProject.Application/Orders/PlaceCustomerOrder/PlaceCustomerOrderCommandHandler.cs
Line 45 in 01a1d65
The text was updated successfully, but these errors were encountered: