Unity IoC Base On MVC
Unity框架,是一个经典的IoC模式实现方式,其通过config文件配置section,将接口与实现解藕,config中的section配置的container以全名称对应,使得应用程序无需像Ninject方式那样,依赖接口项和实现项,因其解藕的配置,能让应用程序实现静态更新服务的效果,即不退出应用程序更新服务的功能。
下面是Unity的实现,其核心接口IUnityContainer是Unity模式实现的基础,在Controller生命周期中IUnityContainer起到传递section配置的作用,在Controller实例化时创建依赖的接口服务的实例对象,当Countroller执行结束后,接口服务的实例对象交由IUnityContainer维护
首先需要安装Unity,引用以下两个dll

接下来实现Unity的管理类,新建mvc5网站项目,并如下实现
public class UnityControllerFactory : DefaultControllerFactory
{
static object syncHelper = new object();
static Dictionary<string, IUnityContainer> containers = new Dictionary<string, IUnityContainer>();
public IUnityContainer UnityContainer { get; private set; }
public UnityControllerFactory(string containerName = "")
{
if (containers.ContainsKey(containerName))
{
this.UnityContainer = containers[containerName];
return;
}
lock (syncHelper)
{
if (containers.ContainsKey(containerName))
{
this.UnityContainer = containers[containerName];
return;
}
IUnityContainer container = new UnityContainer();
//配置UnityContainer
UnityConfigurationSection configSection = ConfigurationManager.GetSection(UnityConfigurationSection.SectionName) as UnityConfigurationSection;
if (null == configSection && !string.IsNullOrEmpty(containerName))
{
throw new ConfigurationErrorsException("The <unity> configuration section does not exist.");
}
if (null != configSection)
{
if (string.IsNullOrEmpty(containerName))
{
configSection.Configure(container);
}
else
{
configSection.Configure(container, containerName);
}
}
containers.Add(containerName, container);
this.UnityContainer = containers[containerName];
}
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (null == controllerType)
{
return null;
}
return (IController)this.UnityContainer.Resolve(controllerType);
}
public override void ReleaseController(IController controller)
{
this.UnityContainer.Teardown(controller);
}
}
假设有一个服务接口在一个独立的C#类库名为MyInterface
public interface IEmployeeRepository
{
IEnumerable<Employee> GetEmployees(string id = "");
}
和服务接口的实现项在一个独立的C#类库名为MyService
public class EmployeeRepository: IEmployeeRepository
{
private static IList<Employee> employees;
static EmployeeRepository()
{
employees = new List<Employee>();
employees.Add(new Employee(Guid.NewGuid().ToString(), "张三", "男", new DateTime(, , ), "销售部"));
employees.Add(new Employee(Guid.NewGuid().ToString(), "李四", "女", new DateTime(, , ), "人事部"));
employees.Add(new Employee(Guid.NewGuid().ToString(), "王五", "男", new DateTime(, , ), "人事部"));
}
public IEnumerable<Employee> GetEmployees(string id = "")
{
return employees.Where(e => e.Id == id || string.IsNullOrEmpty(id) || id == "*");
}
}
在上面新建的mvc5网站项目中,引用MyInterface的C#类库,在网站的global文件中Application_Start中添加如下这句,将我们创建的UnityControllerFactory注入给ControllerBuilder初始化
ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory());
在mvc5网站的web.config中添加section接口服务map信息,将接口与其服务实现类信息配置给Unity
<unity>
<containers>
<container>
<register type="Mvc.IEmployeeRepository, Mvc.MvcApp"
mapTo="Mvc.EmployeeRepository, Mvc.MvcApp"/>
</container>
</containers>
</unity>
Unity与于Ninject的好处很明显,接口与实现解藕,无需直接依赖实现项,达到静态更新的效果,在应用程序运行时,只需将实现项的DLL(此处即为MyService.dll)下载替换,即可实现服务更新,要实现自动化静态更新当然需要做些工作,比如MyService应该需要版本检测及下载功能,有兴趣的可以做做
如果想深入了解Unity的对象管理,可以看看老A的这篇文章, 本文参考老A的MVC IoC系列文章
http://www.cnblogs.com/artech/archive/2010/07/13/IoC_Unity_Build.html
Unity IoC Base On MVC的更多相关文章
- MVC中使用Unity Ioc Container
ASP.NET MVC中使用Unity Ioc Container 写在前面 安装Unity 添加服务层 IArticleRepository类型映射 服务注入到控制器 Global.asax初始 ...
- Unity + iBatis + Asp.net Mvc 系统搭建
Unity + iBatis + Asp.net Mvc 系统搭建 之前用EntityFramework Code First做了一些小项目,很是方便:后来在一个 Java 项目中接触了myBatis ...
- 【.NET6+WPF】WPF使用prism框架+Unity IOC容器实现MVVM双向绑定和依赖注入
前言:在C/S架构上,WPF无疑已经是"桌面一霸"了.在.NET生态环境中,很多小伙伴还在使用Winform开发C/S架构的桌面应用.但是WPF也有很多年的历史了,并且基于MVVM ...
- 总结Unity IOC容器通过配置实现类型映射的几种基本使用方法
网上关于Unity IOC容器使用的方法已很多,但未能做一个总结,故我这里总结一下,方便大家选择. 首先讲一下通过代码来进行类型映射,很简单,代码如下: unityContainer = new Un ...
- IOC运用到MVC中
IOC可以摒弃掉类中类的紧耦合,让设计和重用更简单,将IOC加入到MVC中的实现非常简单,那么有哪几种方法?它们的实现又是什么原理呢? IOC在MVC中的注入,主要是在获取Controller对象中实 ...
- Unity IOC容器通过配置实现类型映射的几种基本使用方法
网上关于Unity IOC容器使用的方法已很多,但未能做一个总结,故我这里总结一下,方便大家选择. 首先讲一下通过代码来进行类型映射,很简单,代码如下 unityContainer = new Uni ...
- .NET Unity IOC框架使用实例
1.IOC简介 IOC(Inversion of Control), 控制反转 DI (Dependency Injection),依赖注入 IOC的基本概念是:不创建对象,但是描述创建它们的方式.在 ...
- 今天研究Unity Ioc 框架
今天研究Unity Ioc 框架,被自己坑了两个多小时. 运行就报错,反反复复检查了很多次,配置文件,代码都没有问题,也从新写了好几遍. 最后仔细看报错消息才知道,config文件没有生成到目录……… ...
- Unity Ioc 依赖倒置及Untity AOP被动拦截/自动拦截
各位博友金安,首先声明这是一篇转载的博客,原文链接:https://www.cnblogs.com/scottpei/archive/2013/01/08/2851087.html 十年河东,十年河西 ...
随机推荐
- Android开发 LiveData与MutableLiveData详解
前言 LiveData与ViewMode是经常搭配在一起使用的,但是为了不太混乱,我还是拆分开来说明,此篇博客只讲解 LiveData 与 MutableLiveData的概念与使用方式(但是会涉及到 ...
- css----7渐变
linear-gradient(90deg,red 10%,yellow 20%,green 30%) <!DOCTYPE html> <html> <head> ...
- JavaScript - DOM相关
DOM节点分类 ( node ) : 元素节点 ( element node ) 属性节点 ( attribute node ) 文本节点 ( text node) DOM的相关操作 : 1. 查询元 ...
- Oracle大数据SQL语句优化
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- RocketMQ源码分析之从官方示例窥探:RocketMQ事务消息实现基本思想
摘要: RocketMQ源码分析之从官方示例窥探RocketMQ事务消息实现基本思想. 在阅读本文前,若您对RocketMQ技术感兴趣,请加入RocketMQ技术交流群 RocketMQ4.3.0版本 ...
- vuex的使用入门-官方用例
store/index.js import Vue from 'vue' import Vuex from 'vuex'; // 使用vuex Vue.use(Vuex) const store = ...
- ssm项目中使用拦截器加上不生效解决方案
在很多时候,需要拦截器来帮助我们完成一些特定的工作,比如获取请求的参数,本身在request这种获取数据就是一次磁盘的io, 如果在filter中获取了参数,那么在controller中就不能获取相关 ...
- MFC 窗口刷新防止闪烁方法
防止窗口闪烁的方法 1.将Invalidate()替换为InvalidateRect(). Invalidate()会导致整个窗口的图象重画,需要的时间比较长,而InvalidateRect()仅仅重 ...
- 将本地数据库迁移到云端RDS数据库
- 2 _ 基本框架 _ 检测VMX环境
VT 是先开为大,谁先开谁上层,谁上层 谁权限大. 1 判断是否支持 VMX intel 白皮书 第3卷 传入 参数eax =1, 返回值 ecx 的第5位 = 1 则 surpported VMX. ...