ORM映射框架总结--数据操作(四)

简介: 1.BaseEntityHelper 解析实体类特性 代码   1 /**  2  *   3  * 2009-4-17  4  *   5  *   6  * 字段的特性  7  * */  8 using System;  9 using System.

 

1.BaseEntityHelper 解析实体类特性

img_405b18b4b6584ae338e0f6ecaf736533.gif 代码
  1  /* *
  2   * 
  3   * 2009-4-17
  4   * 
  5   * 
  6   * 字段的特性
  7   *  */
  8  using  System;
  9  using  System.Collections.Generic;
 10  using  System.Linq;
 11  using  System.Text;
 12  using  System.Reflection;
 13  using  CommonData.Model;
 14 
 15  namespace  CommonData.Data
 16  {
 17       public   abstract   class  BaseEntityHelper
 18      {
 19           ///   <summary>
 20           ///  根据反射类型获得相应实体的特性
 21           ///   </summary>
 22           ///   <param name="type"> 实体类型 </param>
 23           ///   <returns></returns>
 24           public  TableInfo GetTableInfo(Type type)
 25          {
 26              TableInfo info  =  EntityTypeCache.Get(type);
 27               if  (info  ==   null )
 28              {
 29                   // 获得表类型的特性集合
 30                  TableAttribute[] tableAttribute  =  type.GetCustomAttributes( typeof (TableAttribute), false as  TableAttribute[];
 31                   // 获得列类型的特性集合
 32                  List < ColumnAttribute >  listColumnAttribute  =   new  List < ColumnAttribute > ();
 33                   // 获得主表类型的特性集合
 34                  List < LinkTableAttribute >  listLinkTableAttribute  =   new  List < LinkTableAttribute > ();
 35                   // 获得子表类型的特性集合
 36                  List < LinkTablesAttribute >  listLinktablesAttribute  =   new  List < LinkTablesAttribute > ();
 37                  
 38                   // 获得该类型实体的所有属性
 39                  PropertyInfo[] propertyInfo  =  type.GetProperties();
 40 
 41                   foreach  (PropertyInfo property  in  propertyInfo)
 42                  {
 43                       // ColumnAttribute[] columnAttribute = property.GetCustomAttributes(typeof(ColumnAttribute), false) as ColumnAttribute[];
 44                       // LinkTableAttribute[] linkTableAttribute = property.GetCustomAttributes(typeof(LinkTableAttribute),false) as LinkTableAttribute[];
 45                       // LinkTablesAttribute[] linkTablesAttribute = property.GetCustomAttributes(typeof(LinkTablesAttribute), false) as LinkTablesAttribute[];
 46 
 47                       // if (columnAttribute.Length > 0)
 48                       // {
 49                       //     listColumnAttribute.Add(columnAttribute[0]);
 50                       // }
 51                       // if (linkTableAttribute.Length > 0)
 52                       // {
 53                       //     listLinkTableAttribute.Add(listLinkTableAttribute[0]);
 54                       // }
 55                       // if (linkTablesAttribute.Length > 0)
 56                       // {
 57                       //     listLinktablesAttribute.Add(linkTablesAttribute[0]);
 58                       // }
 59                       if  (property.GetCustomAttributes( typeof (ColumnAttribute),  false ).Length  >   0 )
 60                      {
 61                          listColumnAttribute.Add(property.GetCustomAttributes( typeof (ColumnAttribute), false )[ 0 as  ColumnAttribute);
 62                      }
 63                       if  (property.GetCustomAttributes( typeof (LinkTableAttribute),  false ).Length  >   0 )
 64                      {
 65                          listLinkTableAttribute.Add(property.GetCustomAttributes( typeof (LinkTableAttribute), false )[ 0 as  LinkTableAttribute);
 66                      }
 67                       if  (property.GetCustomAttributes( typeof (LinkTablesAttribute),  false ).Length  >   0 )
 68                      {
 69                          listLinktablesAttribute.Add(property.GetCustomAttributes( typeof (LinkTablesAttribute), false )[ 0 as  LinkTablesAttribute);
 70                      }
 71                  }
 72 
 73                  info  =   new  TableInfo();
 74                  info.Table  =  tableAttribute[ 0 ];
 75                  info.Columns  =  listColumnAttribute.ToArray();
 76                  info.LinkTable  =  listLinkTableAttribute.ToArray();
 77                  info.LinkTables  =  listLinktablesAttribute.ToArray();
 78                  info.Properties  =  propertyInfo;
 79                  EntityTypeCache.InsertTableInfo(type,info);
 80              }
 81               return  info;
 82          }
 83 
 84 
 85           ///   <summary>
 86           ///  根据实体公共父接口获得特性信息
 87           ///   </summary>
 88           ///   <param name="entity"> 公共接口对象 </param>
 89           ///   <returns></returns>
 90           public  TableInfo GetTableInfo(IEntity entity)
 91          {
 92               return  GetTableInfo(entity.GetType());
 93          }
 94 
 95           ///   <summary>
 96           ///  根据实体类型获得表的特性信息
 97           ///   </summary>
 98           ///   <returns></returns>
 99           public  TableInfo GetTableInfo < T > ()
100          {
101              Type type = typeof (T);
102               return  GetTableInfo(type);
103          }
104 
105           ///   <summary>
106           ///  更具实体公共接口获得列类型特新信息
107           ///   </summary>
108           ///   <param name="entity"> 公共实体接口 </param>
109           ///   <returns></returns>
110           public  ColumnAttribute[] GetColumnAttribute(IEntity entity)
111          {
112               return  GetTableInfo(entity).Columns;
113          }
114 
115           ///   <summary>
116           ///  更具实体类型获得列类特性信息
117           ///   </summary>
118           ///   <param name="type"> 实体类类型 </param>
119           ///   <returns></returns>
120           public  ColumnAttribute[] GetColumnAttribute(Type type)
121          {
122               return  GetTableInfo(type).Columns;
123          }
124 
125           ///   <summary>
126           ///  更具泛型类型获得列类型特性信息
127           ///   </summary>
128           ///   <typeparam name="T"> 泛型类型 </typeparam>
129           ///   <returns></returns>
130           public  ColumnAttribute[] GetColumnAttribute < T > ()
131          {
132               return  GetTableInfo < T > ().Columns;
133          }
134 
135           ///   <summary>
136           ///  更具实体公共接口获得主表类型特性信息
137           ///   </summary>
138           ///   <param name="entity"> 公共实体接口 </param>
139           ///   <returns></returns>
140           public  LinkTableAttribute[] GetLinkTableAttribute(IEntity entity)
141          {
142               return  GetTableInfo(entity).LinkTable;
143          }
144 
145           ///   <summary>
146           ///  更具实体类型获得主表特性信息
147           ///   </summary>
148           ///   <param name="type"> 实体类型 </param>
149           ///   <returns></returns>
150           public  LinkTableAttribute[] GetLinkTableAttribute(Type type)
151          {
152               return  GetTableInfo(type).LinkTable;
153          }
154 
155           ///   <summary>
156           ///  更具泛型类型获得主表类型特性信息
157           ///   </summary>
158           ///   <typeparam name="T"> 泛型类型 </typeparam>
159           ///   <returns></returns>
160           public  LinkTableAttribute[] GetLinkTableAttribute < T > ()
161          {
162               return  GetTableInfo < T > ().LinkTable;
163          }
164 
165           ///   <summary>
166           ///  更具实体公共接口来获得子表类型特性信息
167           ///   </summary>
168           ///   <param name="entity"> 公共接口 </param>
169           ///   <returns></returns>
170           public  LinkTablesAttribute[] GetLinkTablesAttribute(IEntity entity)
171          {
172               return  GetTableInfo(entity).LinkTables;
173          }
174 
175 
176           ///   <summary>
177           ///  更具实体类型获得子表类型特性信息
178           ///   </summary>
179           ///   <param name="type"> 实体类型 </param>
180           ///   <returns></returns>
181           public  LinkTablesAttribute[] GetLinkTablesAttribute(Type type)
182          {
183               return  GetTableInfo(type).LinkTables;
184          }
185 
186           ///   <summary>
187           ///  更具泛型获得子表类型特性信息
188           ///   </summary>
189           ///   <typeparam name="T"> 泛型类型 </typeparam>
190           ///   <returns></returns>
191           public  LinkTablesAttribute[] GetLinkTablesAttribute < T > ()
192          {
193               return  GetTableInfo < T > ().LinkTables;
194          }
195 
196           ///   <summary>
197           ///  根据共同实体接口获得表特性信息
198           ///   </summary>
199           ///   <param name="entity"> 公共实体接口 </param>
200           ///   <returns></returns>
201           public  TableAttribute GetTableAttribute(IEntity entity)
202          {
203               return  GetTableInfo(entity).Table;
204          }
205 
206           ///   <summary>
207           ///  根据实体类型获得实体特性信息
208           ///   </summary>
209           ///   <param name="type"> 实体类型 </param>
210           ///   <returns></returns>
211           public  TableAttribute GetTableAttribute(Type type)
212          {
213               return  GetTableInfo(type).Table;
214          }
215 
216           ///   <summary>
217           ///  根据泛型类型获得实体特性信息
218           ///   </summary>
219           ///   <typeparam name="T"> 泛型类型 </typeparam>
220           ///   <returns></returns>
221           public  TableAttribute GetTableAttribute < T > ()
222          {
223               return  GetTableInfo < T > ().Table;
224          }
225 
226      }
227  }
228 

 

  BaseEntityHelper 这个类主要是为了解析实体类的特性信息。 public TableInfo GetTableInfo(Type type) 这个方法这个类中的核心,我们从Type 类型可以获得该类型的所有属性,方法和特性信息,同时我们也可以动态的从这个实体类中获得属性的值或者为这个属性赋值。反射可以保证我们在不知道实体相关信息的情况下获得实体的数据信息。这也是后面再组装sql语句赋值的核心

  同时这个类中大量重载了一小儿方法,包括泛型的重载方法。目的是为了体现程序的灵活性。

 

2.拓展类 DataExtension

img_405b18b4b6584ae338e0f6ecaf736533.gif 代码
 1  /* *
 2   * 
 3   * 2009-4-17
 4   * 
 5   * 
 6   * 拓展方法,用于添加参数
 7   *  */
 8  using  System;
 9  using  System.Collections.Generic;
10  using  System.Linq;
11  using  System.Text;
12  using  System.Data;
13 
14  namespace  CommonData.Data
15  {
16       public   static   class  DataExtension
17      {
18           ///   <summary>
19           ///  拓展方法,用于给执行语句添加参数
20           ///   </summary>
21           ///   <param name="paramenters"> 参数集合列表 </param>
22           ///   <param name="param"> 传递的参数值集合 </param>
23           public   static   void  AddRange( this  IDataParameterCollection paramenters,  params  IDataParameter[] param)
24          {
25               if  (param  !=   null )
26              {
27                   foreach  (IDbDataParameter p  in  param)
28                  {
29                      paramenters.Add(p);
30                  }
31              }
32          }
33      }
34  }
35 

 

  这个类拓展了一个借口IDataParameterCollection,.net 中的拓展方法可以说为.net 平台注入了新的血液,让程序更加生动和富有色彩。扩展方法不仅可以或者自己定义的类中的方法,而且可以更具需要扩展系统中的方法。其中这个类中的AddRange 方法是一个很好的例子,扩展了IDataParamterCollection 这个接口中的方法。扩展这份方法可以将占位符中的变量一次性赋值,而不用每次都去循环赋值。

  其实SqlParamters 这个类中是有AddRange 这个方法的,可以一次为占位符添加数组和集合,但是现在这个是它的父接口,没有提供这样的方法,因此我们扩展了这个方法。还要说明的是 这里使用接口而没有直接使用SqlParameters 这个类是因为当程序需要扩展的时候,我们不局限于sql数据库(可能是Oracle 数据库),我们只需要实现相应的接口即可,实现的过程可以任意调整,这样程序才具有灵活性。本人写的这个只是实现了sql server 数据

相关文章
|
SQL 关系型数据库 API
ORM对象关系数据库映射
ORM对象关系数据库映射
ORM对象关系数据库映射
|
数据库连接 数据库
ORM映射框架总结--数据操作(五)
1.数据库加载驱动和操作接口 IDbProvider 代码  1 /** 2  *  3  * 2009-4-22 4  *  5  *  6  * 数据库操作加载驱动接口,  7  * 提供了数据库操作的各种命令  8  * */ 9 using System;10 using System.
772 0
|
存储 SQL 数据库
ORM映射框架总结--数据操作(七)
2. 数据库操作实现类 SqlHelper 代码 /** *  * 2009-4-22 *  *  * 数据库操作的公共类 * */using System;using System.Collections.
672 0
|
SQL 存储 数据库
ORM映射框架总结--数据操作(六)
1. 数据库操作接口 IDbHelper 代码 /** *  * 2009-4-22 *  *  * 提供各种数据库操作方法以及实体类操作方法 * */using System;using System.
632 0
|
SQL 存储 .NET
ORM映射框架总结--数据库操作库(精修版)
1.       ORM数据库操作原理 前面已经介绍过了个人ORM映射框架中的三个核心库: 实体—数据库 映射特性关系: http://www.cnblogs.com/qingyuan/archive/2010/04/02/1702998.
1364 0
|
Web App开发
ORM映射框架总结--Excel 操作
  在很多时候,我们需要将查询的数据做成报表统计,然后生成Excel文档格式的。再此提供了将DataTable 数据导出excel 的方法   代码   1 /**  2  *   3  * 2009-5-2  4  *   5  *   6  * 将DataTable导出为excel文件  7  * */  8 using System;  9 using System.
879 0
|
移动开发 监控 .NET
ORM映射框架总结--日志处理
  在我们做项目的过程中,日志跟踪异常是非常必要的,当程序发布到服务器上时,如果出现异常直接抛出给用户这是非常不友好的。对于不懂程序的用户来说,这回让人感觉莫名其妙,对于那些程序高手,可能就是攻破这个网站的关键。
695 0
|
SQL 安全 数据库
ORM映射框架总结--代码生成器
年前发布了一些文章,是关于.NET数据操作(点击查看)的。刚开始学习编程的时候,总感觉Java中的Hibernate 功能好强大,现在也不可否认它的确强大,特别是它在数据关系处理上,却是那样的让人称叹。
1233 0
|
数据库
ORM映射框架总结--映射桥梁
1.       感言 写博客之前先自我吹嘘一下,给这些文章来些自我介绍。 半年前自己借用了5个多月的业务时间写了一个个人ORM映射框架。在之前的博 客中也有过写过该框架的相关介绍。半年前的那个ORM只不过是自己想象的关系映射的一个雏形,那一段曾经让自己骄傲过得代码的确存在着太多的问题,但是我始终没有放弃过对它的修改。
811 0
|
存储 缓存 数据库
ORM映射框架总结--实体分析器
1.       什么是数据分析器 前面一篇文章讲到过数据分析器,什么是数据分析器。其实很容易理解,就是对数据进行分析采集的一个工具,说白了就是一个小程序,在本ORM框架中对实体对象进行必要的数据分析,获得实体对象的各种信息缓存,以便在后续的工作中直接提取数据。
742 0