今天在这里写一套简单的框架,主要用于一些小型的项目,用于数据的增删改查,通用分页查询等操作.所以功能并不强大,而且笔者也是新手,这里面也难免会有bug,忘读者朋友提醒.
现在我们进入正题吧,想要写一套通用的框架,反射是必须要熟知的.至少你需要知道如何通过反射获取属性和值.然后我们自定义一些特性,用于和数据库的连接.
完成框架的制作,我们分步骤来说
1.编写自定义特性
2.通过发射,拼接字符串,用于数据操作
3.完善框架
好,我们先编写自定义特性,创建2.0(因为2.0可以向上兼容,为了实现通用,习惯使用4.0的朋友们,辛苦你们一下了)类库,我的项目名称为ORMAttributes,创建完成之后,我们创建我们的特性,添加cs文件,取名为AttributeTF
这个类需要继承Attribute,首先我们创建特性BindTableAttribute,用于标识类与数据库表之间的关系
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 /// <summary> 2 /// 表特性 3 /// </summary> 4 /// 特性用于class类,一个类中使用只能出现一次 5 /// 6 [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] 7 public class BindTableAttribute : Attribute 8 { 9 public BindTableAttribute() 10 : base() 11 { 12 13 } 14 15 public BindTableAttribute(string name,Type t) 16 { 17 _name = name; 18 _ct = t; 19 20 new TabelInfo(name,t,ColAdd); 21 } 22 23 private Type _ct; 24 25 public Type Ct 26 { 27 get { return _ct; } 28 set { _ct = value; } 29 } 30 31 private string _name = null; 32 public string name 33 { 34 get { return _name; } 35 } 36 37 private bool _colAdd = false; 38 public bool ColAdd 39 { 40 get { return _colAdd; } 41 set { _colAdd = value; 42 ManageTabel._tabels[_name].ColAdd = value; 43 } 44 } 45 }
_ct:使用记录当前这个类的实体(其实可以不使用,具体原因我后面会说到)
_name:记录名称,也就是用于对应的数据库表名称
_colAdd:是否自动添加列(这是一个辅助的功能,主要用于实体类添加字段后,对应的数据库中是否同样添加列)
接着我们创建BindFiledAttribute,用于标识属性与数据库表中列之间的关系
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 /// <summary> 2 /// 类特性 3 /// </summary> 4 /// 特性作用于属性/字段上.且只能出现一次 5 [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] 6 public class BindFiledAttribute : Attribute 7 { 8 public BindFiledAttribute() 9 : base() 10 { 11 12 } 13 14 public BindFiledAttribute(string name) 15 { 16 _strName = name; 17 } 18 19 private string _strName = null; 20 21 public string StrName 22 { 23 get { return _strName; } 24 } 25 26 27 private string _type = null; 28 29 public string Type 30 { 31 get { return _type; } 32 set { _type = value; } 33 } 34 35 private string _readFormat = null; 36 37 public string ReadFormat 38 { 39 get { return _readFormat; } 40 set { _readFormat = value; } 41 } 42 43 private string _writeFormat = null; 44 45 public string WriteFormat 46 { 47 get { return _writeFormat; } 48 set { _writeFormat = value; } 49 } 50 51 private int _index = -1; 52 53 public int Index 54 { 55 get { return _index; } 56 set { _index = value; } 57 } 58 59 private bool _isKey = false; 60 61 public bool IsKey 62 { 63 get { return _isKey; } 64 set { _isKey = value; 65 } 66 } 67 68 private bool _isIdentity = false; 69 70 public bool IsIdentity 71 { 72 get { return _isIdentity; } 73 set { _isIdentity = value; } 74 } 75 76 }
_strName:表示列的名称
_type:数据类型,例如:varchar(100)...
_readFormat:读取时样式(这个不用在意,笔者发现,这个完全可以返回由程序员自己完成,意义不大)
_writeFormat:保存时样式(同上)
_index:序列号,新增时比较实用,同样可以提高效率(笔者解决了新增时顺序的问题,后面有详细介绍)
_isKey:是否是主键列
is_IsIdentity:是否为自动增涨列
ok,,这样我们的自定义特性也就完成了,,下面我们在model层的实体类上标识下我们所写的特性吧
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 [BindTableAttribute("MyFromWork", typeof(MyFromWork), ColAdd = true)] 2 public class MyFromWork { 3 private int _id; 4 [BindFiledAttribute("id",IsIdentity=true, IsKey = true, Type = "int")] 5 public int Id 6 { 7 get { return _id; } 8 set { _id = value; } 9 } 10 11 private string _name; 12 [BindFiledAttribute("name", Type = "varchar(50)")] 13 public string Name 14 { 15 get { return _name; } 16 set { _name = value; } 17 } 18 19 private string _author; 20 21 [BindFiledAttribute("author", Type = "varchar(50)")] 22 public string Author 23 { 24 get { return _author; } 25 set { _author = value; } 26 } 27 28 private string _remarks; 29 [BindFiledAttribute("remarks", Type = "nvarchar(500)")] 30 public string Remarks 31 { 32 get { return _remarks; } 33 set { _remarks = value; } 34 }
怎么样,是不是很轻松呢,好,我们继续向下说
特性已经出现了,那么我们必定需要有一个地方来存储我们所标识的实体对应的值,所以笔者在这定义了TabelInfo和FileInfo,分别来记录BindTableAttribute和BindFiledAttribute.
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
public class FileInfo { public FileInfo() : base() { } /// <summary> /// 字段名 /// </summary> private string _strName; public string StrName { get { return _strName; } set { _strName = value; } } /// <summary> /// 数据类型(数据库中) /// </summary> private string _type; public string Type { get { return _type; } set { _type = value; } } /// <summary> /// 展现时字符串样式 /// </summary> private string _readFormat; public string ReadFormat { get { return _readFormat; } set { _readFormat = value; } } /// <summary> /// 保存时字符格式 /// </summary> private string _writeFormat; public string WriteFormat { get { return _writeFormat; } set { _writeFormat = value; } } /// <summary> /// 序列号 /// </summary> private int _index=-1; public int Index { get { return _index; } set { _index = value; } } /// <summary> /// 是否作为条件使用 /// </summary> private bool _isKey; public bool IsKey { get { return _isKey; } set { _isKey = value; } } /// <summary> /// 是否为自动增涨列 /// </summary> private