我需要保存/更新单个或实体集合。我已经写了一个相同的课程。在我的例子中,我只使用了两个实体,它们被部分保存,也保存为整体。但实际情况可能会有更多实体:
public class Supplier
{
}
public class Product
{
}
public class Data
{
public Product prod { get; set; }
public Supplier supp { get; set; }
}
//保存实体的相应方法
public static class ProductFactory
{
public static void Save(Product prod)
{
//Save Product table
}
}
public static class SupplierFactory
{
public static void Save(Supplier supp)
{
//Save Supplier table
}
}
//Method to Save All ENTITIES
public static class DataFactory
{
public static void Save(Data data)
{
//Save Data which Consists Product and Supplier
ProductFactory.Save(data.prod);
SupplierFactory.Save(data.supp);
}
}
//Implemention of Save
Product prod = new Product() ;
Supplier supp=new Supplier();
Data data=new Data() ;
//Saving Product individually
ProductFactory.Save(prod);
//Saving Supplier individually
SupplierFactory.Save(supp);
//Saving Data which Consists Supplier and Product
DataFactory.Save(data);
有人会建议更好的设计吗?产品/供应商可能会有更多实体。
答案 0 :(得分:1)
查看存储库模式,最终会得到“存储库”类,它们将处理所有CRUD操作。将所有CRUD操作放在不同的层中将有助于您以后使用依赖注入等事项。