ASP.Net MVC OA项目笔记<二>
1.1.0 创建数据层
1.1.1 CZBK.ItcastOA.IDAL 引用 CZBK.ItcastOA.Model
1.2.1 给IDAL添加一个接口IUserInfoDal
里面写增删改查分页的接口方法
public interface IUserInfoDal { //查 IQueryable<UserInfo> LoadEntities(System.Linq.Expressions.Expression<Func<UserInfo,bool>> whereLambda); //分页 //<s>方法的泛型,调这个方法时,传递这个类型 IQueryable<UserInfo> PageLoadEntities<s>(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<UserInfo,bool>>whereLambda, System.Linq.Expressions.Expression<Func<UserInfo, s>>orderbyLambda, bool isAsc); //增 UserInfo AddEntity(UserInfo entity); //删 bool EditEntity(UserInfo entity); //改 bool DeleteEntity(UserInfo entity); }
1.3.1 给IDAL添加一个接口IBaseDal,并把IUserInfoDal的代码剪切过去,UserInfo替换成T
public interface IBaseDal<T>where T:class,new() { //查 IQueryable<T> LoadEntities(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda); //分页 //<s>方法的泛型,调这个方法时,传递这个类型 IQueryable<T> PageLoadEntities<s>(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, s>> orderbyLambda, bool isAsc); //增 T AddEntity(T entity); //删 bool EditEntity(T entity); //改 bool DeleteEntity(T entity); }
1.4.1 让IUserInfoDal接口继承IBaseDal,IUserInfoDal接口中只定义它特有的方法
public interface IUserInfoDal:IBaseDal<UserInfo> { //定义自己特有的方法 }
2.1.1 CZBK.ItcastOA.DAL引用 IDAL和Model,添加一个类UserInfoDal
2.2.1 UserInfoDal继承并实现IUserInfoDal
2.3.1 先拿到操作数据的类 OAEntities
2.3.2 创建一个数据操作对象 Db,但是Db.UserInfo.点不出来
因为DAL没有引入MVC
2.4.1 用新建空的实体模型的方法引入EF
2.4.2 引入了下面这几个文件,可以删除掉生成的Model1.cs,这时候可以使用Db了
2.5.1 UserInfoDal实现增删改查分页的方法
public class UserInfoDal : IUserInfoDal { OAEntities Db = new OAEntities(); /// <summary> /// 新增 /// </summary> /// <param name="entity"></param> /// <returns></returns> public UserInfo AddEntity(UserInfo entity) { Db.UserInfo.Add(entity); Db.SaveChanges(); return entity; } /// <summary> /// 删除 /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool DeleteEntity(UserInfo entity) { //追加到ef上,打上删除标记,再保存 Db.Entry<UserInfo>(entity).State = System.Data.Entity.EntityState.Deleted; return Db.SaveChanges() > 0; } /// <summary> /// 更新 /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool EditEntity(UserInfo entity) { //追加到ef上,打上编辑标记,再保存 Db.Entry<UserInfo>(entity).State = System.Data.Entity.EntityState.Modified; return Db.SaveChanges() > 0; } /// <summary> /// 查询过滤 /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> public IQueryable<UserInfo> LoadEntities(Expression<Func<UserInfo, bool>> whereLambda) { return Db.UserInfo.Where(whereLambda); } /// <summary> /// 分页 /// </summary> /// <typeparam name="s"></typeparam> /// <param name="pageIndex">页码</param> /// <param name="pageSize">每页数量</param> /// <param name="totalCount">总数</param> /// <param name="whereLambda">过滤条件</param> /// <param name="orderbyLambda">排序条件</param> /// <param name="isAsc"></param> /// <returns></returns> public IQueryable<UserInfo> PageLoadEntities<s>(int pageIndex, int pageSize, out int totalCount, Expression<Func<UserInfo, bool>> whereLambda, Expression<Func<UserInfo, s>> orderbyLambda, bool isAsc) { var temp = Db.UserInfo.Where(whereLambda); totalCount = temp.Count(); if(isAsc)//正序 { //例:pageIndex=3,pageSize=15 //正序排列后,跳过第3页前的数据(前2页*15),剩下的只读取15条 temp = temp.OrderBy<UserInfo, s>(orderbyLambda).Skip<UserInfo>((pageIndex - 1) * pageSize).Take<UserInfo>(pageSize); } else//倒序 { temp = temp.OrderByDescending<UserInfo, s>(orderbyLambda).Skip<UserInfo>((pageIndex - 1) * pageSize).Take<UserInfo>(pageSize); } return temp; } }
2.6.1 每个页面都要实现增删改查分页的方法,所以可以新建一个BaseDal,把它们剪切到这里来,把公用方法的具体实现封装起来
把UserInfo都替换成T
Db.T.Add() 没有这种写法,改成Db.Set<T>().Add(),返回的就是DbSet<T>
EF可以操作数据,就是因为这个DbSet<T>
2.6.2 BaseDal不需要继承IBaseDal,因为它已经把IBaseDal里面的公用方法都实现了
public class BaseDal<T> where T : class, new() { OAEntities Db = new OAEntities(); /// <summary> /// 新增 /// </summary> /// <param name="entity"></param> /// <returns></returns> public T AddEntity(T entity) { Db.Set<T>().Add(entity);//DbSet<T> Db.SaveChanges(); return entity; } /// <summary> /// 删除 /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool DeleteEntity(T entity) { //追加到ef上,打上删除标记,再保存 Db.Entry<T>(entity).State = System.Data.Entity.EntityState.Deleted; return Db.SaveChanges() > 0; } /// <summary> /// 更新 /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool EditEntity(T entity) { //追加到ef上,打上编辑标记,再保存 Db.Entry<T>(entity).State = System.Data.Entity.EntityState.Modified; return Db.SaveChanges() > 0; } /// <summary> /// 查询过滤 /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda) { return Db.Set<T>().Where(whereLambda); } /// <summary> /// 分页 /// </summary> /// <typeparam name="s"></typeparam> /// <param name="pageIndex">页码</param> /// <param name="pageSize">每页数量</param> /// <param name="totalCount">总数</param> /// <param name="whereLambda">过滤条件</param> /// <param name="orderbyLambda">排序条件</param> /// <param name="isAsc"></param> /// <returns></returns> public IQueryable<T> PageLoadEntities<s>(int pageIndex, int pageSize, out int totalCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, s>> orderbyLambda, bool isAsc) { var temp = Db.Set<T>().Where(whereLambda); totalCount = temp.Count(); if (isAsc)//正序 { //列:pageIndex=3,pageSize=15 //正序排列后,跳过第3页前的数据(前2页*15),剩下的只读取15条 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; } }
2.7.1 UserInfoDal继承 BaseDal的公用方法,还继承了接口IUserInfoDal的自定义方法
public class UserInfoDal : BaseDal<UserInfo>,IUserInfoDal { //继承了公用方法,并且还继承了自定义的方法 //因为BaseDal已经实现了IBaseDal的公共方法,这里不需要再实现IUserInfo继承的IBaseDal的公共方法 }