做一个OA系统
项目架构
三层架构+Spring.Net+抽象工厂
数据层创建
- 用EF连接已建好的数据库OA中的Userinfo表
- 在IDAL层新建IBaseDAL接口并引用MODEL层
public interface IBaseDAL<T> where T : class, new()
//增加数据
T AddEntity(T entity);
//删除数据
bool DelteEntity(T entity);
//查询数据
IQueryable<T>LoadEntities(System.Linq.Expressions.Expression<Func<T,bool>> whereLambda);
//修改数据
bool UpdateEntity(T entity);
/// <summary>
/// 获取分页
/// </summary>
/// <typeparam name="s"></typeparam>
/// <param name="PageSize">一页页数</param>
/// <param name="PageIndex">当前页</param>
/// <param name="totalCount">总数</param>
/// <param name="whereLambda">筛选条件</param>
/// <param name="orderbyLambda">排序条件</param>
/// <param name="isAsc"></param>
/// <returns></returns>
IQueryable<T>LoadPageEntities<s>(int PageSize, int PageIndex, out int totalCount, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, s>> orderbyLambda, bool isAsc);
- 其它接口继承基接口IBaseDAL
- 在DAL层新建BaseDAL类
- 引用EF所需要的dll组件,可先建一个ADO数据模型来引入组件,再删掉ADO模型
public class BaseDAL<T>where T :class,new()
{
private OAEntities dbContext = new OAEntities();
//查询数据
public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda)
{
return dbContext.Set<T>().Where<T>(whereLambda);
}
//查询分页
public IQueryable<T> LoadPageEntities<s>(int PageSize, int PageIndex, out int totalCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, s>> orderbyLambda, bool isAsc)
{
var temp = dbContext.Set<T>().Where<T>(whereLambda);
totalCount = temp.Count();
if (isAsc)
{
temp = temp.OrderBy<T, s>(orderbyLambda)
.Skip<T>((PageIndex - 1) * PageSize)
.Take<T>(PageSize);
}
else
{
temp = temp.OrderByDescending<T, s>(orderbyLambda)
.Skip<T>((PageIndex - 1) * PageSize)
.Take<T>(PageSize);
}
return temp;
}
//增加数据
public T AddEntity(T entity)
{
dbContext.Set<T>().Add(entity);
dbContext.SaveChanges();
return entity;
}
//删除数据
public bool DelteEntity(T entity)
{
dbContext.Entry<T>(entity).State = System.Data.EntityState.Deleted;
return dbContext.SaveChanges() > 0;
}
//修改数据
public bool UpdateEntity(T entity)
{
dbContext.Entry<T>(entity).State = System.Data.EntityState.Modified;
return dbContext.SaveChanges() > 0;
}
}
- 其他DAL类先继承基类再实现相应接口
-------------------到这里最底层的数据层就做好了----------------------------------