开发者

autofac xml configuration

开发者 https://www.devze.com 2023-03-23 23:50 出处:网络
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'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 Fin开发者_JAVA百科ish 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消