autofac xml 配置
我正在使用带有 xml 配置的 autofac 框架。我有一个问题,情况是这样的。我有一个名为 ApplicationConfig 的类,它包含一个实现接口的对象数组。我有两种方法开始和结束。这个想法是在应用程序开始时调用 start 方法,在应用程序结束时调用 Finish 方法。
为了设置对象,我调用 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
。
所以我想要这样的东西。
class constructor
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>
我不知道如何为其他组件的构造函数指定参数。
因此,我希望能够在不编译的情况下配置应用程序。
I'm using autofac framework with xml configuration. I have a question, here is the situation. I have a class called ApplicationConfig that holds an array of objects thats implements an interface. And I have two methods Start and finish. The idea is call the start method at the beginning of the application and Finish at the end.
To set the objects I call SetConfigurations which has variable numbers of arguments.
Here is the code:
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>
I just wonder if I can via xml configure the components that will start at the begining of the app, in stead of SetConfigurations
. I use SetConfigurations
in app's code.
So i want something like this.
class constructor
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>
I don't know how to specify parameters for the constructor that are other components..
So, I want to be able to configure the app without compiling.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Autofac 的 XML 配置不支持这种情况。
实现您的目标的最简单方法是使用
IStartable
(http://code.google.com/p/autofac/wiki/Startable) 和IDisposable
配置对象,并且根本没有ApplicationConfig
类。 Autofac 将自动调用Start()
和Dispose()
。如果您确实需要让
ApplicationConfig
类协调开始/完成过程,您可以控制注册哪些IApplicationConfiguration
组件。默认情况下,Autofac 会将IApplicationConfiguration
的所有实现注入到appConfigs
构造函数参数中,因为它是一个数组,并且 Autofac 对数组类型有特殊处理。只需为您需要的每个IApplicationConfiguration
添加
标记,并排除您不需要的标记。希望这有帮助,
尼克
Autofac's XML configuration doesn't support this scenario.
The simplest way to achieve what you're after is to use
IStartable
(http://code.google.com/p/autofac/wiki/Startable) andIDisposable
on the configuration objects, and not have anApplicationConfig
class at all. Autofac will callStart()
andDispose()
automatically.If you do need to have the
ApplicationConfig
class orchestrate the start/finish process, you can control whichIApplicationConfiguration
components are registered. By default, Autofac will inject all implementations ofIApplicationConfiguration
into theappConfigs
constructor parameter, because it is an array and Autofac has special handling for array types. Just include<component>
tags for each of theIApplicationConfiguration
s you need, and exclude those you don't.Hope this helps,
Nick