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开发框架,是基于 ...
随机推荐
- 51Nod 1352 集合计数(扩展欧几里德)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1352 题目大意: 给出N个固定集合{1,N},{2,N-1} ...
- HDU 3533 Escape(BFS+预处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=10 ...
- hdu 5692(dfs序+线段树,好题)
Snacks Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- ultra-console
console.__proto__.styleText = function (option) { if (!option) { console.groupCollapsed('请输入option') ...
- [实战]MVC5+EF6+MySql企业网盘实战(14)——逻辑重构
写在前面 上篇文章关于修改文件夹和文件名称导致的找不到物理文件的问题,这篇文章将对其进行逻辑的修改. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6 ...
- 使用 JQuery 实现将 table 按照列排序
使用 JQuery 实现将 table 按照列排序 使用 JQuery 实现将 table 按照列排序 代码如下 <!DOCTYPE html> <html> <head ...
- phpstorm+xdebug远程调试设置
1 xdebug扩展安装 1.1 xdebug扩展安装: 2 服务器PHP配置 3 phpstorm设置 3.1 添加远程debug 3.2 phpstorm设置: 4 浏览器插件安装 4.1 chr ...
- linux中使用rm命令将文件移到回收站的方法
今天在终端下,看到我的用户目录下有个-的文件夹(maven生成),相要删除收回点空间,习惯性的用命令 rm -rf ~ ,一回车,猛然想起的时候已经来不及了,世界一下子清静了,想死的心都有了! 没错, ...
- ref:学习笔记 UpdateXml() MYSQL显错注入
ref:https://www.cnblogs.com/MiWhite/p/6228491.html 学习笔记 UpdateXml() MYSQL显错注入 在学习之前,需要先了解 UpdateXml( ...
- enumerate()和map()函数用法
一.python enumerate用法 先出一个题目: 1.有一 list= [1, 2, 3, 4, 5, 6] 请打印输出: 0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输出 ...