Castle IOC容器实践之FactorySupport Facility(十三)

摘要:FactorySupport Facility允许我们使用已经存在的工厂来创建组件的实例,可以把已经存在的对象模型加入到容器中,以便它能够使用自动装配。它使用了以下两种处理策略:

1.使用访问器来访问组件的实例

2.使用静态方法或者实例方式来访问组件的实例

 

主要内容

1.概述

2.为什么需要FactorySupport Facility

3.如何使用

4.常见的配置示例

5.实现原理浅析

 

一.概述

FactorySupport Facility允许我们使用已经存在的工厂来创建组件的实例,可以把已经存在的对象模型加入到容器中,以便它能够使用自动装配。它使用了以下两种处理策略:

1.通过访问器来访问组件的实例

2.通过静态方法或者实例方式来访问组件的实例

二.为什么需要FactorySupport Facility

为了回答这个问题,我们先来看一个简单的使用工厂的例子,如下代码所示:

public   interface  IComponent 

ExpandedBlockStart.gifContractedBlock.gif
{
    
void Display();
}


public   class  MyComponent : IComponent

ExpandedBlockStart.gifContractedBlock.gif
{
    
public MyComponent()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{   

    }


    
public void Display()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        Console.WriteLine(
"MyComponent");
    }

}


public   class  MyFactory

ExpandedBlockStart.gifContractedBlock.gif
{
    
public MyFactory()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

    }


    
public IComponent Create()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return new MyComponent();
    }

}

现在要在程序中使用MyComponent的实例,简单一点会这样去写:

public   class  App

ExpandedBlockStart.gifContractedBlock.gif
{
    
static void Main()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        MyFactory factory 
= new MyFactory();

        IComponent comp 
= factory.Create();

        comp.Display();

    }

}

正如你所看到的,在程序中使用new关键字创建了一个工厂的实例,再由工厂得到组件的实例。现在我们看使用FactorySupport Facility后带来了什么好处。工厂和组件的代码不变,仍然是:

public   interface  IComponent 

ExpandedBlockStart.gifContractedBlock.gif
{
    
void Display();
}



public   class  MyComponent : IComponent

ExpandedBlockStart.gifContractedBlock.gif
{
    
public MyComponent()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

    }


    
public void Display()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        Console.WriteLine(
"MyComponent");
    }

}


public   class  MyFactory

ExpandedBlockStart.gifContractedBlock.gif
{
    
public MyFactory()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

    }


    
public IComponent Create()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return new MyComponent();
    }

}

配置文件,现在你可以不用理解怎么去配置,后面会讲到。

<? xml version="1.0" encoding="utf-8"  ?>

< configuration >

    
< facilities >

        
< facility  id ="factorysupport"  type ="Castle.Facilities.FactorySupport.FactorySupportFacility, Castle.MicroKernel"   />

    
</ facilities >

    
< components >

        
< component  id ="myfactory"  type ="FactorySupportDemo.MyFactory,FactorySupportDemo"   />

        
< component  id ="mycomp"  type ="FactorySupportDemo.MyComponent,FactorySupportDemo"

            factoryId
="myfactory"  factoryCreate ="Create" ></ component >

    
</ components >

</ configuration >

使用组件的实例

public   class  App

ExpandedBlockStart.gifContractedBlock.gif
{
    
static void Main()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        IWindsorContainer container 
= new WindsorContainer("http://www.cnblogs.com/Basic.xml");


        IComponent instance 
= (IComponent) container["mycomp"];

        instance.Display();

    }

}

看到了吗?我们并没有使用new关键字创建任何对象,只是把工厂、组件等通过配置文件抛给了IOC容器,再从容器中往外取的时候,就可以直接取到组件的实例,这些实例是由工厂自动创建的,这正是FactorySupport Facility的作用。

三.如何使用

知道了为什么需要FactorySupport Facility之后,下面就来看如何使用Facility。在前面的代码中大家都已经看到了,使用FactorySupport Facility是非常的简单。

1.加入FactorySupport Facility到容器中,可以使用代码加入也可以使用配置文件。

使用代码加入:

IWindsorContainer container  =   new  WindsorContainer( " http://www.cnblogs.com/Basic.xml " );


container.AddFacility(
" factorysupport " , new  Castle.Facilities.FactorySupport.FactorySupportFacility());

使用配置文件:

< facilities >

    
< facility  id ="factorysupport"  type ="Castle.Facilities.FactorySupport.FactorySupportFacility, Castle.MicroKernel"   />

</ facilities >

2.加入组件时指定使用的是哪种方式

如果使用访问器只需要指定访问器的名称就可以了:

< component  id ="accessor.comp"  

    type
="Castle.Facilities.FactorySupport.Tests.Components.SingletonWithAccessor, Castle.Facilities.FactorySupport.Tests"

    instance-accessor
="Instance" >

</ component >

如果使用工厂方法,需要指定工厂ID和工厂方法:

< component  id ="mycomp"  

            type
="Castle.Facilities.FactorySupport.Tests.Components.MyComp, Castle.Facilities.FactorySupport.Tests"

            factoryId
="mycompfactory"  factoryCreate ="Create" >

</ component >

四.常见的配置示例

这是一些常见的配置示例,来自于Castle自带的测试代码中。

1.访问器配置示例

有这样一个单件类

public   class  SingletonWithAccessor

ExpandedBlockStart.gifContractedBlock.gif
{
    
private static readonly SingletonWithAccessor instance = new SingletonWithAccessor();

    
private SingletonWithAccessor()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

    }


    
public static SingletonWithAccessor Instance

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return instance; }
    }

}

配置文件如下:

<? xml version="1.0" encoding="utf-8"  ?>

< configuration >

    
< facilities >

        
< facility  id ="factorysupport"  type ="Castle.Facilities.FactorySupport.FactorySupportFacility, Castle.MicroKernel"   />

    
</ facilities >

    
< components >

        
< component  id ="accessor.comp"  type ="Castle.Facilities.FactorySupport.Tests.Components.SingletonWithAccessor, Castle.Facilities.FactorySupport.Tests"

            instance-accessor
="Instance" ></ component >

    
</ components >

</ configuration >

2.普通工厂配置示例

组件类和工厂类如下

public   class  MyComp

ExpandedBlockStart.gifContractedBlock.gif
{
    
public MyComp()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

    }

}


public   class  MyCompFactory

ExpandedBlockStart.gifContractedBlock.gif
{
    
public MyComp Create()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return new MyComp();
    }

}

配置文件如下:

<? xml version="1.0" encoding="utf-8"  ?>

< configuration >

    
< facilities >

        
< facility  id ="factorysupport"  type ="Castle.Facilities.FactorySupport.FactorySupportFacility, Castle.MicroKernel"   />

    
</ facilities >

    
< components >

        
< component  id ="mycompfactory"  type ="Castle.Facilities.FactorySupport.Tests.Components.MyCompFactory, Castle.Facilities.FactorySupport.Tests"   />

        
< component  id ="mycomp"  type ="Castle.Facilities.FactorySupport.Tests.Components.MyComp, Castle.Facilities.FactorySupport.Tests"

            factoryId
="mycompfactory"  factoryCreate ="Create" ></ component >

    
</ components >

</ configuration >

3.带参工厂配置示例

组件类和工厂类如下:

public   class  MyComp

ExpandedBlockStart.gifContractedBlock.gif
{
    
private string storeName;

    
private IDictionary props;

    
internal MyComp()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

    }


    
internal MyComp(String storeName, IDictionary props)

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
this.storeName = storeName;

        
this.props = props;
    }


    
public string StoreName

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return storeName; }
    }


    
public IDictionary Props

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return props; }
    }

}


public   class  MyCompFactory2

ExpandedBlockStart.gifContractedBlock.gif
{
    
public MyCompFactory2()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

    }


    
public MyComp Create(String storeName, IDictionary props)

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return new MyComp(storeName, props);
    }

}

配置文件如下:

<? xml version="1.0" encoding="utf-8"  ?>

< configuration >

    
< facilities >

        
< facility  id ="factorysupport"  type ="Castle.Facilities.FactorySupport.FactorySupportFacility, Castle.MicroKernel"   />

    
</ facilities >

    
< components >

       
< component  id ="mycompfactory"  type ="Castle.Facilities.FactorySupport.Tests.Components.MyCompFactory2, Castle.Facilities.FactorySupport.Tests" ></ component >

        
< component  id ="mycomp"  type ="Castle.Facilities.FactorySupport.Tests.Components.MyComp, Castle.Facilities.FactorySupport.Tests"

            factoryId
="mycompfactory"  factoryCreate ="Create" >

            
< parameters >

                
< storeName > MyStore </ storeName >

                
< props >

                    
< dictionary >

                        
< entry  key ="key1" > item1 </ entry >

                        
< entry  key ="key2" > item2 </ entry >

                    
</ dictionary >

                
</ props >

            
</ parameters >

        
</ component >

    
</ components >

</ configuration >

4.使用自动装配工厂配置示例

组件类和工厂类如下:

public   interface  IMyService

ExpandedBlockStart.gifContractedBlock.gif
{

}


public   class  MyComp

ExpandedBlockStart.gifContractedBlock.gif
{
    
private IMyService serv;

    
internal MyComp()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

    }


    
internal MyComp(IMyService serv)

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
this.serv = serv;
    }


    
public IMyService Service

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return serv; }
    }

}


public   class  MyCompFactory3

ExpandedBlockStart.gifContractedBlock.gif
{
    
public MyCompFactory3()

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

    }


    
public MyComp Create(IMyService serv)

ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return new MyComp(serv);
    }

}

配置文件如下:

<? xml version="1.0" encoding="utf-8"  ?>

< configuration >

    
< facilities >

        
< facility  id ="factorysupport"  type ="Castle.Facilities.FactorySupport.FactorySupportFacility, Castle.MicroKernel"   />

    
</ facilities >

    
< components >

        
< component  id ="myserv"  service ="Castle.Facilities.FactorySupport.Tests.Components.IMyService, Castle.Facilities.FactorySupport.Tests"

            type
="Castle.Facilities.FactorySupport.Tests.Components.DefaultMyService, Castle.Facilities.FactorySupport.Tests"   />

       
< component  id ="mycompfactory"  type ="Castle.Facilities.FactorySupport.Tests.Components.MyCompFactory3, Castle.Facilities.FactorySupport.Tests" ></ component >

        
< component  id ="mycomp"  type ="Castle.Facilities.FactorySupport.Tests.Components.MyComp, Castle.Facilities.FactorySupport.Tests"

            factoryId
="mycompfactory"  factoryCreate ="Create" >

            
<!--  parameters is not necessary_u97 ?s it will auto wire  --> </ component >

    
</ components >

</ configuration >

五.实现原理浅析

FactorySupport Facility的实现也是非常的简单,主要是通过两个ComponentActivator来实现,分别为AccessorActivatorFactoryActivator,根据配置文件的不同调用不同的Activator

String instanceAccessor  =  model.Configuration.Attributes[ " instance-accessor " ];

String factoryId 
=  model.Configuration.Attributes[ " factoryId " ];

String factoryCreate 
=  model.Configuration.Attributes[ " factoryCreate " ];


if  (instanceAccessor  !=   null )

ExpandedBlockStart.gifContractedBlock.gif
{

    model.ExtendedProperties.Add( 
"instance.accessor", instanceAccessor );

    model.CustomComponentActivator 
= typeof(AccessorActivator);

}


else   if  (factoryId  !=   null )

ExpandedBlockStart.gifContractedBlock.gif
{

    model.ExtendedProperties.Add( 
"factoryId", factoryId );

    model.ExtendedProperties.Add( 
"factoryCreate", factoryCreate );

    model.CustomComponentActivator 
= typeof(FactoryActivator);

}

好了,关于FactorySupport Facility就介绍到这儿,更多Castle的文章您可以访问我的blog:http://terrylee.cnblogs.com 《Castle 开发系列文章》。

 

PDF版本下载/Files/Terrylee/FactorySupportFacility.rar

示例代码下载/Files/Terrylee/FactorySupportDemo.rar

转载于:https://www.cnblogs.com/Madream/articles/1680467.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值