MVC三层架构搭建
mvc三层架构搭建
项目主要是用三层来搭建项目,三层分为表现层,数据层和业务层。项目用了目前比较流行的ioc架构。目前流行的ioc 框架有autofac,unity,spring.net等,项目中选用spring.net来搭建三层。
ioc简单介绍
ioc(inversion of control),中文译为控制反转,又称为“依赖注入”(di =dependence injection)ioc的基本概念是:不创建对象,但是描述创建它们的方式。在代码中不直接与对象和服务连接,但在配置文件中描述哪一个组件需要哪一项服务。容器负责将这些联系在一起。网上有好多关于ioc介绍的文章,可以在网上查找关于ioc的知识。
三层搭建图
开始项目搭建
使用模块表为例,来搭建。表结构如下
解决方案项目设计:
1.新建一个空白解决方案名称为ycuniverse
2.在该解决方案下,新建解决方案文件夹业务层,网站层,数据层,业务层,基础层
3.网站层里面添加asp.net web应用程序,项目选用mvc
4.数据层分为新建ycuniverse.dal,ycuniverse.idal,ycuniverse.dalfactory类库
5.业务层分别新建ycuniverse.bll,ycuniverse.ibll类库
6.基础层新建ycuniverse.model类库和ycuniverse.ioc类库
建好后结构如下
基础层搭建
ycuniverse.model作为实体类库,将ef模型实体添加到ycuniverse.model类库
右键单击ycuniverse.model类库=>添加=>新建项
选择好之后点击添加按钮
点击下一步进入下一步配置
点击完成按钮 完成ef的引用
将ycuniverse.model类库中的app.config 中链接数据库字符串复制到webappl网站web.config文件中
数据层配置
ycuniverse.idal 类库添加ycuniverse.model引用
在项目中每一张表都会涉及到查询,新增,删除,编辑,分页等操作,要是每一张表都要新建查询,删除,编辑,分页的方法,这样会增加项目开发时间和产生重复的代码。
解决方法将重复用到的方法封装成一个父类,当子类用到这些方法时可以继承父类。
1.新建父接口的类 名称为ibaserepository.cs
public interface ibaserepository<t> where t : class, new() { /// <summary> /// 查询 /// </summary> /// <param name="wherelambda"></param> /// <returns></returns> iqueryable<t> loadentities(expression<func<t, bool>> wherelambda); /// <summary> /// 添加 /// </summary> /// <param name="entity"></param> /// <returns></returns> t addentity(t entity); /// <summary> /// 编辑 /// </summary> /// <param name="entity"></param> /// <returns></returns> bool editentity(t entity); /// <summary> /// 删除 /// </summary> /// <param name="entity"></param> /// <returns></returns> bool delentity(t entity); /// <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> iqueryable<t> loadpageentities<s>(int pageindex, int pagesize, out int totalcount, expression<func<t, bool>> wherelambda, expression<func<t, s>> orderbylambda, bool isasc); }
2.新建模块表接口类 名称为itbmenurepository.cs继承父类ibaserepository
public interface itbmenurepository : ibaserepository<tbmenu> { }
ycuniverse.idal 类库配置完成
ycuniverse.dal类库开始配置
ycuniverse.dal类库添加ycuniverse.model,ycuniverse.idal,ycuniverse.ioc和ef引用
1.新建ef上下类getdbcontext.cs
public class factorydbcontext { /// <summary> /// 保证ef上下文实例是线程内唯一 /// </summary> /// <returns></returns> public static dbcontext getdbcontext() { dbcontext dbcontext = (dbcontext)callcontext.getdata("dbcontext"); if (dbcontext == null) { dbcontext = new ycuniverseentities(); callcontext.setdata("dbcontext", dbcontext); } return dbcontext; } }
2.新建父类baserepository.cs
public class baserepository<t> where t : class, new() { #region 1.0 获取ef上下文 public dbcontext db = factorydbcontext.getdbcontext(); #endregion #region 2.0 条件查询 public iqueryable<t> loadentities(expression<func<t, bool>> wherelambda) { return db.set<t>().where(wherelambda); } #endregion #region 3.0 分页 /// <summary> /// 分页 /// </summary> /// <typeparam name="s"></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">true 升序 falase 降序</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) { var temp = db.set<t>().where(wherelambda); totalcount = temp.count(); if (isasc) { temp = temp.orderby<t, s>(orderbylambda).take<t>((pageindex - 1) * pagesize).skip<t>(pagesize); } else { temp = temp.orderbydescending<t, s>(orderbylambda).take<t>((pageindex - 1) * pagesize).skip<t>(pagesize); } return temp; } #endregion #region 4.0 添加 public t addentity(t entity) { db.set<t>().add(entity); return entity; } #endregion #region 5.0 删除 public bool delentity(t entity) { db.set<t>().attach(entity); db.entry<t>(entity).state = entitystate.deleted; return true; } #endregion #region 6.0 编辑 public bool editentity(t entity) { db.set<t>().attach(entity); db.entry<t>(entity).state = entitystate.modified; return true; } #endregion }
3.0 新建tbmenurepositor.cs类继承baserepository
public class tbmenurepositor : baserepository<tbmenu>, itbmenurepository { //自定义方法 }
ycuniverse.dal类库配置完成
ycuniverse.ioc配置
webappl添加ycuniverse.ioc,common.logging.dll引用
在webappl网站web.config中添加已下内容
<sectiongroup name="spring"> <!--跟下面spring.net节点配置是一一对应关系--> <!--配置解析spring块的对象--> <section name="context" type="spring.context.support.contexthandler, spring.core" /> <!--配置解析spring存放对象的容器集合--> <section name="objects" type="spring.context.support.defaultsectionhandler, spring.core" /> </sectiongroup> <!--spring.net节点配置--> <spring> <context> <!--容器配置--> <!--<resource uri="config://spring/objects"/>--> <!--xml文件方式,更改属性,复制到输出目录:始终复制--> <resource uri="~/config/dal.xml" /> <resource uri="~/config/controllers.xml" /> <resource uri="~/config/service.xml" /> </context> </spring> <!--log4net节点配置-->
ycuniverse.ioc 类库添加spring.core.dll引用
新建springhelper.cs类
public class springhelper { #region spring容器上下文 +iapplicationcontext springcontext /// <summary> /// spring容器上下文 /// </summary> private static iapplicationcontext springcontext { get { return contextregistry.getcontext(); } } #endregion #region 获取配置文件配置的对象 +t getobject<t>(string objname) where t : class /// <summary> /// 获取配置文件配置的对象 /// </summary> /// <typeparam name="t"></typeparam> /// <param name="objname"></param> /// <returns></returns> public static t getobject<t>(string objname) where t : class { return (t)springcontext.getobject(objname); } #endregion }
ycuniverse.ioc配置完成
在ycuniverse.idal类库添加接口类idbsession.cs
public interface idbsession { bool savechange(); itbmenurepository itbmenurepository { get; set; } }
ycuniverse.dalfactory类库配置
引用ycuniverse.model,ycuniverse.idal,ycuniverse.dal,ef
1.在ycuniverse.dal中新建dalfactory.cs
public class dalfactory { public static itbmenurepository gettbmenurepository { get { return springhelper.getobject<itbmenurepository>("tbmenurepository"); } } }
2.新建dbsession.cs
public class dbsession : idbsession { private itbmenurepository _tbmenurepository; public itbmenurepository itbmenurepository { get { if (_tbmenurepository == null) { _tbmenurepository = dalfactory.gettbmenurepository; } return _tbmenurepository; } set { _tbmenurepository = value; } } dbcontext db = factorydbcontext.getdbcontext(); public bool savechange() { return db.savechanges() > 0; } }
3.新建factorydbsession.cs
public class factorydbsession { /// <summary> /// 实例化dbsession /// </summary> /// <returns></returns> public static idbsession getdbsession() { dbsession dbsession = (dbsession)callcontext.getdata("dbsession"); if (dbsession == null) { dbsession = new dbsession(); callcontext.setdata("dbsession", dbsession); } return dbsession; } }
ycuniverse.dalfactory类库配置完成
ycuniverse.ibll类库开始配置
引用ycuniverse.model,ycuniverse.idal,ycuniverse.dal和ycuniverse.dalfactory
1.新建父类ibaseservice.cs
public interface ibaseservice<t> where t : class, new() { ibaserepository<t> currentrepository { get; set; } idbsession getcurrentdbsession { get; } 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); t addentity(t entity); bool editentity(t entity); bool delentity(t entity); }
2.新建itbmenuservice.cs
public interface itbmenuservice : ibaseservice<tbmenu> { //自定义方法 }
ycuniverse.ibll类库配置完成
ycuniverse.bll类库开始配置
引用ycuniverse.model,ycuniverse.idal,ycuniverse.dal,ycuniverse.ibll和ycuniverse.dalfactory
1.新建父类baseservice.cs
public abstract class baseservice<t> where t : class, new() { public ibaserepository<t> currentrepository { get; set; } public idbsession getcurrentdbsession { get { return factorydbsession.getdbsession(); } } public abstract void setcurretnrepository(); public baseservice() { setcurretnrepository(); } #region 添加 public t addentity(t entity) { currentrepository.addentity(entity); getcurrentdbsession.savechange(); return entity; } #endregion #region 删除 public bool delentity(t entity) { currentrepository.delentity(entity); return getcurrentdbsession.savechange(); } #endregion #region 编辑 public bool editentity(t entity) { currentrepository.editentity(entity); return getcurrentdbsession.savechange(); } #endregion #region 条件查询 public iqueryable<t> loadentities(expression<func<t, bool>> wherelambda) { return currentrepository.loadentities(wherelambda); } #endregion #region 分页 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 currentrepository.loadpageentities<s>(pageindex, pagesize, out totalcount, wherelambda, orderbylambda, isasc); } #endregion }
2.新建tbmenuservice.cs继承父类
public class tbmenuservice : baseservice<tbmenu>, itbmenuservice { public override void setcurretnrepository() { currentrepository = this.getcurrentdbsession.itbmenurepository; } }
ycuniverse.bll配置完成
网站层 webappl配置
引用ycuniverse.model,ycuniverse.bll和ycuniverse.ibll
新建文件夹config
分别新建controllers.xml,dal.xml,service.xml设置3个文件的属性为始终复制
dal.xml
<objects xmlns="http://www.springframework.net"> <!--这里放容器里面的所有节点--> <description>an example that demonstrates simple ioc features.</description> <!--name 必须要唯一的,type=类的全名称,所在的程序集--> <object name="tbmenurepository" type="ycuniverse.dal.tbmenurepositor, ycuniverse.dal" singleton="false"></object> </objects>
service.xml
<objects xmlns="http://www.springframework.net"> <!--这里放容器里面的所有节点--> <!--name 必须要唯一的,type=类的全名称,所在的程序集--> <object type="ycuniverse.bll.tbmenuservice, ycuniverse.bll" singleton="false" name="tbmenuservice"></object> </objects>
controllers.xml
<objects xmlns="http://www.springframework.net"> <!--这里放容器里面的所有节点--> <!--name 必须要唯一的,type=类的全名称,所在的程序集--> <!--属性注入--> <!--权限管理--> <object type="webappl.controllers.basecontroller, webappl" singleton="false" > <property name="tbmenuservice" ref="tbmenuservice" /> </object> </objects>
在控制器中新建类basecontroller.cs
public class basecontroller : controller { //属性注入 public itbmenuservice tbmenuservice { get { return springhelper.getobject<itbmenuservice>("tbmenuservice"); } } }
到此三层架构搭建完成
新建页面测试
新建testcontroller控制器
运行项目 页面如下所示
项目源代码地址 提取码 ky97
视频地址 提取码: 7wuu
上一篇: Flutter学习笔记(3)--Dart变量与基本数据类型
下一篇: AQS原理