Skip to content

Instantly share code, notes, and snippets.

@thangchung
Created September 7, 2018 03:54
Show Gist options
  • Save thangchung/4512fc438e98d930b41bc66057e57a16 to your computer and use it in GitHub Desktop.
Save thangchung/4512fc438e98d930b41bc66057e57a16 to your computer and use it in GitHub Desktop.
Clean Domain-driven Design article - Add Product to a Cart
public class RequestHandler : TxRequestHandlerBase<InsertItemToNewCartRequest, InsertItemToNewCartResponse>
{
private readonly ICatalogGateway _catalogGateway;
private readonly IShippingGateway _shippingGateway;
private readonly IPromoGateway _promoGateway;
public RequestHandler(
IUnitOfWorkAsync uow, IQueryRepositoryFactory qrf,
ICatalogGateway catalogGateway, IShippingGateway shippingGateway,
IPromoGateway promoGateway) : base(uow, qrf)
{
_catalogGateway = catalogGateway;
_shippingGateway = shippingGateway;
_promoGateway = promoGateway;
}
public override async Task<InsertItemToNewCartResponse> Handle(InsertItemToNewCartRequest request, CancellationToken cancellationToken)
{
var cartCommander = UnitOfWork.Repository<Domain.Cart>();
var cart = await Domain.Cart.Load()
.InsertItemToCart(request.ProductId, request.Quantity)
.CalculateCartAsync(TaxType.NoTax, _catalogGateway, _promoGateway, _shippingGateway);
await cartCommander.AddAsync(cart);
await UnitOfWork.SaveChangesAsync(cancellationToken);
return new InsertItemToNewCartResponse { Result = cart.ToDto() };
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment