autofac xml 配置

发布于 2024-11-26 17:41:24 字数 1945 浏览 1 评论 0原文

我正在使用带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无名指的心愿 2024-12-03 17:41:24

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) and IDisposable on the configuration objects, and not have an ApplicationConfig class at all. Autofac will call Start() and Dispose() automatically.

If you do need to have the ApplicationConfig class orchestrate the start/finish process, you can control which IApplicationConfiguration components are registered. By default, Autofac will inject all implementations of IApplicationConfiguration into the appConfigs constructor parameter, because it is an array and Autofac has special handling for array types. Just include <component> tags for each of the IApplicationConfigurations you need, and exclude those you don't.

Hope this helps,

Nick

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文