private async Task <object> HandleNext(ICommandBase command, CommandBusMiddlewareDelegate next, IUnitOfWork unitOfWork) { ICommandContext newContext = new CommandContext(command, unitOfWork); commandContextStack.Push(newContext); newContext.UnitOfWork?.Begin(); try { var result = await next(command); if (newContext.UnitOfWork != null) { await newContext.UnitOfWork.CommitAsync(); } return(result); } finally { Debug.Assert(commandContextStack.PeekOrDefault == newContext); commandContextStack.Pop(); } }
public async Task PostFilterAsync(ICommandBase command, object result) { try { if (commandContextStack.UnitOfWork != null && !command.GetType().GetInterfaces().Any( x => x.IsConstructedGenericType && x.GetGenericTypeDefinition() == typeof(IQuery <>))) { await commandContextStack.UnitOfWork.CommitAsync(); } } finally { if (commandContextStack.PeekOrDefault != null) { commandContextStack.Pop(); } } }