public async Task HandleAsync(IReadOnlyCollection <IDomainEvent> domainEvents, CancellationToken cancellationToken) { var domainEvent = domainEvents.Single(); switch (domainEvent.EventType.FullName) { case "Invoice.Core.DomainModel.InvoiceCreatedEvent": { // Initiate process for this invoice var id = domainEvent.GetIdentity(); var invoiceId = Guid.Parse(id.Value.Replace("invoice-", "")); await _assignmentService.CreateAssignment(new CreateAssignmentCommand(AssignmentId.New, invoiceId)); break; } case "Invoice.Core.DomainModel.InvoiceSentEvent": { var id = domainEvent.GetIdentity(); var invoiceId = Guid.Parse(id.Value.Replace("invoice-", "")); var assignment = await _queryProcessor.ProcessAsync(new GetAssignmentByInvoiceIdQuery(invoiceId), CancellationToken.None); await _assignmentService.SetWaitingForPayment(new SetWaitingForPaymentCommand(AssignmentId.With(assignment.AssignmentId), assignment.InvoiceId)); await _paymentService.SetWaitingForPayment(new WaitingForPaymentRequest() { InvoiceId = invoiceId }); var assignmentInvoice = await _queryProcessor.ProcessAsync(new GetAssignmentInvoiceQuery(invoiceId), CancellationToken.None); if (assignmentInvoice.PayInAdvance) { await _payoutService.PayEmployee(new PayEmployeeRequest { InvoiceId = invoiceId }); } break; } case "Payment.Core.DomainModel.PaymentReceivedEvent": { dynamic paymentReceived = domainEvent.GetAggregateEvent(); Guid invoiceId = paymentReceived.InvoiceId; var assignmentInvoice = await _queryProcessor.ProcessAsync(new GetAssignmentInvoiceQuery(invoiceId), CancellationToken.None); if (!assignmentInvoice.PayInAdvance) { await _payoutService.PayEmployee(new PayEmployeeRequest { InvoiceId = invoiceId }); } break; } case "Payment.Core.DomainModel.PaymentDueEvent": { dynamic paymentDue = domainEvent.GetAggregateEvent(); Guid invoiceId = paymentDue.InvoiceId; await _invoiceService.SendReminder(invoiceId); break; } case "Payout.Core.DomainModel.EmployeePaidEvent": { // close assignment dynamic employeePaid = domainEvent.GetAggregateEvent(); Guid invoiceId = employeePaid.InvoiceId; var assignment = await _queryProcessor.ProcessAsync(new GetAssignmentByInvoiceIdQuery(invoiceId), CancellationToken.None); await _assignmentService.CloseAssignment(new CloseAssignmentCommand(AssignmentId.With(assignment.AssignmentId))); break; } } }