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

  我们建立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. maven 简单入门教学实战手册

    Maven那点事儿(Eclipse版)   前言: 由于最近工作学习,总是能碰到Maven的源码.虽然平时工作并不使用Maven,但是为了学习一些源码,还是必须要了解下.这篇文章不是一个全面的Mave ...

  2. rsync(二):inotify+rsync详细说明和sersync

    以下是rsync系列篇: inotify+rsync 如果要实现定时同步数据,可以在客户端将rsync加入定时任务,但是定时任务的同步时间粒度并不能达到实时同步的要求.在Linux kernel 2. ...

  3. 【旧文章搬运】Windows句柄表分配算法分析(三)

    原文发表于百度空间,2009-03-30========================================================================== 三.当需要 ...

  4. DC 兼容的DC

    DC是 "Device Content" , MS VC++ 的 MFC图形设备接口 的 设备描述表.它是MFC的主要对象之一.通过CDC类进行各种绘图操作,例如选笔,选色,选涂色 ...

  5. 洛谷 - P1142 - 轰炸 - 计算几何

    https://www.luogu.org/problemnew/show/P1142 枚举一个基点,枚举另一个点找出斜率,约分后存入.记得要加上那个点本身. __gcd(x,y),返回值符号与y的符 ...

  6. 解决sqlserver的sql脚本内存不足问题

    1.Win+R输出cmd 2.sqlcmd -i  E:\Work\xxxxx.sql -d xxxxx(数据库名称) -s 127.0.0.1

  7. dd 使用记录

    使用dd的工具 1)测试写速度 2)测试读速度 3)测试读写速度 dd说明: if是输入文本,of是输出文本,bs是块大小,count是你指定读写块的数量 /dev/zero是从内存里面读取,不会产生 ...

  8. iOS UITableView ExpandableHeader(可形变的Header)

    最常见的header就是在tableView下拉时header里的图片会放大的那种, 最近研究了一下,自己实现了这种header. 1.设置TableView的contentInset(为header ...

  9. PostgreSQL - 官方手册、中文手册及Github项目地址

    PostgreSQL每次更新都会有语法变化,低版本的PostgreSQL是无法运行高版本的sql语法的,下边是官方手册地址,可以查看多个版本的: https://www.postgresql.org/ ...

  10. Python标准库 datetime

    >>> import datetime >>> now = datetime.datetime.now() >>> now datetime.da ...