最近公司用到了Unity,自己就研究了一下。

新建一个ASP.NET MVC基本项目,在NuGet上引入Unity4.0.1最新版。
因为我使用的项目为ASP.NET MVC,所以又添加一个Unity bootstrapper for ASP.NET MVC。
NuGet会帮你在App_Start文件夹下自动添加两个文件UnityConfig.cs和UnityMvcActivator.cs。
 
文件UnityConfig.cs是配置Unity依赖注入用的,可以分为两种
这两种方式的不同点在于,修改依赖注入时,如果是方式一可以在系统运行时修改(支持热插拔)。
文件UnityWebActivator.cs是我们创建的依赖关系注册到MVC中。
我们需要做的是在Global.asax文件中将Unity运行。
 
下面来详细讲述一下这两方式的配置:
方式一使用web.config配置:
1.在UnityConfig.cs下将注释取消container.LoadConfiguration()
        /// <summary>Registers the type mappings with the Unity container.</summary>
/// <param name="container">The unity container to configure.</param>
/// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to
/// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
public static void RegisterTypes(IUnityContainer container)
{
// NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
container.LoadConfiguration(); // TODO: Register your types here
// container.RegisterType<IProductRepository, ProductRepository>();
}

2.在configSections节点下添加以下内容

  <configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
</configSections>

3.配置unity节点信息

  <unity  xmlns="http://schemas.microsoft.com/practices/2010/unity">
<assembly name="IBLL"/>
<assembly name="BLL"/>
<containers>
<container>
<register type="IBLL.ITest,IBLL" mapTo="BLL.Test,BLL" />
</container>
</containers>
</unity>

配置完成运行就ok了。

这是我遇到一个问题,找了很多资料才解决的
原来web.config是这样配置的
  <unity  xmlns="http://schemas.microsoft.com/practices/2010/unity">
<containers>
<container>
<register type="IBLL.ITest" mapTo="BLL.Test" />
</container>
</containers>
</unity>

这种情况下会报这种错误

The type name or alias IBLL.ITest could not be resolved. Please check your configuration file and verify this type name.
 
 
方式二使用代码配置:
这种方式十分的简单基本上我们只要参考作者给的提示就ok
在UnityConfig.cs下配置如下代码:
        /// <summary>Registers the type mappings with the Unity container.</summary>
/// <param name="container">The unity container to configure.</param>
/// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to
/// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
public static void RegisterTypes(IUnityContainer container)
{
// NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
//container.LoadConfiguration(); // TODO: Register your types here
// container.RegisterType<IProductRepository, ProductRepository>();
container.RegisterType<IBLL.ITest,BLL.Test>();
}

以上是个人使用经验分享给大家,上面的内容比较浅显,如果有错误请大家指正

 
 
 
 
 
 

Unity4.0的使用的更多相关文章

  1. Unity4.0配置

    关于Unity4.0的使用: 一 安装Unity 在程序包管理器控制台输入命令:Istall-Pckage unity.mvc安装后会在App_Start中生成UnityConfig.cs 和Unit ...

  2. Ioc依赖注入:Unity4.0.1 在项目中的应用 (MVC和API)

    使用Unity的好处网上有很多,百度一下即可 这里引用了一篇关于面向接口编程的好处的文章作为引申:https://blog.csdn.net/Cyy19970527/article/details/8 ...

  3. 【英宝通Unity4.0公开课学习 】(五)47讲到75讲

    不知不觉今天都已经看了快30讲了,虽然很想快马加鞭地将后面的内容看完,但为了学习的质量,还是挺下来写一篇blog :) 这两天有些昏昏沉沉的,但感觉生活还安排得不错,原因找去找来觉得是没怎么开窗透气. ...

  4. 【英宝通Unity4.0公开课学习 】(六)76讲到90讲

    还是关于Mecanim动画的内容. 这些讲的每讲长度明显比前面的长,而且很多都涉及到脚本编写. 不过我还是2倍速给略览过去了,主要目的就是学个框架嘛 :) 1. Blend Tree 可嵌套. 可理解 ...

  5. 【英宝通Unity4.0公开课学习 】(四)GUI到物理引擎

    今天老妈打电话来说和老爸吵架了... 真的是家家都有本难念的经啊.前后帮她分析了个半小时才帮她解开心结...现在想想老爸还是蛮可怜的,连分享的人都木有 讲的GUI都看睡着了...因为想着可以用NGUI ...

  6. 【英宝通Unity4.0公开课学习 】(三)脚本使用

    清明出去放松了一天. 看了下大姑爷,然后去大姑家吃了个午饭,下午三点左右出去找煤球耍,在他们学校和良乡镇逛了一下.当时感觉离北京好远好远啊... 其实不得不说现在的交通确实很方便,到哪都要不了几天,如 ...

  7. 【英宝通Unity4.0公开课学习 】(二)场景创建

    本讲共四节,貌似讲课老师的速度变快了,2倍速听不清了...调成了1.7倍...老师果然越来越熟练了啊! 而且最开始的萌妹纸也不再出现在视频里了,我当时还想着完全可以换成老师自己提问嘛! 不过有妹纸声音 ...

  8. 【英宝通Unity4.0公开课学习 】(一)资源管理

    经过多次面试后发现自己对Unity3D的框架缺乏一个整体的认识. 而前面由于离职等原因总是忙于修修补补,疲于奔命,感觉相当疲惫. 还好,后来经过调整,开始淡定了起来.得特别感谢一本书哇:<高效人 ...

  9. ASP.NET MVC5+EF6+EasyUI 后台管理系统(64)-WebApi与Unity注入

    系列目录 前言: 有时候我们系统需要开放数据给手机App端或其他移动设备,不得不说Asp.net WebApi是目前首选 本节记录Asp.net MVC WebApi怎么利用Unity注入.系列开头已 ...

随机推荐

  1. Mac 效率工具

    我的Mac开发环境 http://blog.csdn.net/feelang/article/details/45071249 iterm2 http://iterm2.com/documentati ...

  2. .edmx 文件概述(实体框架)

    (一) 认识.edmx文件结构 参考资料: .edmx 文件概述(实体框架) Entity Framework 全面教程详解(转) Entity Framework 教程 Entity Framewo ...

  3. Androidstudio报错UnsupportedClassVersionError

    报错信息 Error:java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.m ...

  4. VBA笔记(一)

    开启VBA编程环境--VBE 方法一:按<Alt+F11>组合建 方法二:查看代码 宏设置 当然启用宏的设置方式不同,宏的启动方式也不一样. 首先打开"office 按钮&quo ...

  5. Excel 锁定特定单元格 不允许更改

    1.首先,excel默认所有单元格都是被"锁定"的--当"锁定单元格"按钮被激活时. 因此,第一步,选择所有单元格,设置单元格格式,取消"保护&quo ...

  6. Java Web 学习链接

    解决JSP中文乱码问题:http://www.cnblogs.com/chengkai/articles/2171848.html 编程思想之多线程与多进程:http://blog.csdn.net/ ...

  7. Linux C 静态库(.a) 与 动态库(.so) 的详解

    库从本质上来说是一种可执行代码的二进制格式,可以被载入内存中执行.库分静态库和动态库两种. 一.静态库和动态库的区别 1.静态函数库 这类库的名字一般是libxxx.a:利用静态函数库编译成的文件比较 ...

  8. java基础知识(三)java关键字

    关键字是电脑语言事先定义的,是特别意义的标识符,又叫保留字.用来表示一种数据类型或程序的结构等,关键字不能用作变量名.类名.方法名或参数.java目前共有50个关键字,其中"const&qu ...

  9. java基础 集合类

    java集合类主要有以下集中: List结构的集合类: ArrayList类, LinkedList类, Vector类, Stack类 Map结构的集合类: HashMap类,Hashtable类 ...

  10. render()方法是render_to_response

    自django1.3开始:render()方法是render_to_response的一个崭新的快捷方式, 前者会自动使用 RequestContext.而后者必须coding 出来,这是最明显的区别 ...