Domain Event Handlers are now grouped by Domain Events, as any Domain Event could have 'n' handlers related.

This commit is contained in:
Cesar De la Torre 2017-03-18 11:44:18 -07:00
parent c7e317413e
commit 3ed136b00f
8 changed files with 31 additions and 16 deletions

View File

@ -5,13 +5,13 @@ using Ordering.Domain.Events;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Ordering.API.Application.DomainEventHandlers namespace Ordering.API.Application.DomainEventHandlers.BuyerAndPaymentMethodVerified
{ {
public class BuyerPaymentMethodVerifiedDomainEventHandler : IAsyncNotificationHandler<BuyerPaymentMethodVerifiedDomainEvent> public class UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler : IAsyncNotificationHandler<BuyerAndPaymentMethodVerifiedDomainEvent>
{ {
private readonly IOrderRepository<Order> _orderRepository; private readonly IOrderRepository<Order> _orderRepository;
private readonly ILoggerFactory _logger; private readonly ILoggerFactory _logger;
public BuyerPaymentMethodVerifiedDomainEventHandler(IOrderRepository<Order> orderRepository, ILoggerFactory logger) public UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler(IOrderRepository<Order> orderRepository, ILoggerFactory logger)
{ {
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));
@ -20,7 +20,7 @@ namespace Ordering.API.Application.DomainEventHandlers
// Domain Logic comment: // Domain Logic comment:
// When the Buyer and Buyer's payment method have been created or verified that they existed, // When the Buyer and Buyer's payment method have been created or verified that they existed,
// then we can update the original Order with the BuyerId and PaymentId (foreign keys) // then we can update the original Order with the BuyerId and PaymentId (foreign keys)
public async Task Handle(BuyerPaymentMethodVerifiedDomainEvent buyerPaymentMethodVerifiedEvent) public async Task Handle(BuyerAndPaymentMethodVerifiedDomainEvent buyerPaymentMethodVerifiedEvent)
{ {
var orderToUpdate = await _orderRepository.GetAsync(buyerPaymentMethodVerifiedEvent.OrderId); var orderToUpdate = await _orderRepository.GetAsync(buyerPaymentMethodVerifiedEvent.OrderId);
orderToUpdate.SetBuyerId(buyerPaymentMethodVerifiedEvent.Buyer.Id); orderToUpdate.SetBuyerId(buyerPaymentMethodVerifiedEvent.Buyer.Id);
@ -29,7 +29,7 @@ namespace Ordering.API.Application.DomainEventHandlers
await _orderRepository.UnitOfWork await _orderRepository.UnitOfWork
.SaveEntitiesAsync(); .SaveEntitiesAsync();
_logger.CreateLogger(nameof(BuyerPaymentMethodVerifiedDomainEventHandler)) _logger.CreateLogger(nameof(UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler))
.LogTrace($"Order with Id: {buyerPaymentMethodVerifiedEvent.OrderId} has been successfully updated with a payment method id: { buyerPaymentMethodVerifiedEvent.Payment.Id }"); .LogTrace($"Order with Id: {buyerPaymentMethodVerifiedEvent.OrderId} has been successfully updated with a payment method id: { buyerPaymentMethodVerifiedEvent.Payment.Id }");
} }
} }

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
{
public class SendEmailToCustomerWhenOrderStartedDomainEventHandler
{
//TO DO
}
}

View File

@ -6,15 +6,15 @@ using Ordering.Domain.Events;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Ordering.API.Application.DomainEventHandlers namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
{ {
public class OrderStartedDomainEventHandler : IAsyncNotificationHandler<OrderStartedDomainEvent> public class ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler : IAsyncNotificationHandler<OrderStartedDomainEvent>
{ {
private readonly ILoggerFactory _logger; private readonly ILoggerFactory _logger;
private readonly IBuyerRepository<Buyer> _buyerRepository; private readonly IBuyerRepository<Buyer> _buyerRepository;
private readonly IIdentityService _identityService; private readonly IIdentityService _identityService;
public OrderStartedDomainEventHandler(ILoggerFactory logger, IBuyerRepository<Buyer> buyerRepository, IIdentityService identityService) public ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler(ILoggerFactory logger, IBuyerRepository<Buyer> buyerRepository, IIdentityService identityService)
{ {
_buyerRepository = buyerRepository ?? throw new ArgumentNullException(nameof(buyerRepository)); _buyerRepository = buyerRepository ?? throw new ArgumentNullException(nameof(buyerRepository));
_identityService = identityService ?? throw new ArgumentNullException(nameof(identityService)); _identityService = identityService ?? throw new ArgumentNullException(nameof(identityService));
@ -46,7 +46,7 @@ namespace Ordering.API.Application.DomainEventHandlers
await _buyerRepository.UnitOfWork await _buyerRepository.UnitOfWork
.SaveEntitiesAsync(); .SaveEntitiesAsync();
_logger.CreateLogger(nameof(OrderStartedDomainEventHandler)).LogTrace($"A new payment method has been successfully added for orderId: {orderNotification.Order.Id}."); _logger.CreateLogger(nameof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler)).LogTrace($"A new payment method has been successfully added for orderId: {orderNotification.Order.Id}.");
} }
} }

View File

@ -3,7 +3,7 @@ using Autofac.Core;
using MediatR; using MediatR;
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands; using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Decorators; using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Decorators;
using Ordering.API.Application.DomainEventHandlers; using Ordering.API.Application.DomainEventHandlers.OrderStartedEvent;
using Ordering.Domain.Events; using Ordering.Domain.Events;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -24,7 +24,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Autof
.Select(i => new KeyedService("IAsyncRequestHandler", i))); .Select(i => new KeyedService("IAsyncRequestHandler", i)));
builder builder
.RegisterAssemblyTypes(typeof(OrderStartedDomainEventHandler).GetTypeInfo().Assembly) .RegisterAssemblyTypes(typeof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler).GetTypeInfo().Assembly)
.Where(t => t.IsClosedTypeOf(typeof(IAsyncNotificationHandler<>))) .Where(t => t.IsClosedTypeOf(typeof(IAsyncNotificationHandler<>)))
.AsImplementedInterfaces(); .AsImplementedInterfaces();

View File

@ -41,7 +41,6 @@ namespace Ordering.API.Infrastructure.Middlewares
private async Task ProcessConfigRequest(HttpContext context) private async Task ProcessConfigRequest(HttpContext context)
{ {
int i = 0;
var enable = context.Request.Query.Keys.Any(k => k == "enable"); var enable = context.Request.Query.Keys.Any(k => k == "enable");
var disable = context.Request.Query.Keys.Any(k => k == "disable"); var disable = context.Request.Query.Keys.Any(k => k == "disable");

View File

@ -58,6 +58,10 @@
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" /> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="Application\DomainEventHandlers\OrderStartedEvent\SendEmailToCustomerWhenOrderStartedDomainEventHandler.cs" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="Dockerfile"> <None Update="Dockerfile">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>

View File

@ -33,7 +33,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.B
if (existingPayment != null) if (existingPayment != null)
{ {
AddDomainEvent(new BuyerPaymentMethodVerifiedDomainEvent(this, existingPayment, orderId)); AddDomainEvent(new BuyerAndPaymentMethodVerifiedDomainEvent(this, existingPayment, orderId));
return existingPayment; return existingPayment;
} }
else else
@ -41,7 +41,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.B
var payment = new PaymentMethod(cardTypeId, alias, cardNumber, securityNumber, cardHolderName, expiration); var payment = new PaymentMethod(cardTypeId, alias, cardNumber, securityNumber, cardHolderName, expiration);
_paymentMethods.Add(payment); _paymentMethods.Add(payment);
AddDomainEvent(new BuyerPaymentMethodVerifiedDomainEvent(this, payment, orderId)); AddDomainEvent(new BuyerAndPaymentMethodVerifiedDomainEvent(this, payment, orderId));
return payment; return payment;
} }
} }

View File

@ -6,14 +6,14 @@ using System.Text;
namespace Ordering.Domain.Events namespace Ordering.Domain.Events
{ {
public class BuyerPaymentMethodVerifiedDomainEvent public class BuyerAndPaymentMethodVerifiedDomainEvent
: IAsyncNotification : IAsyncNotification
{ {
public Buyer Buyer { get; private set; } public Buyer Buyer { get; private set; }
public PaymentMethod Payment { get; private set; } public PaymentMethod Payment { get; private set; }
public int OrderId { get; private set; } public int OrderId { get; private set; }
public BuyerPaymentMethodVerifiedDomainEvent(Buyer buyer, PaymentMethod payment, int orderId) public BuyerAndPaymentMethodVerifiedDomainEvent(Buyer buyer, PaymentMethod payment, int orderId)
{ {
Buyer = buyer; Buyer = buyer;
Payment = payment; Payment = payment;