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的公共方法 }