在应用程序中我有两个模型类别和产品。显示如下
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public int CategoryId { get; set; }
}
public class Category
{
public Category()
{
Products = new List<Product>();
}
public int Id { get; set; }
public string Name { get; set; }
public List<Product> Products { get; set; }
}
我将数据添加到类别和产品列表中,如下所示。
List<Category> data = new List<Category>();
data.Add(new Category { Id = 1, Name = "Plastic"});
data.Add(new Category { Id = 2, Name = "Metal"});
data.Add(new Category { Id = 3, Name = "Paper" });
foreach (var item in data)
{
switch (item.Id)
{
case 1:
item.Products.Add(new Product { Id = 1, Name = "Doll", CategoryId = item.Id });
item.Products.Add(new Product { Id = 2, Name = "Pen", CategoryId = item.Id });
break;
case 2:
item.Products.Add(new Product { Id = 3, Name = "Nail", CategoryId = item.Id });
item.Products.Add(new Product { Id = 4, Name = "Key", CategoryId = item.Id });
break;
case 3:
item.Products.Add(new Product { Id = 5, Name = "Book", CategoryId = item.Id });
break;
default:
item.Products.Add(new Product { Id = 0, Name = null });
break;
}
}
还有其他方法可以用最少的代码行编写吗?如果有人为此提出另一种解决方案,我将不胜感激。
答案 0 :(得分:2)
您可以提供流畅的构建器方法来将产品添加到类别(它可以是扩展方法或类别的实例方法)。这些方法应返回添加产品的类别。用法看起来像(将产品添加到类别有三种不同的选项):
var data = new List<Category> {
new Category { Id = 1, Name = "Plastic" }
.WithProducts(
new Product { Id = 1, Name = "Doll" }
new Product { Id = 2, Name = "Pen" }
),
new Category { Id = 3, Name = "Paper" }
.WithProduct( new Product { Id = 2, Name = "Book" } ),
new Category { Id = 2, Name = "Plastic" }
.WithProduct(3, "Nail")
.WithProduct(4, "Key"),
};
流畅的扩展方法(你可以使用all,或者选择你更喜欢的语法):
public static Category WithProduct(this Category category, int id, string product)
{
category.Products.Add(new Product { Id = id, Name = name, CategoryId = category.Id });
return category;
}
public static Category WithProduct(this Category category, Product product)
{
product.CategoryId = category.Id;
category.Products.Add(product);
return category;
}
public static Category WithProducts(this Category category, params Product[] products)
{
foreach(var product in products)
{
product.CategoryId = category.Id;
category.Products.Add(product);
};
return category;
}
请注意,将产品添加到类别时,系统会自动设置类别ID。
进一步说明:您的设计存在一个问题 - 当您公开类别Products
和类别ID以进行修改时,有人可能会将其他类别的产品添加到产品列表中:
var plastic = new Category { Id = 1, Name = "Plastic"};
plastic.Products.Add(new Product {Id = 1, Name = "Doll", CategoryId = 1});
plastic.Products.Add(new Product {Id = 5, Name = "Nail", CategoryId = 2}); // plastic nail
我至少会考虑禁用类别产品的直接修改,并在那里移动产品的设置类别ID:
private List<Product> products = new List<Product>();
public IEnumerable<Product> Products => products;
public void AddProduct(Product product)
{
// probably check if product was not added to some other category
product.CategoryId = Id;
products.Add(product);
}
答案 1 :(得分:1)
由于它处理对象创建,所以没有什么会明显缩短,所以你必须进行这些新的调用。你可以做点像......
在您的Product class上创建一个名为Create:
的静态方法public static Product Create(int id, string name, Category cat)
{
return new Product { Id = id, Name = name, cat.Id };
}
然后而不是写作:
item.Products.Add(new Product { Id = 1, Name = "Doll", CategoryId = item.Id });
写:
item.Products.Add(Product.Create(1, "Doll", item);
答案 2 :(得分:0)
我会做类似于谢尔盖别列佐夫斯基建议的事情,但我也会在两个类中添加构造函数。扩展方法很好,但是,您也可以为Category
类创建一个公共方法。
这是输出:
var categories = new List<Category>
{
new Category(1, "Plastic").WithProduct(1, "Doll").WithProduct(2, "Pen"),
new Category(2, "Metal").WithProduct(3, "Nail").WithProduct(4, "Key"),
new Category(3, "Paper").WithProduct(5, "Book")
};
这是修改后的Product
类:
public class Product
{
public Product(int id, string name, int categoryId)
{
Id = id;
Name = name;
CategoryId = categoryId;
}
public int Id { get; set; }
public string Name { get; set; }
public int CategoryId { get; set; }
}
这是修改后的Category
类:
public class Category
{
public Category(int id, string name)
{
Id = id;
Name = name;
Products = new List<Product>();
}
public int Id { get; set; }
public string Name { get; set; }
public List<Product> Products { get; set; }
public Category WithProduct(int id, string name)
{
Products.Add(new Product(id, name, Id));
return this;
}
}