2011-07-29 71 views
2

我正在使用xml配置的autofac框架。我有一个问题,这里是情况。我有一个名为ApplicationConfig的类,它包含一个实现接口的对象数组。我有两种方法开始和结束。这个想法是在应用程序开始时调用start方法,在结束时调用Finish。autofac xml配置

要设置对象,我调用具有可变数量参数的SetConfigurations。

下面是代码:

public class ApplicationConfig 
{ 
    private IAppConfiguration[] configurators; 

    public void SetConfigurations(params IAppConfiguration[] appConfigs) 
    { 
     this.configurators = appConfigs ?? new IAppConfiguration[0]; 
    } 

    public void Start() 
    { 
     foreach (IAppConfiguration conf in this.configurators) 
      conf.OnStart(); 
    } 

    public void Finish() 
    { 
     foreach (IAppConfiguration conf in this.configurators) 
      conf.OnFinish(); 
    } 
} 

XML

<component type="SPCore.ApplicationConfig, SPCore" 
    instance-scope="single-instance"> 
</component> 

我只是想知道,如果我可以通过XML配置,将在应用程序的开头开始,在SetConfigurations代替的组件。我在应用的代码中使用SetConfigurations

所以我想要这样的东西。

类的构造函数

public ApplicationConfig(params IAppConfiguration[] appConfigs) 
{ 
    this.configurators = appConfigs; 
} 

XML

<component type="SPCore.ApplicationConfiguration.ConfigurationParamters, SPCore" 
    instance-scope="single-instance"> 
</component> 

<component type="SPCore.ApplicationConfig, SPCore" instance-scope="single-instance"> 
    <parameters> 
     <parameter>--Any componet--</parameter> 
     <parameter>--Any componet--</parameter> 
     .... 
     .... 
     <parameter>--Any componet--</parameter> 
    </parameters> 
</component> 

我不知道如何指定为其它部件的构造函数的参数..

所以,我希望能够配置应用程序而无需编译。

回答

2

Autofac的XML配置不支持这种情况。

最简单的方式来实现你追求的是在配置对象使用IStartable(http://code.google.com/p/autofac/wiki/Startable)和IDisposable,而不是有一个ApplicationConfig类在所有。 Autofac将自动呼叫Start()Dispose()

如果确实需要使用ApplicationConfig类来编排开始/结束过程,则可以控制哪些IApplicationConfiguration组件已注册。默认情况下,Autofac将全部实现IApplicationConfiguration注入appConfigs构造函数参数中,因为它是一个数组,Autofac对数组类型有特殊处理。只需包含您需要的每个IApplicationConfiguration<component>标签,并排除您不需要的标签。

希望这有助于

尼克