最近公司用到了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. 【Beta版本】冲刺随笔汇总

    [Beta版本]冲刺计划及安排 [Beta版本]冲刺-Day1 [Beta版本]冲刺-Day2 [Beta版本]冲刺-Day3 [Beta版本]冲刺-Day4 [Beta版本]冲刺-Day5 [Bet ...

  2. Python的方法解析顺序(MRO)[转]

    本文转载自: http://hanjianwei.com/2013/07/25/python-mro/ 对于支持继承的编程语言来说,其方法(属性)可能定义在当前类,也可能来自于基类,所以在方法调用时就 ...

  3. linux下centos的网络连接

  4. MySQL5.0数据库的安装

    ======================= 未完待续,持续更新中... -------------------------------------------------

  5. arcgis engine 监听element的添加、更新和删除事件(使用IGraphicsContainerEvents)

    IGraphicsContainerEvents Interface 如何监听 element事件? 如,当我们在Mapcontrol上添加.删除.更新了一个Element后,如何捕捉到这个事件?   ...

  6. .NET 多个程序配置文件合并到主app.config

    .NET 多个程序配置文件合并到主app.config

  7. Less的简单使用

    在浏览器中使用LESSCSS 浏览器端使用是在使用LESS开发时最直观的一种方式.如果是在生产环境中,尤其是对性能要求比较高的场合,建议使用node或者其它第三方工具先编译成CSS再上线使用. 浏览器 ...

  8. jQuery入门(4)jQuery中的Ajax应用

    jQuery入门(1)jQuery中万能的选择器 jQuery入门(2)使用jQuery操作元素的属性与样式 jQuery入门(3)事件与事件对象 jQuery入门(4)jQuery中的Ajax()应 ...

  9. vi 常用命令

    1.关于退出 :wq!  ----强制保存退出 :wq  ---- 保存退出 ZZ  ---- 作用和:wq一样,(注意Z是大写的,并且不是在命令模式) :q  ---- 退出 :q!  ---  强 ...

  10. URL转义

    在处理一些url的时候常常会出现一些让人意想不到的情况,比如:当图片的链接中有空格的时候, this.src='http://img.cits-sh.com/UploadImgs/beihaidao2 ...