EF封装类,供参考!

以下是我对EF DB FIRST 生成的ObjectContext类进行封装,代码如下,供参考学习:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Data.Objects.DataClasses;
  6 using ZBService.Model;
  7 using System.Linq.Expressions;
  8 
  9 namespace ZBService
 10 {
 11     public abstract class ServiceBase<T> where T:EntityObject
 12     {
 13         protected mZhaoBiaoEntities zbEntities = new mZhaoBiaoEntities();
 14 
 15         /// <summary>
 16         /// 判断是否存在
 17         /// </summary>
 18         /// <param name="whereExpr"></param>
 19         /// <returns></returns>
 20         public bool Exist(Expression<Func<T,bool>> whereExpr)
 21         {
 22             return (this.Count(whereExpr) > 0);
 23         }
 24 
 25         /// <summary>
 26         /// 获取记录数
 27         /// </summary>
 28         /// <param name="whereExpr"></param>
 29         /// <returns></returns>
 30         public int Count(Expression<Func<T, bool>> whereExpr)
 31         {
 32             return zbEntities.CreateObjectSet<T>().Where(whereExpr).Count();
 33         }
 34 
 35         /// <summary>
 36         /// 查找实体对象
 37         /// </summary>
 38         /// <param name="whereExpr"></param>
 39         /// <returns></returns>
 40         public T Find(Expression<Func<T, bool>> whereExpr)
 41         {
 42             return zbEntities.CreateObjectSet<T>().Where(whereExpr).FirstOrDefault();
 43         }
 44 
 45         /// <summary>
 46         /// 查找实体对象列表
 47         /// </summary>
 48         /// <param name="whereExpr"></param>
 49         /// <returns></returns>
 50         public IEnumerable<T> FindList<TKey>(Expression<Func<T, bool>> whereExpr, Expression<Func<T, TKey>> orderbyExpr, int orderDirection)
 51         {
 52             return this.FindList<T, TKey>(whereExpr,t=>t,orderbyExpr,orderDirection);
 53         }
 54 
 55         /// <summary>
 56         /// 查找实体对象列表
 57         /// </summary>
 58         /// <typeparam name="TResult"></typeparam>
 59         /// <typeparam name="TKey"></typeparam>
 60         /// <param name="whereExpr"></param>
 61         /// <param name="selectExpr"></param>
 62         /// <param name="orderbyExpr"></param>
 63         /// <param name="orderDirection"></param>
 64         /// <param name="returnCount"></param>
 65         /// <returns></returns>
 66         public IEnumerable<TResult> FindList<TResult,TKey>(Expression<Func<T, bool>> whereExpr, Expression<Func<T, TResult>> selectExpr,Expression<Func<TResult,TKey>> orderbyExpr,int orderDirection,int returnCount=-1)
 67         {
 68             var result = zbEntities.CreateObjectSet<T>().Where(whereExpr).Select(selectExpr);
 69             if (result != null && result.Count() > 0)
 70             {
 71                 if (returnCount > 0)
 72                 {
 73                     if (orderDirection > 0)
 74                     {
 75                         result = result.OrderByDescending(orderbyExpr).Take(returnCount);
 76                     }
 77                     else
 78                     {
 79                         result = result.OrderBy(orderbyExpr).Take(returnCount);
 80                     }
 81                 }
 82                 return result.ToList();
 83             }
 84             return null;
 85         }
 86 
 87         /// <summary>
 88         /// 分页查找实体对象列表
 89         /// </summary>
 90         /// <typeparam name="TResult"></typeparam>
 91         /// <typeparam name="TKey"></typeparam>
 92         /// <param name="whereExpr"></param>
 93         /// <param name="selectExpr"></param>
 94         /// <param name="orderbyExpr"></param>
 95         /// <param name="orderDirection"></param>
 96         /// <param name="pageSize"></param>
 97         /// <param name="pageNo"></param>
 98         /// <param name="recordCount"></param>
 99         /// <returns></returns>
100         public IEnumerable<TResult> FindListByPage<TResult,TKey>(Expression<Func<T, bool>> whereExpr, Expression<Func<T, TResult>> selectExpr,Expression<Func<TResult,TKey>> orderbyExpr,int orderDirection,int pageSize,int pageNo,out int recordCount)
101         {
102             var result = zbEntities.CreateObjectSet<T>().Where(whereExpr).Select(selectExpr);
103             recordCount = result.Count();
104 
105             if(pageNo>recordCount) pageNo=recordCount;
106             if(pageNo<=0) pageNo=1;
107 
108             if (recordCount > 0)
109             {
110                 if (recordCount > pageSize)
111                 {
112                     if (orderDirection > 0)
113                     {
114                         return result.OrderByDescending(orderbyExpr).Skip((pageNo - 1) * pageSize).Take(pageSize).ToList();
115                     }
116                     else
117                     {
118                         return result.OrderBy(orderbyExpr).Skip((pageNo - 1) * pageSize).Take(pageSize).ToList();
119                     }
120                 }
121                 else
122                 {
123                     if (orderDirection > 0)
124                     {
125                         return result.OrderByDescending(orderbyExpr).ToList();
126                     }
127                     else
128                     {
129                         return result.OrderBy(orderbyExpr).ToList();
130                     }
131                 }
132                 
133             }
134             return null;
135         }
136 
137 
138 
139 
140         /// <summary>
141         /// 增加实体
142         /// </summary>
143         /// <param name="entity"></param>
144         public virtual void Add(T entity)
145         {
146             this.ValidateEntity(entity,ValidateMode.Add);
147             zbEntities.CreateObjectSet<T>().AddObject(entity);
148         }
149 
150 
151         /// <summary>
152         /// 增加实体列表
153         /// </summary>
154         /// <param name="entities"></param>
155         public virtual void AddList(IEnumerable<T> entities)
156         {
157             var objSet = zbEntities.CreateObjectSet<T>();
158             foreach (T entity in entities)
159             {
160                 this.ValidateEntity(entity, ValidateMode.Add);
161                 objSet.AddObject(entity);
162             }
163         }
164 
165         /// <summary>
166         /// 更新已分离实体,若未分离则不需要执行该方法
167         /// </summary>
168         /// <param name="entity"></param>
169         public virtual void Update(T entity)
170         {
171             this.ValidateEntity(entity, ValidateMode.Update);
172             zbEntities.CreateObjectSet<T>().ApplyCurrentValues(entity);
173         }
174 
175         /// <summary>
176         /// 删除实体
177         /// </summary>
178         /// <param name="entity"></param>
179         public virtual void Delete(T entity)
180         {
181             this.ValidateEntity(entity, ValidateMode.Delete);
182             zbEntities.CreateObjectSet<T>().DeleteObject(entity);
183         }
184 
185         /// <summary>
186         /// 删除实体
187         /// </summary>
188         /// <param name="whereExpr"></param>
189         public virtual void Delete(Expression<Func<T, bool>> whereExpr)
190         {
191             var objSet = zbEntities.CreateObjectSet<T>();
192             T entity = objSet.Where(whereExpr).Single();
193             //this.ValidateEntity(entity, ValidateMode.Delete);
194             objSet.DeleteObject(entity);
195         }
196 
197         /// <summary>
198         /// 删除实体列表
199         /// </summary>
200         /// <param name="entities"></param>
201         public virtual void DeleteList(IEnumerable<T> entities)
202         {
203             var objSet = zbEntities.CreateObjectSet<T>();
204             foreach (T entity in entities)
205             {
206                 //this.ValidateEntity(entity, ValidateMode.Delete);
207                 objSet.DeleteObject(entity);
208             }
209         }
210 
211 
212         /// <summary>
213         /// 提交保存所有变更操作
214         /// </summary>
215         public void SubmitSave()
216         {
217             zbEntities.SaveChanges();
218         }
219 
220 
221         /// <summary>
222         /// 验证
223         /// </summary>
224         /// <param name="entity"></param>
225         /// <returns></returns>
226         protected virtual void ValidateEntity(T entity,ValidateMode mode=ValidateMode.Add)
227         {
228 
229         }
230 
231         /// <summary>
232         /// 验证模式
233         /// </summary>
234         protected enum ValidateMode
235         {
236             Add=0,
237             Update=1,
238             Delete=-1
239         }
240 
241     }
242 }

之所以声明为abstract,要求子类必需继承后才能正常使用,这里面有一个ValidateEntity方法,主用于子类在RUD前,验证实体对象数据完整性,可重写也可不重写!

更多IT相关资讯与技术文章,欢迎光临我的个人网站:http://www.zuowenjun.cn/

posted @ 2015-01-29 13:02  梦在旅途  阅读(3592)  评论(0编辑  收藏  举报