/// <summary>
        /// 将指定的商品添加到指定客户的购物篮里。
        /// </summary>
        /// <param name="customerID">用于指代特定客户对象的全局唯一标识。</param>
        /// <param name="productID">用于指代特定商品对象的全局唯一标识。</param>
        public void AddProductToCart(Guid customerID, Guid productID, int quantity)
        {
            var user = userRepository.GetByKey(customerID);

            var shoppingCart = shoppingCartRepository.FindShoppingCartByUser(user);

            if (shoppingCart == null)
            {
                throw new DomainException("Shopping cart doesn't exist for customer {0}.", customerID);
            }
            var product          = productRepository.GetByKey(productID);
            var shoppingCartItem = shoppingCartItemRepository.FindItem(shoppingCart, product);

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppingCartItem(product, shoppingCart, quantity);
                shoppingCartItemRepository.Add(shoppingCartItem);
            }
            else
            {
                shoppingCartItem.UpdateQuantity(shoppingCartItem.Quantity + quantity);
                shoppingCartItemRepository.Update(shoppingCartItem);
            }
            Context.Commit();
        }
        public void AddProductToCart(Guid customerId, Guid productId, int quantity)
        {
            var user = _userRepository.GetByKey(customerId);

            var shoppingCart = _shoppingCartRepository.GetBySpecification(new ExpressionSpecification <ShoppingCart>(s => s.User.Id == user.Id));

            if (shoppingCart == null)
            {
                throw new DomainException("用户{0}不存在购物车.", customerId);
            }

            var product          = _productRepository.GetByKey(productId);
            var shoppingCartItem = _shoppingCartItemRepository.FindItem(shoppingCart, product);

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppingCartItem()
                {
                    Product      = product,
                    ShoopingCart = shoppingCart,
                    Quantity     = quantity
                };

                _shoppingCartItemRepository.Add(shoppingCartItem);
            }
            else
            {
                shoppingCartItem.UpdateQuantity(shoppingCartItem.Quantity + quantity);
                _shoppingCartItemRepository.Update(shoppingCartItem);
            }

            RepositorytContext.Commit();
        }
Exemplo n.º 3
0
        public void AddProductToCart(Guid customerId, Guid productId, int quantity)
        {
            var user = _userRepository.GetByKey(customerId);

            //备注:源代码此处 可以加载到shoppingCart.User的值,而这里不能,为什么?(已解决,仓储上下文必须单例)
            var shoppingCart = _shoppingCartRepository.GetBySpecification(new ExpressionSpecification <ShoppingCart>(s => s.User.Id == user.Id));

            /*调试代码*/
            //var shoppingCart1 = _shoppingCartRepository.GetBySpecification(new ExpressionSpecification<ShoppingCart>(s => s.User.Id == user.Id),s=>s.User);
            //var userInfo = shoppingCart.User;   /*User 非vitrual  不支持延时加载*/
            /*以上*/
            if (shoppingCart == null)
            {
                throw new DomainException("用户{0}不存在购物车", customerId);
            }

            var product          = _productRepository.GetByKey(productId);
            var shoppingCartItem = _shoppingCartItemRepository.FindItem(shoppingCart, product);

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppingCartItem()
                {
                    Product      = product,
                    ShoopingCart = shoppingCart,
                    Quantity     = quantity
                };

                _shoppingCartItemRepository.Add(shoppingCartItem);
            }
            else
            {
                shoppingCartItem.UpdateQuantity(shoppingCartItem.Quantity + quantity);
                _shoppingCartItemRepository.Update(shoppingCartItem);
            }
            RepositoryContext.Commit();
        }