MVC项目结构搭建及单个类的实现学习笔记1
新人刚开始学习asp.net mvc,若有不足之处希望能得到您的指点,不胜感激!
先来一张项目的层级结构图:
model:模型层,主要是各种类型、枚举以及orm框架,框架完成数据库和实体类的映射。项目中选用了微软的开源orm框架 entityframework 6.0 (以下简称ef),数据库则选择了微软的轻量级数据库sql server compact 4.0本地数据库(简称compact),compact对ef支持比较完美,又属于文档型数据库,部署起来比较简洁。
dal:数据访问层,主要是对数据库的操作层,为业务逻辑层或表示层提供数据服务。
idal:数据访问接口层,是数据访问层的接口,降低耦合。
dalfactory:数据会话层,封装了所有数据操作类实例的创建,将数据访问层与业务逻辑层解耦。
bll:业务逻辑层,主要负责对数据层的操作,把一些数据层的操作进行组合以完成业务的需要。
ibll:业务逻辑接口层,业务逻辑层的接口,降低耦合。
webapp:表现层,是一个asp.net mvc项目,完成具体网站的实现。
common:通用层,用来存放一些工具类。
下面是各个层级之间具体的实现,首先创建以 项目名.层级名 命名的各个层次,除webapp层为asp.net mvc项目外,其余均创建为类库项目。
模型层的构建
先建立模型层,新建asp.net 实体数据模型,关联到已经设计好的数据库,ef自动完成模型类的创建。
数据访问层的构建
dal层中,我们首先需要一个方法来获取单例的ef数据操纵上下文对象,以保证每个用户访问时只有使用一个上下文对象对数据库进行操作。
dbcontextfactory.cs
using system.data.entity; using system.runtime.remoting.messaging; using pms.model; namespace pms.dal { public class dbcontextfactory { /// <summary> /// 负责创建ef数据操作上下文实例,必须保证线程内唯一 /// </summary> public static dbcontext createcontext() { dbcontext dbcontext = (dbcontext)callcontext.getdata("dbcontext"); if (dbcontext != null) return dbcontext; dbcontext = new pmsentities(); callcontext.setdata("dbcontext", dbcontext); return dbcontext; } } }
为user类创建dal层,实现查询、分页查询、增加、删除和修改这五个基本的方法:
userdal.cs
using system; using system.data.entity; using system.linq; using pms.idal; namespace pms.dal { public partial class userdal { public dbcontext dbentities = dbcontextfactory.createcontext(); /// <summary> /// 查询过滤 /// </summary> /// <param name="wherelamada">过滤条件lambda表达式</param> /// <returns>实体集合</returns> public iqueryable<userdal> loadentities(system.linq.expressions.expression<func<userdal, bool>> wherelamada) { return dbentities.set<userdal>().where(wherelamada); } /// <summary> /// 分页查询 /// </summary> /// <typeparam name="ts">排序类型</typeparam> /// <param name="pageindex">查询的页码</param> /// <param name="pagesize">每页显示的数目</param> /// <param name="totalcount">符合条件的总行数</param> /// <param name="wherelambda">过滤条件lambda表达式</param> /// <param name="orderbylambda">排序lambda表达式</param> /// <param name="isasc">排序方向</param> /// <returns>实体集合</returns> public iqueryable<userdal> loadpageentities<ts>(int pageindex, int pagesize, out int totalcount, system.linq.expressions.expression<func<userdal, bool>> wherelambda, system.linq.expressions.expression<func<userdal, ts>> orderbylambda, bool isasc) { var temp = dbentities.set<userdal>().where(wherelambda); totalcount = temp.count(); temp = isasc ? temp.orderby(orderbylambda).skip((pageindex - 1) * pagesize).take(pagesize) : temp.orderbydescending(orderbylambda).skip((pageindex - 1) * pagesize).take(pagesize); return temp; } /// <summary> /// 删除数据 /// </summary> /// <param name="entity">待删数据</param> /// <returns>删除结果</returns> public bool deleteentity(userdal entity) { dbentities.entry(entity).state = entitystate.deleted; return true; } /// <summary> /// 编辑数据 /// </summary> /// <param name="entity">待编辑数据</param> /// <returns>编辑结果</returns> public bool editentity(userdal entity) { dbentities.entry(entity).state = entitystate.modified; return true; } /// <summary> /// 添加数据 /// </summary> /// <param name="entity">待添加数据</param> /// <returns>已添加数据</returns> public userdal addentity(userdal entity) { entity = dbentities.set<userdal>().add(entity); return entity; } } }
注:这里的增删改操作并不即时进行,而是在封装在数据会话层中,以实现工作单元模式,提高数据库的操作效率。
考虑到每个类都需要实现相同的数据操作,我们可以将以上方法封装到一个泛型基类中,各类型只需要继承泛型基类就可以实现以上方法:
basedal.cs
using system; using system.data.entity; using system.linq; namespace pms.dal { public class basedal<t> where t:class ,new() { public dbcontext dbentities = dbcontextfactory.createcontext(); /// <summary> /// 查询过滤 /// </summary> /// <param name="wherelamada">过滤条件lambda表达式</param> /// <returns>实体集合</returns> public iqueryable<t> loadentities(system.linq.expressions.expression<func<t, bool>> wherelamada) { return dbentities.set<t>().where(wherelamada); } /// <summary> /// 分页查询 /// </summary> /// <typeparam name="ts">排序类型</typeparam> /// <param name="pageindex">查询的页码</param> /// <param name="pagesize">每页显示的数目</param> /// <param name="totalcount">符合条件的总行数</param> /// <param name="wherelambda">过滤条件lambda表达式</param> /// <param name="orderbylambda">排序lambda表达式</param> /// <param name="isasc">排序方向</param> /// <returns>实体集合</returns> public iqueryable<t> loadpageentities<ts>(int pageindex, int pagesize, out int totalcount, system.linq.expressions.expression<func<t, bool>> wherelambda, system.linq.expressions.expression<func<t, ts>> orderbylambda, bool isasc) { var temp = dbentities.set<t>().where(wherelambda); totalcount = temp.count(); temp = isasc ? temp.orderby(orderbylambda).skip((pageindex - 1) * pagesize).take(pagesize) : temp.orderbydescending(orderbylambda).skip((pageindex - 1) * pagesize).take(pagesize); return temp; } /// <summary> /// 删除数据 /// </summary> /// <param name="entity">待删数据</param> /// <returns>删除结果</returns> public bool deleteentity(t entity) { dbentities.entry(entity).state = entitystate.deleted; return true; } /// <summary> /// 编辑数据 /// </summary> /// <param name="entity">待编辑数据</param> /// <returns>编辑结果</returns> public bool editentity(t entity) { dbentities.entry(entity).state = entitystate.modified; return true; } /// <summary> /// 添加数据 /// </summary> /// <param name="entity">待添加数据</param> /// <returns>已添加数据</returns> public t addentity(t entity) { entity = dbentities.set<t>().add(entity); //dbentities.savechanges(); return entity; } } }
userdal继承basedal
using pms.idal; using pms.model; namespace pms.dal { public partial class userdal : basedal<user> { } }
数据访问接口层的构建
然后我们建立相应的ibasedal接口和iuserdal接口,并且使userdal类实现iuserdal接口
ibasedal:
using system; using system.linq; namespace pms.idal { public interface ibasedal<t> where t:class,new() { iqueryable<t> loadentities(system.linq.expressions.expression<func<t, bool>> wherelamada); iqueryable<t> loadpageentities<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); bool deleteentity(t entity); bool editentity(t entity); t addentity(t entity); } }
iuserdal:
using pms.model; namespace pms.idal { public partial interface iuserdal:ibasedal<user> { } }
userdal实现iuserdal接口:
public partial class userdal : basedal<user>,iuserdal
数据会话层的构建
抽象工厂类abstractfactory:
using system.configuration; using system.reflection; using pms.idal; namespace pms.dalfactory { public partial class abstractfactory { //读取保存在配置文件中的程序集名称与命名空间名 private static readonly string assemblypath = configurationmanager.appsettings["assemblypath"]; private static readonly string namespace = configurationmanager.appsettings["namespace"]; /// <summary> /// 获取userdal的实例 /// </summary> /// <returns></returns> public static iuserdal createuserinfodal() { var fullclassname = namespace + ".userinfodal"; return createinstance(fullclassname) as iuserdal; } /// <summary> /// 通过反射获得程序集中某类型的实例 /// </summary> /// <param name="classname"></param> /// <returns></returns> private static object createinstance(string classname) { var assembly = assembly.load(assemblypath); return assembly.createinstance(classname); } } }
数据会话类dbsession:
using system.data.entity; using pms.idal; using pms.dal; namespace pms.dalfactory { public partial class dbsession:idbsession { public dbcontext db { get { return dbcontextfactory.createcontext(); } } private iuserdal _userdal; public iuserdal userdal { get { return _userdal ?? (_userdal = abstractfactory.createuserinfodal()); } set { _userdal = value; } } /// <summary> /// 工作单元模式,统一保存数据 /// </summary> /// <returns></returns> public bool savechanges() { return db.savechanges() > 0; } } }
业务逻辑层的构建
业务类基类baseservice
using system; using system.linq; using system.linq.expressions; using pms.dalfactory; using pms.idal; namespace pms.bll { public abstract class baseservice<t> where t:class,new() { public idbsession currentdbsession { get { return new dbsession(); } } public ibasedal<t> currentdal { get; set; } public abstract void setcurrentdal(); public baseservice() { setcurrentdal();//子类一定要实现抽象方法,以指明当前类的子类类型。 } /// <summary> /// 查询过滤 /// </summary> /// <param name="wherelambda"></param> /// <returns></returns> public iqueryable<t> loadentities(expression<func<t, bool>> wherelambda) { return currentdal.loadentities(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> loadpageentities<s>(int pageindex, int pagesize, out int totalcount, expression<func<t, bool>> wherelambda, expression<func<t, s>> orderbylambda, bool isasc) { return currentdal.loadpageentities<s>(pageindex, pagesize, out totalcount, wherelambda, orderbylambda, isasc); } /// <summary> /// 删除 /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool deleteentity(t entity) { currentdal.deleteentity(entity); return currentdbsession.savechanges(); } /// <summary> /// 编辑 /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool editentity(t entity) { currentdal.editentity(entity); return currentdbsession.savechanges(); } /// <summary> /// 添加数据 /// </summary> /// <param name="entity"></param> /// <returns></returns> public t addentity(t entity) { currentdal.addentity(entity); currentdbsession.savechanges(); return entity; } } }
userservice类:
using pms.ibll; using pms.model; namespace pms.bll { public partial class userservice : baseservice<user> { public override void setcurrentdal() { currentdal = currentdbsession.userdal; } } }
业务逻辑接口层的构建
直接建立对应的接口并使用userservice类实现iuserservice接口
ibaseservice接口:
using system; using system.linq; using system.linq.expressions; using pms.idal; namespace pms.ibll { public interface ibaseservice<t> where t : class,new() { idbsession currentdbsession { get; } ibasedal<t> currentdal { get; set; } void setcurrentdal(); iqueryable<t> loadentities(expression<func<t, bool>> wherelambda); iqueryable<t> loadpageentities<s>(int pageindex, int pagesize, out int totalcount, expression<func<t, bool>> wherelambda, expression<func<t, s>> orderbylambda, bool isasc); bool deleteentity(t entity); bool editentity(t entity); t addentity(t entity); } }
iuserservice接口:
using pms.model; namespace pms.ibll { public partial interface iuserservice:ibaseservice<user> { } }
使用userservice类实现iuserservice接口:
public partial class userservice : baseservice<user>, iuserservice
以上我们就完成了整个框架中关于user类的各层次的实现。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 详解微信小程序中组件通讯