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开发框架,是基于 ...
随机推荐
- Codeforces 918C The Monster(括号匹配+思维)
题目链接:http://codeforces.com/contest/918/problem/C 题目大意:给你一串字符串,其中有'('.')'.'?'三种字符'?'可以当成'('或者')'来用,问该 ...
- HDU 1043 Eight(反向BFS+打表+康托展开)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...
- Synergy,一个软件团队质量改进之路之一 --- 规划
关于质量改进 ISO9001:2000的核心思想是持续改进 ISO关于质量管理有8项原则: 原则一: 以顾客为关注焦点 原则二: 领导作用 原则三: 全员参与 原则四: 过程方法 原则五:管理的系统方 ...
- 数据库介绍及MySQL安装
阅读目录 一.数据库是什么? 二.数据库特点 三. 什么是数据库管理系统(DataBase Management System 简称DBMS) 四.数据库服务器.数据管理系统.数据库.表与记录的关系( ...
- IEEEXtreme 10.0 - Ellipse Art
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Ellipse Art 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank ...
- IEEEXtreme 10.0 - Always Be In Control
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Always Be In Control 题目来源 第10届IEEE极限编程大赛 https://www.h ...
- bzoj1452 最大流
很明显最大流.. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #defi ...
- C++后台研发面试总结
前言: 从中秋到国庆这几天面试了几家公司,有大公司也有小公司,连续几天面试没有系统的整理整理,正好有时间系统的整理一下,好多考点牛客的大佬们都分享过了,虽然每个人的方向不相同,不过多看一些总能找到一些 ...
- 关于在vue里使用脚手架空行、空格会报错的问题
第一种方法: 重新用脚手架安装项目,在命令行里选择Use ESLint to lint your code?这项是输入 n 第二种方法: 找到build文件夹下的 webpack.base.conf ...
- Python类总结-多态及鸭子类型
Python天生支持多态. 什么是多态: 一类事务的多种形态. 多态的一个例子 class Alipay(): def pay(self,money): print('用支付宝支付了%s元' % mo ...