Domain Event Handlers refactored with some missing cases

This commit is contained in:
Cesar De la Torre 2017-03-18 22:01:05 -07:00
parent 3ed136b00f
commit aee1ac6a06
6 changed files with 48 additions and 24 deletions

View File

@ -7,7 +7,8 @@ using System.Threading.Tasks;
namespace Ordering.API.Application.DomainEventHandlers.BuyerAndPaymentMethodVerified namespace Ordering.API.Application.DomainEventHandlers.BuyerAndPaymentMethodVerified
{ {
public class UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler : IAsyncNotificationHandler<BuyerAndPaymentMethodVerifiedDomainEvent> public class UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler
: IAsyncNotificationHandler<BuyerAndPaymentMethodVerifiedDomainEvent>
{ {
private readonly IOrderRepository<Order> _orderRepository; private readonly IOrderRepository<Order> _orderRepository;
private readonly ILoggerFactory _logger; private readonly ILoggerFactory _logger;

View File

@ -3,10 +3,22 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediatR;
using Ordering.Domain.Events;
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
{ {
public class SendEmailToCustomerWhenOrderStartedDomainEventHandler public class SendEmailToCustomerWhenOrderStartedDomainEventHandler
//: IAsyncNotificationHandler<OrderStartedDomainEvent>
{ {
//TO DO public SendEmailToCustomerWhenOrderStartedDomainEventHandler()
{
}
//public async Task Handle(OrderStartedDomainEvent orderNotification)
//{
// //TBD - Send email logic
//}
} }
} }

View File

@ -8,7 +8,8 @@ using System.Threading.Tasks;
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
{ {
public class ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler : 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;
@ -21,33 +22,34 @@ namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));
} }
public async Task Handle(OrderStartedDomainEvent orderNotification) public async Task Handle(OrderStartedDomainEvent orderStartedEvent)
{ {
var cardTypeId = orderNotification.CardTypeId != 0 ? orderNotification.CardTypeId : 1; var cardTypeId = (orderStartedEvent.CardTypeId != 0) ? orderStartedEvent.CardTypeId : 1;
var buyerGuid = _identityService.GetUserIdentity(); var userGuid = _identityService.GetUserIdentity();
var buyer = await _buyerRepository.FindAsync(buyerGuid);
if (buyer == null) var buyer = await _buyerRepository.FindAsync(userGuid);
bool buyerOriginallyExisted = (buyer == null) ? false : true;
if (!buyerOriginallyExisted)
{ {
buyer = new Buyer(buyerGuid); buyer = new Buyer(userGuid);
} }
var paymentMethod = buyer.VerifyOrAddPaymentMethod(cardTypeId, buyer.VerifyOrAddPaymentMethod(cardTypeId,
$"Payment Method on {DateTime.UtcNow}", $"Payment Method on {DateTime.UtcNow}",
orderNotification.CardNumber, orderStartedEvent.CardNumber,
orderNotification.CardSecurityNumber, orderStartedEvent.CardSecurityNumber,
orderNotification.CardHolderName, orderStartedEvent.CardHolderName,
orderNotification.CardExpiration, orderStartedEvent.CardExpiration,
orderNotification.Order.Id); orderStartedEvent.Order.Id);
_buyerRepository.Add(buyer); var buyerUpdated = buyerOriginallyExisted ? _buyerRepository.Update(buyer) : _buyerRepository.Add(buyer);
await _buyerRepository.UnitOfWork await _buyerRepository.UnitOfWork
.SaveEntitiesAsync(); .SaveEntitiesAsync();
_logger.CreateLogger(nameof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler)).LogTrace($"A new payment method has been successfully added for orderId: {orderNotification.Order.Id}."); _logger.CreateLogger(nameof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler)).LogTrace($"Buyer {buyerUpdated.Id} and related payment method were validated or updated for orderId: {orderStartedEvent.Order.Id}.");
} }
} }
} }

View File

@ -18,11 +18,13 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Autof
builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly) builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly)
.AsImplementedInterfaces(); .AsImplementedInterfaces();
// Register all the Command classes (they implement IAsyncRequestHandler) in assembly holding the Commands
builder.RegisterAssemblyTypes(typeof(CreateOrderCommand).GetTypeInfo().Assembly) builder.RegisterAssemblyTypes(typeof(CreateOrderCommand).GetTypeInfo().Assembly)
.As(o => o.GetInterfaces() .As(o => o.GetInterfaces()
.Where(i => i.IsClosedTypeOf(typeof(IAsyncRequestHandler<,>))) .Where(i => i.IsClosedTypeOf(typeof(IAsyncRequestHandler<,>)))
.Select(i => new KeyedService("IAsyncRequestHandler", i))); .Select(i => new KeyedService("IAsyncRequestHandler", i)));
// Register all the Domain Event Handler classes (they implement IAsyncNotificationHandler<>) in assembly holding the Domain Events
builder builder
.RegisterAssemblyTypes(typeof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler).GetTypeInfo().Assembly) .RegisterAssemblyTypes(typeof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler).GetTypeInfo().Assembly)
.Where(t => t.IsClosedTypeOf(typeof(IAsyncNotificationHandler<>))) .Where(t => t.IsClosedTypeOf(typeof(IAsyncNotificationHandler<>)))

View File

@ -9,7 +9,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.B
public interface IBuyerRepository<T> : IRepository<T> where T : IAggregateRoot public interface IBuyerRepository<T> : IRepository<T> where T : IAggregateRoot
{ {
Buyer Add(Buyer buyer); Buyer Add(Buyer buyer);
Buyer Update(Buyer buyer);
Task<Buyer> FindAsync(string BuyerIdentityGuid); Task<Buyer> FindAsync(string BuyerIdentityGuid);
} }
} }

View File

@ -40,6 +40,13 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositor
} }
public Buyer Update(Buyer buyer)
{
return _context.Buyers
.Update(buyer)
.Entity;
}
public async Task<Buyer> FindAsync(string identity) public async Task<Buyer> FindAsync(string identity)
{ {
var buyer = await _context.Buyers var buyer = await _context.Buyers