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);
}
View Code

2.新建模块表接口类 名称为ItbMenuRepository.cs继承父类IBaseRepository

public interface ItbMenuRepository : IBaseRepository<tbMenu>
{

}
View Code

YCUniverse.IDAL 类库配置完成

YCUniverse.DAL类库开始配置

YCUniverse.DAL类库添加YCUniverse.Model,YCUniverse.IDAL,YCUniverse.Ioc和EF引用

1.新建EF上下类FactoryDBContext.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;
}
}
View Code

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
}
View Code

3.0 新建tbMenuRepositor.cs类继承BaseRepository

public class tbMenuRepositor : BaseRepository<tbMenu>, ItbMenuRepository
{
//自定义方法
}
View Code

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节点配置-->
View Code

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
}
View Code

YCUniverse.Ioc配置完成

在YCUniverse.IDAL类库添加接口类IDBSession.cs

public interface IDBSession
{
bool SaveChange();
ItbMenuRepository ItbMenuRepository { get; set; }
}
View Code

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");
}
}
}
View Code

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;
}
}
View Code

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;
}
}
View Code

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);
}
View Code

2.新建ItbMenuService.cs

public interface ItbMenuService : IBaseService<tbMenu>
{
//自定义方法
}
View Code

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

}
View Code

2.新建tbMenuService.cs继承父类

public class tbMenuService : BaseService<tbMenu>, ItbMenuService
{
public override void SetCurretnRepository()
{
CurrentRepository = this.GetCurrentDbSession.ItbMenuRepository;
}
}
View Code

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>
View Code

service.xml

<objects xmlns="http://www.springframework.net">
<!--这里放容器里面的所有节点-->
<!--name 必须要唯一的,type=类的全名称,所在的程序集-->
<object type="YCUniverse.BLL.tbMenuService, YCUniverse.BLL" singleton="false" name="tbMenuService"></object>
</objects>
View Code

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>
View Code

在控制器中新建类BaseController.cs

public class BaseController : Controller
{
//属性注入
public ItbMenuService tbMenuService
{
get
{
return SpringHelper.GetObject<ItbMenuService>("tbMenuService");
}
}
}
View Code

到此三层架构搭建完成

新建页面测试

新建TestController控制器

运行项目 页面如下所示

项目源代码地址 提取码 ky97

视频地址 提取码: 7wuu 

  

原文地址:https://www.cnblogs.com/zhangwg/p/11363129.html