以下是我对EF DB FIRST 生成的ObjectContext类进行封装,代码如下,供参考学习:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using ZBService.Model;
using System.Linq.Expressions; namespace ZBService
{
public abstract class ServiceBase<T> where T:EntityObject
{
protected mZhaoBiaoEntities zbEntities = new mZhaoBiaoEntities(); /// <summary>
/// 判断是否存在
/// </summary>
/// <param name="whereExpr"></param>
/// <returns></returns>
public bool Exist(Expression<Func<T,bool>> whereExpr)
{
return (this.Count(whereExpr) > );
} /// <summary>
/// 获取记录数
/// </summary>
/// <param name="whereExpr"></param>
/// <returns></returns>
public int Count(Expression<Func<T, bool>> whereExpr)
{
return zbEntities.CreateObjectSet<T>().Where(whereExpr).Count();
} /// <summary>
/// 查找实体对象
/// </summary>
/// <param name="whereExpr"></param>
/// <returns></returns>
public T Find(Expression<Func<T, bool>> whereExpr)
{
return zbEntities.CreateObjectSet<T>().Where(whereExpr).FirstOrDefault();
} /// <summary>
/// 查找实体对象列表
/// </summary>
/// <param name="whereExpr"></param>
/// <returns></returns>
public IEnumerable<T> FindList<TKey>(Expression<Func<T, bool>> whereExpr, Expression<Func<T, TKey>> orderbyExpr, int orderDirection)
{
return this.FindList<T, TKey>(whereExpr,t=>t,orderbyExpr,orderDirection);
} /// <summary>
/// 查找实体对象列表
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="whereExpr"></param>
/// <param name="selectExpr"></param>
/// <param name="orderbyExpr"></param>
/// <param name="orderDirection"></param>
/// <param name="returnCount"></param>
/// <returns></returns>
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=-)
{
var result = zbEntities.CreateObjectSet<T>().Where(whereExpr).Select(selectExpr);
if (result != null && result.Count() > )
{
if (returnCount > )
{
if (orderDirection > )
{
result = result.OrderByDescending(orderbyExpr).Take(returnCount);
}
else
{
result = result.OrderBy(orderbyExpr).Take(returnCount);
}
}
return result.ToList();
}
return null;
} /// <summary>
/// 分页查找实体对象列表
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="whereExpr"></param>
/// <param name="selectExpr"></param>
/// <param name="orderbyExpr"></param>
/// <param name="orderDirection"></param>
/// <param name="pageSize"></param>
/// <param name="pageNo"></param>
/// <param name="recordCount"></param>
/// <returns></returns>
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)
{
var result = zbEntities.CreateObjectSet<T>().Where(whereExpr).Select(selectExpr);
recordCount = result.Count(); if(pageNo>recordCount) pageNo=recordCount;
if(pageNo<=) pageNo=; if (recordCount > )
{
if (recordCount > pageSize)
{
if (orderDirection > )
{
return result.OrderByDescending(orderbyExpr).Skip((pageNo - ) * pageSize).Take(pageSize).ToList();
}
else
{
return result.OrderBy(orderbyExpr).Skip((pageNo - ) * pageSize).Take(pageSize).ToList();
}
}
else
{
if (orderDirection > )
{
return result.OrderByDescending(orderbyExpr).ToList();
}
else
{
return result.OrderBy(orderbyExpr).ToList();
}
} }
return null;
} /// <summary>
/// 增加实体
/// </summary>
/// <param name="entity"></param>
public virtual void Add(T entity)
{
this.ValidateEntity(entity,ValidateMode.Add);
zbEntities.CreateObjectSet<T>().AddObject(entity);
} /// <summary>
/// 增加实体列表
/// </summary>
/// <param name="entities"></param>
public virtual void AddList(IEnumerable<T> entities)
{
var objSet = zbEntities.CreateObjectSet<T>();
foreach (T entity in entities)
{
this.ValidateEntity(entity, ValidateMode.Add);
objSet.AddObject(entity);
}
} /// <summary>
/// 更新已分离实体,若未分离则不需要执行该方法
/// </summary>
/// <param name="entity"></param>
public virtual void Update(T entity)
{
this.ValidateEntity(entity, ValidateMode.Update);
zbEntities.CreateObjectSet<T>().ApplyCurrentValues(entity);
} /// <summary>
/// 删除实体
/// </summary>
/// <param name="entity"></param>
public virtual void Delete(T entity)
{
this.ValidateEntity(entity, ValidateMode.Delete);
zbEntities.CreateObjectSet<T>().DeleteObject(entity);
} /// <summary>
/// 删除实体
/// </summary>
/// <param name="whereExpr"></param>
public virtual void Delete(Expression<Func<T, bool>> whereExpr)
{
var objSet = zbEntities.CreateObjectSet<T>();
T entity = objSet.Where(whereExpr).Single();
//this.ValidateEntity(entity, ValidateMode.Delete);
objSet.DeleteObject(entity);
} /// <summary>
/// 删除实体列表
/// </summary>
/// <param name="entities"></param>
public virtual void DeleteList(IEnumerable<T> entities)
{
var objSet = zbEntities.CreateObjectSet<T>();
foreach (T entity in entities)
{
//this.ValidateEntity(entity, ValidateMode.Delete);
objSet.DeleteObject(entity);
}
} /// <summary>
/// 提交保存所有变更操作
/// </summary>
public void SubmitSave()
{
zbEntities.SaveChanges();
} /// <summary>
/// 验证
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
protected virtual void ValidateEntity(T entity,ValidateMode mode=ValidateMode.Add)
{ } /// <summary>
/// 验证模式
/// </summary>
protected enum ValidateMode
{
Add=,
Update=,
Delete=-
} }
}
之所以声明为abstract,要求子类必需继承后才能正常使用,这里面有一个ValidateEntity方法,主用于子类在RUD前,验证实体对象数据完整性,可重写也可不重写!
更多IT相关资讯与技术文章,欢迎光临我的个人网站:http://www.zuowenjun.cn/