自己开发轻量级ORM(二)

上一篇简单的对轻量级ORM开发开了个头。这篇主要聊下ORM框架的设计思路。

ORM本质上是对数据库操作的抽象。大体上我将其分为对数据结构的抽象和对执行方法的抽象。

我的ORM设计图:

自己开发轻量级ORM(二)

ORM框架需要完成.net数据和数据库中数据的相互转换,以及对SQL语句中经典的增删改查操作的抽象封装。

封装方法代码:

自己开发轻量级ORM(二)
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Data;
  6 using System.Data.Common;
  7 using Model.Entities;
  8 
  9 namespace DAL.ErpSqlDAL.SqlFactory
 10 {
 11     /// <summary>CRUD方法抽象
 12     /// 创建人:雷旭鹏(leo) 2014-1-13
 13     /// 联系方式:leixupeng823@163.com
 14     /// </summary>
 15     /// <typeparam name="T"></typeparam>
 16     public abstract class OperateEntity<T>:ResolveEntity<T> where T :BaseEntity
 17     {
 18         public OperateEntity(DbCmd dbCmd, string entityName, Type entityType)
 19             : base(dbCmd, entityName, entityType)
 20         {
 21         }
 22 
 23         /// <summary>添加实体
 24         /// </summary>
 25         /// <param name="entityList">要添加的内容</param>
 26         /// <returns></returns>
 27         public virtual object AddEntity(IList<T> entityList)
 28         {
 29             try
 30             {
 31                 base.SourceEntity = entityList;
 32                 base.ConditionEntity = null;
 33                 base.OperateType = OperateType.INSERT;
 34                 base.EntityToSql();
 35 
 36                 string strSql = base.CommandString;
 37 
 38                 return DBHelper.ExecuteScalar(dbCmd.Cmd, CommandType.Text, strSql, base.Parameters.ToArray());
 39             }
 40             catch (Exception ex)
 41             {
 42                 throw ex;
 43             }
 44         }
 45         /// <summary>修改实体,要修改的内容和条件一一对应
 46         /// </summary>
 47         /// <param name="entityList">要修改的内容</param>
 48         /// <param name="conditionList">条件</param>
 49         /// <returns></returns>
 50         public virtual int ModEntity(IList<T> entityList, IList<T> conditionList)
 51         {
 52             try
 53             {
 54                 base.SourceEntity = entityList;
 55                 base.ConditionEntity = conditionList;
 56                 base.OperateType = OperateType.UPDATE;
 57 
 58                 base.EntityToSql();
 59                 string strSql = base.CommandString;
 60                 return DBHelper.ExecuteNonQuery(dbCmd.Cmd, CommandType.Text, strSql, base.Parameters.ToArray());
 61             }
 62             catch
 63             {
 64                 throw;
 65             }
 66         }
 67         /// <summary>根据主键修改实体 
 68         /// </summary>
 69         /// <param name="entityList">要修改的内容和包含主键值的实体</param>
 70         /// <returns></returns>
 71         public virtual int ModEntity(IList<T> entityList)
 72         {
 73             try
 74             {
 75                 IList<T> cndEntity = new List<T>();
 76                 CreatePKConditionFromSourceEntity(entityList, cndEntity);
 77                 return ModEntity(entityList, cndEntity);
 78             }
 79             catch
 80             {
 81                 throw;
 82             }
 83         }
 84         /// <summary>删除实体 
 85         /// </summary>
 86         /// <param name="conditionList">删除的条件</param>
 87         /// <returns></returns>
 88         public virtual int DelEntity(IList<T> conditionList)
 89         {
 90             try
 91             {
 92                 base.SourceEntity = conditionList;
 93                 base.ConditionEntity = conditionList;
 94                 base.OperateType = OperateType.DELETE;
 95 
 96                 base.EntityToSql();
 97                 string strSql = base.CommandString;
 98                 return DBHelper.ExecuteNonQuery(dbCmd.Cmd, CommandType.Text, strSql, base.Parameters.ToArray());
 99             }
100             catch
101             {
102                 throw;
103             }
104         }
105         /// <summary>得到实体 
106         /// </summary>
107         /// <param name="condition">查询条件</param>
108         /// <returns></returns>
109         public virtual IList<T> GetEntity(T condition)
110         {
111             try
112             {
113                 if (condition != null)
114                 {
115                     IList<T> coditionLists = new List<T>();
116                     coditionLists.Add(condition);
117 
118                     base.ConditionEntity = coditionLists;
119                     base.SourceEntity = coditionLists;
120                 }
121                 base.OperateType = OperateType.SELECT;
122 
123                 base.EntityToSql();
124                 string strSql = base.CommandString;
125                 return base.DateSetToEntity(DBHelper.GetDataSet(dbCmd.Adapter, dbCmd.Cmd, CommandType.Text, strSql, base.Parameters.ToArray()));
126             }
127             catch
128             {
129                 throw;
130             }
131         }
132         /// <summary>得到一个值 
133         /// </summary>
134         /// <param name="conditionList">查询条件</param>
135         /// <returns></returns>
136         public virtual Object GetValue(T conditionList)
137         {
138             try
139             {
140                 IList<T> coditionLists = new List<T>();
141                 coditionLists.Add(conditionList);
142 
143                 base.SourceEntity = coditionLists;
144                 base.ConditionEntity = coditionLists;
145                 base.OperateType = OperateType.SELECT;
146 
147                 base.EntityToSql();
148                 string strSql = base.CommandString;
149                 return DBHelper.ExecuteScalar(dbCmd.Cmd, CommandType.Text, strSql, base.Parameters.ToArray());
150             }
151             catch
152             {
153                 throw;
154             }
155         }
156 
157         //exce by sql
158         /// <summary>执行存储过程得到一个值 
159         /// </summary>
160         /// <param name="sqlStr">存储过程的名称</param>
161         /// <param name="Parameters">参数</param>
162         /// <returns></returns>
163         protected virtual Object GetValueByProc(string sqlStr, params DbParameter[] Parameters)
164         {
165             try
166             {
167                 return DBHelper.ExecuteScalar(dbCmd.Cmd, CommandType.StoredProcedure, sqlStr, Parameters);
168             }
169             catch
170             {
171                 throw;
172             }
173         }
174         /// <summary>执行SQL语句得到一个值 
175         /// </summary>
176         /// <param name="sqlStr">SQL语句</param>
177         /// <param name="Parameters">参数</param>
178         /// <returns></returns>
179         protected virtual Object GetValueBySql(string sqlStr, params DbParameter[] Parameters)
180         {
181             try
182             {
183                 return DBHelper.ExecuteScalar(dbCmd.Cmd, CommandType.Text, sqlStr, Parameters);
184             }
185             catch
186             {
187                 throw;
188             }
189         }
190         /// <summary>执行SQL,返回受影响的函数
191         /// </summary>
192         /// <param name="sqlStr">存储过程的名称</param>
193         /// <param name="Parameters">参数</param>
194         /// <returns></returns>
195         protected virtual int ExecuteSql(string sqlStr, params DbParameter[] Parameters)
196         {
197             try
198             {
199                 return DBHelper.ExecuteNonQuery(dbCmd.Cmd, CommandType.Text, sqlStr, Parameters);
200             }
201             catch
202             {
203                 throw;
204             }
205         }
206         /// <summary>执行存储过程,返回受影响的函数
207         /// </summary>
208         /// <param name="sqlStr">存储过程的名称</param>
209         /// <param name="Parameters">参数</param>
210         /// <returns></returns>
211         protected virtual int ExecuteProc(string sqlStr, params DbParameter[] Parameters)
212         {
213             try
214             {
215                 return DBHelper.ExecuteNonQuery(dbCmd.Cmd, CommandType.StoredProcedure, sqlStr, Parameters);
216             }
217             catch
218             {
219                 throw;
220             }
221         }
222 
223         /// <summary>通过SQL语句得到实体 
224         /// </summary>
225         /// <param name="sqlStr">SQL</param>
226         /// <returns></returns>
227         protected virtual IList<T> GetEntityBySql(string sqlStr)
228         {
229             try
230             {
231                 return base.DateSetToEntity(DBHelper.GetDataSet(dbCmd.Adapter, dbCmd.Cmd, CommandType.Text, sqlStr, null));
232             }
233             catch
234             {
235                 throw;
236             }
237         }
238         /// <summary>通过SQL语句得到DataSet 
239         /// </summary>
240         /// <param name="sqlStr">SQL</param>
241         /// <returns></returns>
242         protected virtual DataSet GetDataSetBySql(string sqlStr)
243         {
244             try
245             {
246                 return DBHelper.GetDataSet(dbCmd.Adapter, dbCmd.Cmd, CommandType.Text, sqlStr, null);
247             }
248             catch
249             {
250                 throw;
251             }
252         }
253         /// <summary>通过SQL语句得到实体 
254         /// </summary>
255         /// <param name="sqlStr">SQL</param>
256         /// <param name="Parameters">参数</param>
257         /// <returns></returns>
258         protected virtual IList<T> GetEntityBySql(string sqlStr, params DbParameter[] Parameters)
259         {
260             try
261             {
262                 return base.DateSetToEntity(DBHelper.GetDataSet(dbCmd.Adapter, dbCmd.Cmd, CommandType.Text, sqlStr, Parameters));
263             }
264             catch
265             {
266                 throw;
267             }
268         }
269         /// <summary>通过存储过程,语句得到实体 
270         /// </summary>
271         /// <param name="sqlStr">SQL</param>
272         /// <param name="Parameters">参数</param>
273         /// <returns></returns>
274         protected virtual IList<T> GetEntityByProc(string sqlStr, params DbParameter[] Parameters)
275         {
276             try
277             {
278                 return base.DateSetToEntity(DBHelper.GetDataSet(dbCmd.Adapter, dbCmd.Cmd, CommandType.StoredProcedure, sqlStr, Parameters));
279             }
280             catch
281             {
282                 throw;
283             }
284         }
285     }
286 }
View Code

 

下一篇将讲下从代码上如何实现上面的设计图。

自己开发轻量级ORM(二)

上一篇:position属性absolute与relative(转)


下一篇:轻松解决oracle11g 空表不能exp导出的问题