三、定义主从实体基类
定义主从实体基类的原因是我希望在类的定义时,可以很明确的了解类之间的主从关系。
3.1定义主表基类
定义PrimaryDataEntityBase |
/// <summary> /// 描述主从表的主表的数据实体 /// </summary> /// <typeparam name="ID">主表实体的主键ID</typeparam> /// <typeparam name="P">主表</typeparam> /// <typeparam name="F">从表</typeparam> public abstract class PrimaryDataEntityBase<ID, P, F> : EntityBase<P, ID> where P : PrimaryDataEntityBase<ID, P, F> where F : ForeignDataEntityBase<ID, P, F> { /// <summary> /// PrimaryDataEntityBase的默认构造函数 /// </summary> public PrimaryDataEntityBase() {
}
/// <summary> /// 按table的第一行数据进行属性的初始化 /// </summary> /// <param name="table"></param> public PrimaryDataEntityBase(System.Data.DataTable table) : this(table, 0) {
}
/// <summary> /// 按table的指定行数据进行属性的初始化 /// </summary> /// <param name="table"></param> /// <param name="indexRow"></param> public PrimaryDataEntityBase(System.Data.DataTable table, int indexRow) : base(table, indexRow) {
}
/// <summary> /// 装载当前从表的详细项 /// </summary> protected abstract List<F> LoadDetailedItems();
/// <summary> /// 存放外键表的数据项目的集合 /// </summary> protected List<F> items = new List<F>();
/// <summary> /// 获取外键表数据的集合 /// </summary> public List<F> DetailedItems { get { return LoadDetailedItems(); } }
/// <summary> /// 返回外键表的数据项目数量 /// </summary> public int DetailedItemCount { get { return items.Count; } }
/// <summary> /// 将一个外键实体加入集合 /// </summary> /// <param name="item"></param> /// <returns></returns> public abstract void Add(F item);
/// <summary> /// 从集合中移除一个外键实体 /// </summary> /// <param name="item"></param> public abstract void Remove(F item);
/// <summary> /// 从集合中移除一个外键实体 /// </summary> /// <param name="index"></param> public abstract void RemoveAt(int index);
/// <summary> /// 返回或设置匹配索引的订单详细项 /// </summary> /// <param name="index"></param> /// <returns></returns> public abstract F this[int index] { set; get; } } |
3.2定义从表基类
定义ForeignDataEntityBase |
/// <summary> /// 描述主从表的从表的数据实体 /// </summary> /// <typeparam name="ID">从表实体的主键ID</typeparam> /// <typeparam name="P">主表</typeparam> /// <typeparam name="F">从表</typeparam> public abstract class ForeignDataEntityBase<ID, P, F> : EntityBase<F, ID> where P : PrimaryDataEntityBase<ID, P, F> where F : ForeignDataEntityBase<ID, P, F> { /// <summary> /// ForeignDataEntityBase的默认构造函数 /// </summary> public ForeignDataEntityBase() {
}
/// <summary> /// 按table的第一行数据进行属性的初始化 /// </summary> /// <param name="table"></param> public ForeignDataEntityBase(System.Data.DataTable table) : this(table, 0) {
}
/// <summary> /// 按table的指定行数据进行属性的初始化 /// </summary> /// <param name="table"></param> /// <param name="indexRow"></param> public ForeignDataEntityBase(System.Data.DataTable table, int indexRow) : base(table, indexRow) {
}
/// <summary> /// 对应主键实体 /// </summary> [System.ComponentModel.DataObjectField(false, false, false)] public P RelationOrder { set; get; } } |
3.3使用主从表基类
/// <summary> /// 客户委托单 /// </summary> [System.ComponentModel.DataObject(true)] public class BusinessOrder : LogisticsOrderBase<BusinessOrder, BusinessOrderItem> { } /// <summary> /// 委托单详细 /// </summary> [System.ComponentModel.DataObject(true)] public class BusinessOrderItem : DetailedItemBase<BusinessOrder, BusinessOrderItem> { } |
现在我们的类在定义的时候,就可以非常明确的描述了主从实体的关系,并拥有了数据自动属性装载的能力。