public static async Task PostEvent(this IMediatorHandler mediator, SaleContext ctx) { var domainEntities = ctx.ChangeTracker .Entries <Entity>() .Where(x => x.Entity.Notifications != null && x.Entity.Notifications.Any()); var domainEvents = domainEntities .SelectMany(x => x.Entity.Notifications) .ToList(); domainEntities.ToList() .ForEach(entity => entity.Entity.ClearEvents()); var tasks = domainEvents .Select(async(domainEvent) => { await mediator.PostEvent(domainEvent); }); await Task.WhenAll(tasks); }
public async Task <bool> Commit() { foreach (var entry in ChangeTracker.Entries().Where(entry => entry.Entity.GetType().GetProperty("CreatedDate") != null)) { if (entry.State == EntityState.Added) { entry.Property("CreatedDate").CurrentValue = DateTime.Now; } if (entry.State == EntityState.Modified) { entry.Property("CreatedDate").IsModified = false; } } var sucesso = await base.SaveChangesAsync() > 0; if (sucesso) { await _mediatorHandler.PostEvent(this); } return(sucesso); }
public async Task <bool> Debit(Guid productId, int quantity) { var product = await _productRepository.GetById(productId); if (product == null) { return(false); } if (!product.HasAnyInStock(quantity)) { return(false); } product.DebitStock(quantity); if (product.StockQuantity < 2) { await _memoryBus.PostEvent(new ProductLowStockEvent(product.Id, product.StockQuantity)); } _productRepository.Update(product); return(await _productRepository.UnitOfWork.Commit()); }