PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/Source/Samples/Shop/Bifrost.Samples.Shop.Domain/Shopping/CartHandlers.cs

#
C# | 32 lines | 28 code | 4 blank | 0 comment | 2 complexity | 6a110c1fe923ea5dd9025c6bb74b5098 MD5 | raw file
 1using Bifrost.Commands;
 2using Bifrost.Domain;
 3using Bifrost.Samples.Shop.Domain.Products;
 4
 5namespace Bifrost.Samples.Shop.Domain.Shopping
 6{
 7	public class CartHandlers : ICommandHandler
 8	{
 9		private readonly IAggregatedRootRepository<Cart> _cartRootRepository;
10		private readonly IAggregatedRootRepository<Product> _productRootRepository;
11
12		public CartHandlers(
13			IAggregatedRootRepository<Cart> cartRootRepository, 
14			IAggregatedRootRepository<Product> productRootRepository)
15		{
16			_cartRootRepository = cartRootRepository;
17			_productRootRepository = productRootRepository;
18		}
19
20
21		public void Handle(AddProductToCart productToCart)
22		{
23			var cart = _cartRootRepository.Find(productToCart.Id);
24			if( null == cart )
25			{
26				cart = Cart.Create(productToCart.Id);
27			}
28			var product = _productRootRepository.Get(productToCart.ProductId);
29			cart.AddProduct(product);
30		}
31	}
32}