/// <summary> /// 将指定的商品从其所属的商品分类中移除。 /// </summary> /// <param name="product">商品。</param> /// <param name="category">分类,若为NULL,则表示从所有分类中移除。</param> public void Uncategorize(Product product, Category category = null) { Expression<Func<Categorization, bool>> specExpression; if (category == null) // ReSharper disable ImplicitlyCapturedClosure specExpression = p => p.ProductID == product.ID; // ReSharper restore ImplicitlyCapturedClosure else specExpression = p => p.ProductID == product.ID && p.CategoryID == category.ID; var categorization = _categorizationRepository.Find(Specification<Categorization>.Eval(specExpression)); if (categorization != null) _categorizationRepository.Remove(categorization); _repositoryContext.Commit(); }
/// <summary> /// 将指定的商品归类到指定的商品分类中。 /// </summary> /// <param name="product">需要归类的商品。</param> /// <param name="category">商品分类。</param> /// <returns>用以表述商品及其分类之间关系的实体。</returns> public Categorization Categorize(Product product, Category category) { if (product == null) throw new ArgumentNullException("product"); if (category == null) throw new ArgumentNullException("category"); var categorization = _categorizationRepository.Find(Specification<Categorization>.Eval(c => c.ProductID == product.ID)); if (categorization == null) { categorization = Categorization.CreateCategorization(product, category); _categorizationRepository.Add(categorization); } else { categorization.CategoryID = category.ID; _categorizationRepository.Update(categorization); } _repositoryContext.Commit(); return categorization; }
/// <summary> /// 创建商品与分类之间的关系。 /// </summary> /// <param name="product">商品实体。</param> /// <param name="category">分类实体。</param> /// <returns>描述商品与分类之间关系的实体。</returns> public static Categorization CreateCategorization(Product product, Category category) { return new Categorization(product.ID, category.ID); }
/// <summary> /// 将指定的商品从其所属的商品分类中移除。 /// </summary> /// <param name="product">商品。</param> /// <param name="category">分类,若为NULL,则表示从所有分类中移除。</param> public void Uncategorize(Product product, Category category = null) { Expression<Func<Categorization, bool>> specExpression = null; if (category == null) specExpression = p => p.ProductID == product.ID; else specExpression = p => p.ProductID == product.ID && p.CategoryID == category.ID; var categorization = categorizationRepository.Find(Specification<Categorization>.Eval(specExpression)); if (categorization != null) categorizationRepository.Remove(categorization); repositoryContext.Commit(); }