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开发框架,是基于 ...
随机推荐
- JAVA封装消息中间件调用二(kafka消费者篇)
上一遍我简单介绍了kafka的生成者使用,调用方式比较简单,今天我给大家分享下封装kafka消费者,作为中间件,我们做的就是最大程度的解耦,使业务方接入我们依赖程度降到最低. 第一步,我们先配置一个消 ...
- 计算 Python (list or array) 相同索引元素相等的个数
上代码: a = [2, 3, 3, 1, 1, 3, 3, 3, 2, 1, 3, 1, 3, 2, 2, 2, 3, 1, 2, 3, 2, 3, 1, 1, 2, 1, 1, 1, 2, 2, ...
- mac环境下使用brew安装kafka
1.安装kafka brew install kafka note: ·kafka使用zookeeper管理,安装过程会自动安装zookeeper ·安装目录:/usr/local/Cellar/ka ...
- 783. Minimum Distance Between BST Nodes
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- centos7 开放端口号
firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --reload
- 救济金发放(UVa133)
题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...
- 【PAT】1012. 数字分类 (20)
1012. 数字分类 (20) 给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算 ...
- 【LOJ】#2129. 「NOI2015」程序自动分析
题解 开始是想两个并查集的 和A相等,和A不相等 如果AB相等就连 A 相等,B相等 B不相等 A不相等 如果AB不相等就连 A不相等,B相等 B相等,A不相等 但是显然不对,因为和A不相等的不一定和 ...
- CountDownLatch 使用方法
CountDownLatch 使用方法 import java.util.concurrent.CountDownLatch; public class TestCountDownLatch { pu ...
- 【ASP.NET】:Ckeditor+Fckeditor的使用
首先这三个文件:下载ckeditor和ckeditor_aspnet_3.6.4和ckfinder 然后把这三个文件复制到项目根目录下 添加引用CKEditor.NET.dll CKFind ...