我正在使用带有xml配置的autofac框架。我有一个问题,情况是这样的。我有一个名为ApplicationConfig的类,它包含一个实现接口的对象数组。我有两个方法Start和finish。其思想是在应用程序开始时调用start方法,在结束时结束。
为了设置对象,我调用了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。我在app的代码中使用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>我不知道如何为其他组件的构造函数指定参数。
因此,我希望能够在不编译的情况下配置应用程序。
发布于 2011-08-12 18:06:01
Autofac的XML配置不支持这种情况。
实现目标的最简单方法是在configuration对象上使用IStartable (http://code.google.com/p/autofac/wiki/Startable)和IDisposable,并且根本没有ApplicationConfig类。Autofac将自动调用Start()和Dispose()。
如果您确实需要让ApplicationConfig类编排开始/结束过程,您可以控制注册哪些IApplicationConfiguration组件。默认情况下,Autofac会将IApplicationConfiguration的所有实现注入到appConfigs构造函数参数中,因为它是一个数组,并且Autofac对数组类型有特殊处理。只需为您需要的每个IApplicationConfiguration添加<component>标签,并排除那些您不需要的。
希望这能帮上忙
尼克
https://stackoverflow.com/questions/6872653
复制相似问题