Unity Application Block 学习笔记之一使用接口

     章,想.

     ,合,象.啊,作. 个config,Map,西什么西xxx,据configXXX.,(Contain).

     Microsoft Enterprise Library4.1UnityApplication Block看Unity document,.

     Contain求:Mapping,.

     个interface ():

IDataProcessor

     试: 化Contain,型Mapping,Type

UnityTest

   到UnitTest.PromptDataProcessor. OK个DataProcessor,

UnityTest

   false, 默认例,Cotain个LifetimeManager念,

种:

     TransientLifetimeManager  默认,

     ContainerControlledLifetimeManager 现Singleton

     ExternallyControlledLifetimeManager

     PerThreadLifetimeManager 线

     SynchronizedLifetimeManager  , 

     ContainerControlledLifetimeManager,:

ContainerControlledLifetimeManagerTest

 . OK,ExternallyControlledLifetimeManagerContainerControlledLifetimeManager,

ExternallyControlledLifetimeManager

 ContainerControlledLifetimeManager想,使ExternallyControlledLifetimeManager,

容器仅保弱引用(可被垃圾回收),意味着可能是单一实例(没有回收)可能不是.

     PerThreadLifetimeManager,:

PerThreadLifetimeManagerTest

 ,线线.

求: map?,Unity Contain册,

Map,:

NamedRegister

PrintDataProcessor PromptDataProcessor.

Type + NameMap的,两个实例处理不同方面的对象.

,

Register a type as two Instance

Unity Contain 持instance 觉Contain 扮演 Service Locator,

Register Instance Test

下UnityContainerResloveAll, instance,下:

ResolveAll Test

  ,If the container does not contain any named (non-default) mappings for the specified type, it will return null (in C#) or Nothing .计: ,ResolveAll Resolve. :

ResolveAll Test

下面来看看具体的依赖注入方式,依据文档描述,Unity具体支持三种依赖注入,Constructor injection,Property injection,Method Injection.首先来看Constructor injection.

Constructor Injection

构造函数注入方式有二种,其一是当创建对象只有一个构造函数时,自动依赖注入。其二是有多个构造函数时,使用InjectionConstructorAttribute,简单的先看: 

Constructor Injection

看看有两个构造函数并用InjectionConstructor指定注入

Constructor Injection

 

 

现在假设构造函数依赖一个抽象一个特定的interface,

Constructor Injection

 PropertyInjection

实现只有一种方式,通过给相应的属性标识为Dependency

 

PropertyInjection

 

Method Injection
实现也只有一种方式, 把相应的注入方法标识为InjectionMethod

InjectionMethod

 

Buildup方法
如果你已经有一个对象,但在每次使用前想使初始化一次,可以使用BuildUp方法达到这个目的.

 

BuildUp

 

Reference:

First Steps with Unity
http://www.nablasoft.com/alkampfer/index.php/2009/01/16/first-steps-with-unity/

IoC框架介绍
http://blog.csdn.net/wanghao72214/archive/2009/03/08/3969594.aspx

Martin Fowler的介绍文章

http://martinfowler.Mcom/articles/injection.html

原文地址:https://www.cnblogs.com/jjyjjyjjy/p/1428145.html