Enterprise Library:Unity的几个注意事项
背景
在.Net平台中,几乎所有的Ioc容器在注册方面都不一致,使用Unity需要注意几个事项,咱们通过实验进行验证一下。
验证的内容:
- 集合的获取。
- 生命周期管理。
实验
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 using Microsoft.Practices.Unity;
8
9 namespace UnityStudy
10 {
11 class Program
12 {
13 static void Main(string[] args)
14 {
15 UnityContainer container = new UnityContainer();
16
17 container.RegisterType<ITest, Test>(new PerThreadLifetimeManager());
18 container.RegisterType<ITest, TestA>("A");
19 container.RegisterType<ITest, TestB>("B");
20 container.RegisterType<ITest, Test>("C");
21 container.RegisterType<Test, Test>();
22 container.RegisterType<IOther, Test>();
23
24 Console.WriteLine(container.ResolveAll<ITest>().Count());
25 //输出:3
26
27 Console.WriteLine(container.Resolve<ITest>().GetHashCode());
28 Console.WriteLine(container.Resolve<ITest>().GetHashCode());
29
30 Console.WriteLine(container.Resolve<Test>().GetHashCode());
31 Console.WriteLine(container.Resolve<Test>().GetHashCode());
32
33 Console.WriteLine(container.Resolve<IOther>().GetHashCode());
34 Console.WriteLine(container.Resolve<IOther>().GetHashCode());
35 //输出:上边六行输出内容一样
36
37 Console.WriteLine(container.ResolveAll<ITest>().Last().GetHashCode());
38 Console.WriteLine(container.ResolveAll<ITest>().Last().GetHashCode());
39 //输出:输出两行完全不一样
40 }
41 }
42
43 public interface ITest { }
44
45 public interface IOther { }
46
47 public class Test : ITest, IOther { }
48
49 public class TestA : ITest { }
50
51 public class TestB : ITest { }
52 }
输出

结论
- ResolveAll只返回命名注册。
- 生命周期和具体类型+注册的名字有关系。
备注
使用Unity获取具体类型是不用注册的,有些Ioc容器要求所有类型都必须先注册才能获取。
Enterprise Library:Unity的几个注意事项的更多相关文章
- 通过fsharp 使用Enterprise Library Unity 3 - 三种拦截模式的探索
这篇就三种拦截模式进行一下探索. 特性总结 类型 特点 其它 InterfaceInterceptor Innstance 仅单接口 类内部函数互相引用无法引起拦截行为 TransparentPr ...
- 通过fsharp 使用Enterprise Library Unity 2
接着Depandency Injection继续. 最想做的还是用现成的程序模块对程序进行行为注入.只是不急,在此之前自己写一个接口对象观察一下IInterceptionBehavior接口的功效. ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(八) Unity Dependency Injection and Interception
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(八) Unity Dependency Injection and Interception 依赖注入容器Uni ...
- 微软Enterprise Library 4.1和Unity 1.2
说明 微软模式与实践团队今天发布了Enterprise Library 4.1和Unity 1.2版本,这次发布的主要新特性如下: 1. 支持Visual Studio 2008 SP1 2. Uni ...
- Enterprise Library系列文章目录(转载)
1. Microsoft Enterprise Library 5.0 系列(一) Caching Application Block (初级) 2. Microsoft Enterprise Lib ...
- Enterprise Library 5.0 系列教程
1. Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (初级) 2. Microsoft Enterprise L ...
- 微软企业库5.0 学习之路——扩展学习篇、库中的依赖关系注入(重构 Microsoft Enterprise Library)[转]
这篇文章是我在patterns & practices看到的一篇有关EntLib5.0的文章,主要介绍了EntLib5.0的这次的架构变化由来,觉得很不错,大家可以看一下! 在过去几年中,依赖 ...
- 在数据库访问项目中使用微软企业库Enterprise Library,实现多种数据库的支持
在我们开发很多项目中,数据访问都是必不可少的,有的需要访问Oracle.SQLServer.Mysql这些常规的数据库,也有可能访问SQLite.Access,或者一些我们可能不常用的PostgreS ...
- 基于Enterprise Library的Winform开发框架实现支持国产达梦数据库的扩展操作
由于一个客户朋友的需求,需要我的Winform开发框架支持国产达梦数据库的操作,这个数据库很早就听过,但是真正一般项目用的很少,一般在一些特殊的项目可能需要用到.由于我的Winform开发框架,是基于 ...
随机推荐
- android Webview Html5 相关文章
Android WebView的使用集锦(支持Html5) http://blog.csdn.net/l_215851356/article/details/69239643 WebView详解与简单 ...
- 很好的开源UI框架Chico UI
介绍一个很好的开源的UI框架,依赖于jquery 官网:http://www.chico-ui.com.ar/ 以下是相关截图: 消息提示 自动完成 分页,列表 Chico UI是什么? Chico ...
- shell 从变量中切割字符串
1. 在shell变量中切割字符串 shell中截取字符串的方法有很多中,${expression}一共有9种使用方法.${parameter:-word}${parameter:=word}${pa ...
- 无框架完整搭建安卓app及其服务端(一)
技术背景: 我的一个项目做的的是图片处理,用 python 实现图片处理的核心功能后,想部署到安卓app中,但是对于一个对安卓和服务器都一知半解的小白来说要现学的东西太多了. 而实际上,我们的项目要求 ...
- Python开发基础-Day32 进程间通信、进程池、协程
进程间通信 进程彼此之间互相隔离,要实现进程间通信(IPC),multiprocessing模块支持两种形式:队列和管道,这两种方式都是使用消息传递的. 进程队列queue 不同于线程queue,进程 ...
- PlayMaker布局技巧:预览GUI界面
PlayMaker布局技巧:预览GUI界面 PlayMaker提供丰富的动作用来构建界面.对于复杂界面,每次通过调试方式查看效果,会非常麻烦.这个时候,开发者可以考虑使用PlayMaker GUI ...
- jQuery before 和 after
A.after(B) ==== B.insertAfter(A) B 放在 A 的后面A.before(B) ==== B.insertBefore(A) B 放在 A 的前面 A.append(B) ...
- vijos Warcraft III 守望者的烦恼
题解 转移方程好写吧 一个一维递推式 然后我们可以构造矩阵优化 嗯,最近学一下递推优化 代码 #include<cstdio> #include<cstring> #inclu ...
- python实现图片验证码
1 验证基础知识1.1 Python生成随机验证码,需要使用PIL模块. # 安装 pip3 install pillow 1.2 创建图片 from PIL import Image img = I ...
- hdu 3001 三进制状压
题意:tsp问题,但是每个点可以最多走两次 链接:点我 转移方程见代码 #include<iostream> #include<cstdio> #include<cstr ...