我们有一个数据层,其中包含由数据库的输出(表/视图/procs/函数)生成的类。数据库中的表是规范化的,其设计类似于 OOP 设计(“发票”表与“文档”表具有 1:1 关系,“发票项”表与“文档项”表具有 1:1 关系“等...”。所有对/从数据库的访问都是通过存储过程(也适用于简单表)。
典型的类看起来像(很快):
public class DocumentItem {
public Guid? ItemID { get; set; }
public Guid? IDDocument { get; set; }
public DateTime? LastChange { get; set; }
}
public class InvoiceItem : DocumentItem {
public Guid? IDProduct { get; set; }
public decimal? Price { get; set; }
}
问题是,数据库表的关系类似于 OOP 中的多重继承。现在我们为每个数据库输出创建一个新类。但是每个数据库输出都是数据库中“纯”表的组合。
理想的解决方案是(恕我直言)将类转换为接口,使用接口的多个实现,然后自动实现成员(这个“表类”只有属性,属性的主体总是相同的)。
例如:公共接口 IItem { Guid? 项目 ID { 获取;放; } 约会时间?上次更改 { 获取;放; } }
public interface IDocumentItem : IItem {
Guid? IDDocument { get; set; }
}
public interface IItemWithProduct : IItem {
Guid? IDProduct { get; set; }
}
public interface IItemWithRank : IItem {
string Rank { get; set; }
}
public interface IItemWithPrice : IItem {
decimal? Price { get; set; }
}
// example of "final" item interface
public interface IStorageItem : IDocumentItem, IItemWithProduct, IItemWithRank { }
// example of "final" item interface
public interface IInvoiceItem : IDocumentItem, IItemWithProduct, IItemWithPrice { }
// the result should be a object of class which implements "IInvoiceItem"
object myInvoiceItem = SomeMagicClass.CreateClassFromInterface( typeof( IInvoiceItem ) );
该数据库包含大量表,整个解决方案由动态加载的模块(100 多个模块)组成。
您认为什么是最好的方法,如何处理?
编辑:
使用部分类是一个很好的提示,我们解决方案中的芽不能使用,因为“IDocumentItem”和“IItemWithPrice”(例如)在不同的程序集中。
现在,如果我们在“DocumentItem”表中进行更改,我们必须在所有依赖程序集中重新生成源代码。几乎没有重用(因为不能使用多重继承)。如果有几十个依赖程序集,它会非常耗时。