public async Task Consume(ConsumeContext <CreateBurgerOrder> context) { using (var scope = new TransactionScope(TransactionScopeOption.Required, TransactionScopeAsyncFlowOption.Enabled)) { if (await _orderIdRepository.ExistsAsync(context.Message.OrderId)) { throw new ValidationException("Unable to create Order", new ValidationError(nameof(CreateBurgerOrder), $"Id '{context.Message.OrderId}' already exists")); } var orderAggregate = new Order(context.Message.OrderId, context.Message.CustomerId); // Here we are doing 3 things: // * persisting the aggregate // * publishing domain events // * and persisting order id. // This is across multiple technologies (postgres, mongo, rabbitmq). // We are concerned with data consistency. await _orderEventsService.PersistAsync(orderAggregate); await _orderIdRepository.CreateAsync(context.Message.OrderId); scope.Complete(); } }
public async Task Consume(ConsumeContext <CreateBurgerOrder> context) { using (var scope = new TransactionScope(TransactionScopeOption.Required, TransactionScopeAsyncFlowOption.Enabled)) { if (await _orderIdRepository.ExistsAsync(context.Message.OrderId)) { throw new ValidationException("Unable to create Order", new ValidationError(nameof(CreateBurgerOrder), $"Id '{context.Message.OrderId}' already exists")); } var orderAggregate = new Order(context.Message.OrderId); await _orderEventsService.PersistAsync(orderAggregate); await _orderIdRepository.CreateAsync(context.Message.OrderId); scope.Complete(); } }