先来看一下我们的解决方案

  我们建立Yubay.Models项目,

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Yubay.Models.Data
{
public class YubayDbContext : DbContext
{
public YubayDbContext() : base("YubayConnection")
{ } public DbSet<Category> Category { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.CreateIfNotExists(); base.OnModelCreating(modelBuilder);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Yubay.Models
{
[Table("Category")]
public partial class Category
{
public int Id { get; set; } [StringLength()]
public string Code { get; set; } [StringLength()]
public string ParentCode { get; set; } public int? CategoryLevel { get; set; } [StringLength()]
public string Name { get; set; } [StringLength()]
public string Url { get; set; } public int? State { get; set; }
}
}

  接着再建立Yubay.Service项目

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Yubay.Core.Models; namespace Yubay.Service
{
public interface IBaseService : IDisposable
{
/// <summary>
/// 根据id查询实体
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
T Find<T>(int id) where T : class; /// <summary>
/// 提供对单表的查询,
/// </summary>
/// <returns>IQueryable类型集合</returns>
IQueryable<T> Set<T>() where T : class; /// <summary>
/// 查询
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="funcWhere"></param>
/// <returns></returns>
IQueryable<T> Query<T>(Expression<Func<T, bool>> funcWhere) where T : class; /// <summary>
/// 分页查询
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="S"></typeparam>
/// <param name="funcWhere"></param>
/// <param name="pageSize"></param>
/// <param name="pageIndex"></param>
/// <param name="funcOrderby"></param>
/// <param name="isAsc"></param>
/// <returns></returns>
PageResult<T> QueryPage<T, S>(Expression<Func<T, bool>> funcWhere, int pageSize, int pageIndex, Expression<Func<T, S>> funcOrderby, bool isAsc = true) where T : class; /// <summary>
/// 新增数据,即时Commit
/// </summary>
/// <param name="t"></param>
/// <returns>返回带主键的实体</returns>
T Insert<T>(T t) where T : class; /// <summary>
/// 新增数据,即时Commit
/// 多条sql 一个连接,事务插入
/// </summary>
/// <param name="tList"></param>
/// <returns>返回带主键的集合</returns>
IEnumerable<T> Insert<T>(IEnumerable<T> tList) where T : class; /// <summary>
/// 更新数据,即时Commit
/// </summary>
/// <param name="t"></param>
void Update<T>(T t) where T : class; /// <summary>
/// 更新数据,即时Commit
/// </summary>
/// <param name="tList"></param>
void Update<T>(IEnumerable<T> tList) where T : class; /// <summary>
/// 根据主键删除数据,即时Commit
/// </summary>
/// <param name="t"></param>
void Delete<T>(int Id) where T : class; /// <summary>
/// 删除数据,即时Commit
/// </summary>
/// <param name="t"></param>
void Delete<T>(T t) where T : class; /// <summary>
/// 删除数据,即时Commit
/// </summary>
/// <param name="tList"></param>
void Delete<T>(IEnumerable<T> tList) where T : class; /// <summary>
/// 立即保存全部修改
/// </summary>
void Commit(); /// <summary>
/// 执行sql 返回集合
/// </summary>
/// <param name="sql"></param>
/// <param name="parameters"></param>
/// <returns></returns>
IQueryable<T> ExcuteQuery<T>(string sql, SqlParameter[] parameters) where T : class; /// <summary>
/// 执行sql,无返回
/// </summary>
/// <param name="sql"></param>
/// <param name="parameters"></param>
void Excute<T>(string sql, SqlParameter[] parameters) where T : class;
}
}
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Yubay.Core.Models; namespace Yubay.Service
{ public class BaseService : IBaseService
{
#region Identity
protected DbContext Context { get; private set; }
/// <summary>
/// 构造函数注入
/// </summary>
/// <param name="context"></param>
public BaseService(DbContext context)
{
this.Context = context;
}
#endregion Identity public T Insert<T>(T t) where T : class
{
this.Context.Set<T>().Add(t);
this.Commit();
return t;
} public IEnumerable<T> Insert<T>(IEnumerable<T> tList) where T : class
{
this.Context.Set<T>().AddRange(tList);
this.Commit();//一个链接 多个sql
return tList;
} #region Query
public T Find<T>(int id) where T : class
{
return this.Context.Set<T>().Find(id);
} /// <summary>
/// 不应该暴露给上端使用者
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IQueryable<T> Set<T>() where T : class
{
return this.Context.Set<T>();
}
/// <summary>
/// 这才是合理的做法,上端给条件,这里查询
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="funcWhere"></param>
/// <returns></returns>
public IQueryable<T> Query<T>(Expression<Func<T, bool>> funcWhere) where T : class
{
return this.Context.Set<T>().Where<T>(funcWhere);
} public PageResult<T> QueryPage<T, S>(Expression<Func<T, bool>> funcWhere, int pageSize, int pageIndex, Expression<Func<T, S>> funcOrderby, bool isAsc = true) where T : class
{
var list = this.Set<T>();
if (funcWhere != null)
{
list = list.Where<T>(funcWhere);
}
if (isAsc)
{
list = list.OrderBy(funcOrderby);
}
else
{
list = list.OrderByDescending(funcOrderby);
}
PageResult<T> result = new PageResult<T>()
{
DataList = list.Skip((pageIndex - ) * pageSize).Take(pageSize).ToList(),
PageIndex = pageIndex,
PageSize = pageSize,
TotalCount = this.Context.Set<T>().Count(funcWhere)
};
return result;
}
#endregion /// <summary>
/// 是没有实现查询,直接更新的
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
public void Update<T>(T t) where T : class
{
if (t == null) throw new Exception("t is null"); this.Context.Set<T>().Attach(t);//将数据附加到上下文,支持实体修改和新实体,重置为UnChanged
this.Context.Entry<T>(t).State = EntityState.Modified;
this.Commit();//保存 然后重置为UnChanged
} public void Update<T>(IEnumerable<T> tList) where T : class
{
foreach (var t in tList)
{
this.Context.Set<T>().Attach(t);
this.Context.Entry<T>(t).State = EntityState.Modified;
}
this.Commit();
} /// <summary>
/// 先附加 再删除
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
public void Delete<T>(T t) where T : class
{
if (t == null) throw new Exception("t is null");
this.Context.Set<T>().Attach(t);
this.Context.Set<T>().Remove(t);
this.Commit();
} /// <summary>
/// 还可以增加非即时commit版本的,
/// 做成protected
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Id"></param>
public void Delete<T>(int Id) where T : class
{
T t = this.Find<T>(Id);//也可以附加
if (t == null) throw new Exception("t is null");
this.Context.Set<T>().Remove(t);
this.Commit();
} public void Delete<T>(IEnumerable<T> tList) where T : class
{
foreach (var t in tList)
{
this.Context.Set<T>().Attach(t);
}
this.Context.Set<T>().RemoveRange(tList);
this.Commit();
} public void Commit()
{
this.Context.SaveChanges();
} public IQueryable<T> ExcuteQuery<T>(string sql, SqlParameter[] parameters) where T : class
{
return this.Context.Database.SqlQuery<T>(sql, parameters).AsQueryable();
} public void Excute<T>(string sql, SqlParameter[] parameters) where T : class
{
DbContextTransaction trans = null;
try
{
trans = this.Context.Database.BeginTransaction();
this.Context.Database.ExecuteSqlCommand(sql, parameters);
trans.Commit();
}
catch (Exception ex)
{
if (trans != null)
trans.Rollback();
throw ex;
}
} public virtual void Dispose()
{
if (this.Context != null)
{
this.Context.Dispose();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yubay.Models; namespace Yubay.Service
{
public interface ICategoryService : IBaseService
{
#region Query
/// <summary>
/// 用code获取当前类及其全部子孙类别的id
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
IEnumerable<int> GetDescendantsIdList(string code); /// <summary>
/// 根据类别编码找子类别集合 找一级类用默认code root
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
IEnumerable<Category> GetChildList(string code = "root"); /// <summary>
/// 查询并缓存全部的类别数据
/// 类别数据一般是不变化的
/// </summary>
/// <returns></returns>
List<Category> CacheAllCategory();
#endregion Query
}
}
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yubay.Core.Caching;
using Yubay.Models; namespace Yubay.Service
{
public class CategoryService : BaseService, ICategoryService
{
#region Identity
private DbSet<Category> _CategorySet = null; public CategoryService(DbContext dbContext)
: base(dbContext)
{
this._CategorySet = base.Context.Set<Category>();
}
#endregion Identity #region Query
/// <summary>
/// 用code获取当前类及其全部子孙类别的id
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public IEnumerable<int> GetDescendantsIdList(string code)
{
return this._CategorySet.Where(c => c.Code.StartsWith(code)).Select(c => c.Id);
} /// <summary>
/// 根据类别编码找子类别集合 找一级类用默认code root
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public IEnumerable<Category> GetChildList(string code = "root")
{
return this._CategorySet.Where(c => c.ParentCode.StartsWith(code));
} /// <summary>
/// 查询并缓存全部数据
/// </summary>
/// <returns></returns>
public List<Category> CacheAllCategory()
{
return CacheManager.Get<List<Category>>("AllCategory", () => this._CategorySet.ToList());
}
#endregion Query
}
}

  接着再创建Yubay.Core项目,里面主要的两个类

using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity; namespace Yubay.Core.IOC
{
/// <summary>
/// 依赖注入工厂
/// </summary>
public class DIFactory
{
private static object _SyncHelper = new object();
private static Dictionary<string, IUnityContainer> _UnityContainerDictionary = new Dictionary<string, IUnityContainer>(); /// <summary>
/// 根据containerName获取指定的container
/// </summary>
/// <param name="containerName">配置的containerName,默认为defaultContainer</param>
/// <returns></returns>
public static IUnityContainer GetContainer(string containerName = "unityContainer")
{
if (!_UnityContainerDictionary.ContainsKey(containerName))
{
lock (_SyncHelper)
{
if (!_UnityContainerDictionary.ContainsKey(containerName))
{
//配置UnityContainer
IUnityContainer container = new UnityContainer();
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "config\\unity.config");
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
UnityConfigurationSection configSection = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
configSection.Configure(container, containerName); _UnityContainerDictionary.Add(containerName, container);
}
}
}
return _UnityContainerDictionary[containerName];
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Routing;
using Unity; namespace Yubay.Core.IOC
{
/// <summary>
/// 自定义的控制器实例化工厂
/// </summary>
public class UnityControllerFactory : DefaultControllerFactory
{
private IUnityContainer UnityContainer
{
get
{
return DIFactory.GetContainer();
}
} /// <summary>
/// 创建控制器对象
/// </summary>
/// <param name="requestContext"></param>
/// <param name="controllerType"></param>
/// <returns></returns>
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (null == controllerType)
{
return null;
}
IController controller = (IController)this.UnityContainer.Resolve(controllerType);
return controller;
}
/// <summary>
/// 释放
/// </summary>
/// <param name="controller"></param>
public override void ReleaseController(IController controller)
{
//释放对象
base.ReleaseController(controller);//
}
}
}

  最后在MVC项目里面配置unity.config

<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
</configSections>
<unity>
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Unity.Interception.Configuration"/>
<containers>
<container name="unityContainer">
<extension type="Interception"/>
<register type="System.Data.Entity.DbContext, EntityFramework" mapTo="Yubay.Models.Data.YubayDbContext, Yubay.Models"/>
<register type="Yubay.Service.IBaseService,Yubay.Service" mapTo="Yubay.Service.BaseService, Yubay.Service"/>
<register type="Yubay.Service.ICategoryService,Yubay.Service" mapTo="Yubay.Service.CategoryService, Yubay.Service"/>
</container> <container name="unityContainerGeneric">
<register type="System.Data.Entity.DbContext, EntityFramework" mapTo="Yubay.Models.Data.YubayDbContext, Yubay.Models"/>
<register type="Yubay.Service.IBaseService`1,Yubay.Service" mapTo="Yubay.Service.BaseService`1, Yubay.Service"/>
<register type="Yubay.Service.ICategoryService,Ruanmou.Yubay.Service" mapTo="Yubay.Service.CategoryService, Yubay.Service"/>
</container>
</containers>
</unity>
</configuration>

  那么这时候如何把Unity整合到MVC呢?我们知道http请求管道中,激活Controller的时候有一个DefaultControllerFactory,我们可以通过重写它的方法来实现,在Application_Start中使用下面的语句替换掉默认的DefaultControllerFactory。

 ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory());//替换默认的控制器工厂

  至此就将Unity整合到MVC中了。

Unity整合Asp.Net MVC的更多相关文章

  1. Unity + iBatis + Asp.net Mvc 系统搭建

    Unity + iBatis + Asp.net Mvc 系统搭建 之前用EntityFramework Code First做了一些小项目,很是方便:后来在一个 Java 项目中接触了myBatis ...

  2. Bootstrap整合ASP.NET MVC验证、jquery.validate.unobtrusive

    没什么好讲的,上代码: (function ($) { var defaultOptions = { validClass: 'has-success', errorClass: 'has-error ...

  3. ASP.NET MVC 使用Unity实现Ioc

    为什么有这篇文章 最近在学ASP.NET MVC项目中使用Ioc,选用了Unity作为依赖注入的容器组件,在网上找了相关的文章简单实现了依赖注入,但想用文件配置的方式进行容器注入的注册,发现相关的文章 ...

  4. 深度解析 ASP.NET MVC 5

    ASP.NET MVC基础 IoC容器 ASP.NET MVC可扩展性 ASP.NET MVC Filters & Cache ASP.NET MVC AJAX ASP.NET MVC Cli ...

  5. [转]ASP.NET MVC Spring.NET NHibernate 整合

    请注明转载地址:http://www.cnblogs.com/arhat 在整合这三个技术之前,首先得说明一下整合的步骤,俗话说汗要一口一口吃,事要一件一件做.同理这个三个技术也是.那么在整合之前,需 ...

  6. ASP.NET MVC Spring.NET NHibernate 整合

    请注明转载地址:http://www.cnblogs.com/arhat 在整合这三个技术之前,首先得说明一下整合的步骤,俗话说汗要一口一口吃,事要一件一件做.同理这个三个技术也是.那么在整合之前,需 ...

  7. ASP.NET MVC NHibernate 整合

    请注明转载地址:http://www.cnblogs.com/arhat 在整合这三个技术之前,首先得说明一下整合的步骤,俗话说汗要一口一口吃,事要一件一件做.同理这个三个技术也是.那么在整合之前,需 ...

  8. ASP.NET MVC Spring.NET 整合

    请注明转载地址:http://www.cnblogs.com/arhat 在整合这三个技术之前,首先得说明一下整合的步骤,俗话说汗要一口一口吃,事要一件一件做.同理这个三个技术也是.那么在整合之前,需 ...

  9. ASP.NET MVC IOC之Unity攻略

    ASP.NET MVC IOC之Unity攻略 一.你知道IOC与DI吗? 1.IOC(Inversion of Control )——控制反转 即依赖对象不在被依赖模块的类中直接通过new来获取 先 ...

随机推荐

  1. UI:数据库练习、滤镜效果

    相机处理滤镜效果 滤镜主要使用在相机的美颜. #import "ViewController.h" #import "ImageUtil.h" #import ...

  2. iOS UI控件

    创建: 2018/04/21 完成: 2018/04/25 更新: 2018/09/24 补充UIActivityIndicatorView的显示和隐藏方法 UIButton  设定项目  项目名   ...

  3. composer下载tp5第三方扩展

    (谨记:如果使用 composer 命令安装失败,请查看根目录下的 composer.json 文件是否正确,并查看下的扩展是否有多个版本,下载的版本是否符合当前框架的版本) 1.基础 compose ...

  4. hihocoder 1331 扩展二进制数(递归)

    传送门 题意 略 分析 由低位向高位考虑,令f(n)为n的扩展二进制数表示数 1.当前数为偶数,末位为0或2,那么f(n)=f(n/2)+f(n/2-1) 2.当前数为奇数,末位为1,那么f(n)=f ...

  5. UVALive 6833【模拟】

    题意: 算从左往右的值,先乘后加的值,数的范围<=9= =,然后根据满足的条件输出字符. 思路: 从左往右就是直接来了,先做乘法就是乘法两边的数字靠向右边那个,且左边那个为0,然后所有值一加就好 ...

  6. 密码(Password)

    #include<cstdio> #include<cstring> using namespace std; int k, cnt; ][][], ans[]; bool d ...

  7. iOS 设置UITextView的Placeholder

    代码如下: - (void)setupTextView { UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, ...

  8. Jquery | 基础 | 导航条在项目中的应用

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. nginx媒体类型

    在服务器的响应头中,有Content-Type一行,表明传输的http媒体类型. 比如:txt文件就用text/plain 表明. conf/mime.type types { text/html h ...

  10. Chef and Graph Queries CodeChef - GERALD07

    https://vjudge.net/problem/CodeChef-GERALD07 可以用莫队+带撤销并查集做 错误记录: 1.调试时数组开小了,忘了改大就交了 2.88行和91行少了备份num ...