本身開發輕量級ORM(二)

上一篇簡單的對輕量級ORM開發開了個頭。這篇主要聊下ORM框架的設計思路。html

ORM本質上是對數據庫操做的抽象。大致上我將其分爲對數據結構的抽象和對執行方法的抽象。sql

個人ORM設計圖:數據庫

ORM框架須要完成.net數據和數據庫中數據的相互轉換,以及對SQL語句中經典的增刪改查操做的抽象封裝。數據結構

封裝方法代碼:框架

  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

 

下一篇將講下從代碼上如何實現上面的設計圖。ide

相關文章
相關標籤/搜索