上一篇中简单分享了下ORM的设计思路。现在开始讲如何用代码来实现上篇的设计模型。
我们建2个类库来分别抽象数据库表结构关系映射和SQL增删改查操作。
打开VS2010,新建2个类库。分别起名为Model,和DAL。
Model层为数据库表结构关系映射
DAL层为 SQL增删改查操作的方法抽象封装
我们先从Model层开始。
数据库的表会包含表名,字段名称,字段类型,主键,外键等主要元素。我们在项目中为每张表建立一个Model类来抽象描述。
在Model类中我们定义常量TableName,用来描述数据库表名称。为表的字段逐一添加Model类属性,属性名和字段名相同。
由于SQL字段类型和.net数据类型不一致,我们在字段属性上添加自定义特性类DBType来描述对应的SQL字段类型。
字段会有主键,外键标识或者是虚拟字段标识。我们在字段属性上添加自定义特性类DBField来描述他们。
如:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using Model.Entities; 6 using System.Data; 7 namespace Model 8 { 9 [Serializable] 10 public class EmpInfoModel : BaseEntity 11 { 12 /// <summary>是否可以修改 13 /// </summary> 14 public const bool isCanMod = false; 15 /// <summary>数据库表名 16 /// </summary> 17 public const String TableName = "EmpInfo"; 18 public EmpInfoModel() 19 { } 20 21 private string _Id; 22 private string _Name; 23 private int? _isAllMoneyCheck; 24 private Guid? _MyGuid; 25 private Int16? _MySmallint; 26 private bool? _MyBool; 27 private string _Myntext; 28 [DBField(KeyType = DbKeyType.PK)]//主键标识 29 [DBType(SqlDBType = SqlDbType.NVarChar)]//字段对应SQLSERVER数据库字段类型 30 public virtual string Id 31 { 32 set { _Id = value; } 33 get { return _Id; } 34 } 35 36 public string Name 37 { 38 set { _Name = value; } 39 get { return _Name; } 40 } 41 42 43 public int? isAllMoneyCheck 44 { 45 set { _isAllMoneyCheck = value; } 46 get { return _isAllMoneyCheck; } 47 } 48 49 [DBType(SqlDBType = SqlDbType.UniqueIdentifier)]//字段对应SQLSERVER数据库字段类型 50 public Guid? MyGuid 51 { 52 set { _MyGuid = value; } 53 get { return _MyGuid; } 54 } 55 56 [DBType(SqlDBType = SqlDbType.SmallInt)]//字段对应SQLSERVER数据库字段类型 57 public Int16? MySmallint 58 { 59 set { _MySmallint = value; } 60 get { return _MySmallint; } 61 } 62 63 [DBType(SqlDBType = SqlDbType.Bit)]//字段对应SQLSERVER数据库字段类型 64 public bool? MyBool 65 { 66 set { _MyBool = value; } 67 get { return _MyBool; } 68 } 69 [DBType(SqlDBType = SqlDbType.NText)]//字段对应SQLSERVER数据库字段类型 70 public string Myntext 71 { 72 set { _Myntext = value; } 73 get { return _Myntext; } 74 } 75 } 76 }
我在Model类库下添加DbKeyType类,TableJoinType类,DBFieldAttribute类,DBJoinAttribute类,DBTableAttribute类,DBTypeAttribute类。
DbKeyType类为字段键值枚举类,枚举值包含字段否为主键,外键,无,和虚拟字段。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Model.Entities 7 { 8 /// <summary> 9 /// 创建人:雷旭鹏(leo) 2014-1-13 10 /// 联系方式:leixupeng823@163.com 11 /// </summary> 12 public enum DbKeyType:int 13 { 14 Filed = 0, 15 PK = 1, 16 FK = 2, 17 /// <summary>只用于承载数据 18 /// </summary> 19 DataFiled = 3 20 } 21 }
TableJoinType为表连接类型枚举类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Model.Entities 7 { 8 /// <summary> 9 /// 创建人:雷旭鹏(leo) 2014-1-13 10 /// 联系方式:leixupeng823@163.com 11 /// </summary> 12 public enum TableJoinType : int 13 { 14 INNER_JOIN = 0, 15 LEFT_JOIN = 1, 16 RIGHT_JOIN = 2 17 } 18 }
DBFieldAttribute类为字段键值特性标识类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Model.Entities 7 { 8 /// <summary>字段特性 9 /// 创建人:雷旭鹏(leo) 2014-1-13 10 /// 联系方式:leixupeng823@163.com 11 /// </summary> 12 [AttributeUsage(AttributeTargets.Property, Inherited = false)] 13 public sealed class DBFieldAttribute : Attribute 14 { 15 /// <summary>字段名称 16 /// </summary> 17 public string FieldName 18 { 19 get; 20 set; 21 } 22 /// <summary>键类型 23 /// </summary> 24 public DbKeyType KeyType 25 { 26 get; 27 set; 28 } 29 /// <summary>字段数据类型 30 /// </summary> 31 public Type PropertyType 32 { 33 get; 34 set; 35 } 36 /// <summary>构造函数 37 /// </summary> 38 public DBFieldAttribute() 39 { } 40 } 41 }
DBJoinAttribute类为关系表连接类型特性标识类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Model.Entities 7 { 8 /// <summary>链接属性 9 /// 创建人:雷旭鹏(leo) 2014-1-13 10 /// 联系方式:leixupeng823@163.com 11 /// </summary> 12 [AttributeUsage(AttributeTargets.Property, Inherited = false)] 13 public sealed class DBJoinAttribute : Attribute 14 { 15 /// <summary>主键 16 /// </summary> 17 public string PK 18 { 19 get; 20 set; 21 } 22 /// <summary>外键 23 /// </summary> 24 public string FK 25 { 26 get; 27 set; 28 } 29 /// <summary>表连接关系 30 /// </summary> 31 public TableJoinType JoinType 32 { 33 get; 34 set; 35 } 36 /// <summary>构造函数 37 /// </summary> 38 public DBJoinAttribute() 39 { } 40 } 41 }
DBTableAttribute类用于连接是标识相应表名
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Model.Entities 7 { 8 /// <summary>所属表属性 9 /// 创建人:雷旭鹏(leo) 2014-1-13 10 /// 联系方式:leixupeng823@163.com 11 /// </summary> 12 [AttributeUsage(AttributeTargets.Property, Inherited = false)] 13 public sealed class DBTableAttribute : Attribute 14 { 15 /// <summary>表名称 16 /// </summary> 17 public string TableName 18 { 19 get; 20 set; 21 } 22 /// <summary>表昵称 23 /// </summary> 24 public string TableNickName 25 { 26 get; 27 set; 28 } 29 /// <summary>构造函数 30 /// </summary> 31 public DBTableAttribute() 32 { 33 } 34 } 35 }
DBTypeAttribute类为标识字段对应SQLSERVER字段类型标识类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data; 6 namespace Model.Entities 7 { 8 [AttributeUsage(AttributeTargets.Property, Inherited = false)] 9 public sealed class DBTypeAttribute : Attribute 10 { 11 /// <summary>SQL数据库字段类型 12 /// </summary> 13 public SqlDbType SqlDBType 14 { 15 get; 16 set; 17 } 18 /// <summary>构造函数 19 /// </summary> 20 public DBTypeAttribute() 21 { 22 } 23 } 24 }
通过添加上面的类,我们现在可以对数据库表进行抽象,用对应的特性标识字段是否为主键,外键或者为虚拟字段,SQL字段于.net类型的转换关系。各Model类之间的连接关系。