注入应用程序配置的最佳方式

发布于 2024-12-08 05:39:16 字数 1145 浏览 0 评论 0原文

好吧,我正在进军这个奇妙的网站,带着一个关于将配置设置注入应用程序组件的正确方法的问题。所以,概述是:我有一个用 C# .Net 3.5 编写的应用程序。它由 3 个组件组成 - 核心、数据和服务。数据&服务程序集需要从 app.config 检索设置,这是通过设置文件完成的,例如

代码:

public static String RequestQueueConnectionString
{
    get { return ConnectionSettings.Default.RequestQueueConnectionString; }
}

配置:

<applicationSettings>
  <MyNamespace.Data.ConnectionSettings>
    <setting name="RequestQueueConnectionString" serializeAs="String">
    ...

现在,程序集都是使用 IoC 的 StructureMap 设置的 - 在我看来,这应该提供我正在寻找的答案,但我只是看不到它!

IoC:

public static void ConfigureStructureMap(IContainer container)
{
    container.Configure(x => ...
    ...

我想要做的是将已填充到 IoC 容器中的配置类注入,以便这些设置用于该程序集,而不是设置文件/app.config 中指定的设置。所以也许:

public static void ConfigureStructureMap(IContainer container, MyConfigClass config)
{
    container.Configure(x => x.For<DataConfig>()
                              .Singleton()
                              .Use ???
    ...

我希望我在这里提供了足够的细节 - 如果我没有提供足够的细节,请原谅新手,请让我知道还有什么对回答这个问题有帮助!

Well, I'm making my foray into this fantastic site with a question about the correct way to inject configuration settings into application components. So, the overview is : I have an application written in C# .Net 3.5. It consists of 3 assemblies - a Core, a Data and a Service. The data & service assemblies require settings retrieved from the app.config, which is done via a settings file, eg.

Code :

public static String RequestQueueConnectionString
{
    get { return ConnectionSettings.Default.RequestQueueConnectionString; }
}

Config :

<applicationSettings>
  <MyNamespace.Data.ConnectionSettings>
    <setting name="RequestQueueConnectionString" serializeAs="String">
    ...

Now, the assemblies are all setup using StructureMap for IoC - which to my mind should provide the answer to what I am looking for, but I just can't quite see it!

IoC :

public static void ConfigureStructureMap(IContainer container)
{
    container.Configure(x => ...
    ...

What I want to be able to do is to inject a configuration class already populated into the IoC container such that those settings are used for that assembly, NOT those specified in the settings file / app.config. So perhaps :

public static void ConfigureStructureMap(IContainer container, MyConfigClass config)
{
    container.Configure(x => x.For<DataConfig>()
                              .Singleton()
                              .Use ???
    ...

I hope I have provided enough details here - forgive a newbie if I have not and please let me know what else would be helpful in answering this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

抚你发端 2024-12-15 05:39:16

因此,经过大量搜索和反复试验后,我收到了 @default.kramer 的链接,我正确地遵循了该链接!再次经过一些尝试和错误(我认为最好的方法),我设法得到了我正在寻找的解决方案。现在,虽然您可以点击链接(我强烈建议这样做),但我将在实施时发布我的问题的解决方案。希望这可以帮助有类似问题的人。

所以,我现在有了这样的配置设置类:

public static class DispatchConfiguration
{
    public static void ConfigureStructureMap(IContainer container, IDispatchConfiguration dispatchConfig)
    {
        DispatchProcessBatchSize = dispatchConfig.DispatchProcessBatchSize;
        ServiceIsActive = dispatchConfig.ServiceIsActive;
        ...
    }

现在,在我使用设置文件从 app.config 文件中检索配置之前。这显然有利于确保我可以灵活地更改配置设置,但它给我带来了无法轻松测试这些设置的问题。假设 9/10 测试要求服务处于活动状态,但 1 测试想要测试“ServiceIsActive = false;”,现在我遇到了麻烦。

不过,现在我可以从测试中注入配置:

[Given(@"Config\.IsServiceActive returns false")]
public void GivenConfig_IsServiceActiveReturnsFalse()
{
    var settings = new DispatchSettings
    {
        ServiceIsActive = false,
        DispatchProcessBatchSize = 100,
        UpdatedBy = "Unit Test"    
    };

    DispatchConfiguration.ConfigureStructureMap(ObjectFactory.Container, settings);
}

然后在现实世界中,我可以从 app.config 获取设置:

public void Start(String[] args)
{
    var dispatchConfig = this.GetDispatchConfiguration();
    DispatchConfiguration.ConfigureStructureMap(ObjectFactory.Container, dispatchConfig);
    ...
}

private IDispatchConfiguration GetDispatchConfiguration()
{
    var config = (DispatchSettings)ConfigurationManager.GetSection("DispatchSettings");
    return config;
}

然后实际的配置类如下所示:

[XmlRoot(ElementName = "DispatchSettings", Namespace = "")]
public sealed class DispatchSettings : IDispatchConfiguration
{
    public Int32 DispatchProcessBatchSize { get; set; }
    public Boolean ServiceIsActive { get; set; }
    ...
}

为了完整性,界面看起来如下像这样:

public interface IDispatchConfiguration
{
    Int32 DispatchProcessBatchSize { get; }
    Boolean ServiceIsActive { get; }
    ...
}

最后,配置文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="DispatchSettings" type="MyNamespace.XmlConfigurator, MyNamespace.Core" />
    </configSections>

    <DispatchSettings type="MyNamespace.DispatchSettings, MyNamespace.Core">
        <ServiceIsActive>True</ServiceIsActive>
        <DispatchProcessBatchSize>100</DispatchProcessBatchSize>
    </DispatchSettings>

现在,任何目光敏锐的人都会发现“MyNamespace.XmlConfigurator”。我在一次 Google 旅程中发现了这一点,该代码允许您将 Xml 配置反序列化为您想要的类(如本示例所示)。因此,为了确保您拥有使该技术发挥作用的完整代码,下面是 XmlConfigurator 的代码。我不记得我在哪里看到它,但非常感谢写它的人!

public sealed class XmlConfigurator : IConfigurationSectionHandler
{
    public XmlConfigurator()
    {
    }

    public object Create(object parent, object configContext, XmlNode section)
    {
        XPathNavigator navigator = null;
        String typeName = null;
        Type sectionType = null;
        XmlSerializer xs = null;
        XmlNodeReader reader = null;

        try
        {
            Object settings = null;

            if (section == null)
            {
                return settings;
            }

            navigator = section.CreateNavigator();
            typeName = (string)navigator.Evaluate("string(@type)");
            sectionType = Type.GetType(typeName);
            xs = new XmlSerializer(sectionType);
            reader = new XmlNodeReader(section);

            settings = xs.Deserialize(reader);

            return settings;
        }
        finally
        {
            xs = null;
        }
    }
}

现在你就拥有了!我希望这能让任何有类似问题的人都能解决它,并且足够清楚,可以遵循!

So, after a lot of searching and trial and error, I was presented with @default.kramer's link, which I duely followed! With a little bit of trial and error, again (best way in my opinion), I managed to get the solution I was looking for. Now, whilst you can follow the link (and I would highly suggest doing so), I am going to post the solution to my question as I implemented it. Hopefully this might help someone with a similar problem.

So, I now have my configuration setup class like so :

public static class DispatchConfiguration
{
    public static void ConfigureStructureMap(IContainer container, IDispatchConfiguration dispatchConfig)
    {
        DispatchProcessBatchSize = dispatchConfig.DispatchProcessBatchSize;
        ServiceIsActive = dispatchConfig.ServiceIsActive;
        ...
    }

Now, before I was using a settings file to retrieve the configuration out of the app.config file. This was obviously good for ensuring I had flexibility in changing my config settings, but it left me with the problem of not being able to easily test those settings. Say 9/10 tests required the service to be active, but 1 test wanted to test "ServiceIsActive = false;", now I'm in trouble.

Now, however, I am able to inject the configuration from the test :

[Given(@"Config\.IsServiceActive returns false")]
public void GivenConfig_IsServiceActiveReturnsFalse()
{
    var settings = new DispatchSettings
    {
        ServiceIsActive = false,
        DispatchProcessBatchSize = 100,
        UpdatedBy = "Unit Test"    
    };

    DispatchConfiguration.ConfigureStructureMap(ObjectFactory.Container, settings);
}

And then in the real world I am able to get the settings from app.config :

public void Start(String[] args)
{
    var dispatchConfig = this.GetDispatchConfiguration();
    DispatchConfiguration.ConfigureStructureMap(ObjectFactory.Container, dispatchConfig);
    ...
}

private IDispatchConfiguration GetDispatchConfiguration()
{
    var config = (DispatchSettings)ConfigurationManager.GetSection("DispatchSettings");
    return config;
}

And then the actual config class looks like :

[XmlRoot(ElementName = "DispatchSettings", Namespace = "")]
public sealed class DispatchSettings : IDispatchConfiguration
{
    public Int32 DispatchProcessBatchSize { get; set; }
    public Boolean ServiceIsActive { get; set; }
    ...
}

For the sake of completeness the interface looks like so :

public interface IDispatchConfiguration
{
    Int32 DispatchProcessBatchSize { get; }
    Boolean ServiceIsActive { get; }
    ...
}

And finally, the config file looks like this :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="DispatchSettings" type="MyNamespace.XmlConfigurator, MyNamespace.Core" />
    </configSections>

    <DispatchSettings type="MyNamespace.DispatchSettings, MyNamespace.Core">
        <ServiceIsActive>True</ServiceIsActive>
        <DispatchProcessBatchSize>100</DispatchProcessBatchSize>
    </DispatchSettings>

Now, anyone with a keen eye will spot "MyNamespace.XmlConfigurator". I found this on one of my Google journeys, and the code allows you to deserialize an Xml config into a class of your desire (as shown in this example). So, to ensure you have the complete code to make this technique work, below is the code for the XmlConfigurator. I cannot remember where I came across it, but a big thanks to the person who wrote it!!

public sealed class XmlConfigurator : IConfigurationSectionHandler
{
    public XmlConfigurator()
    {
    }

    public object Create(object parent, object configContext, XmlNode section)
    {
        XPathNavigator navigator = null;
        String typeName = null;
        Type sectionType = null;
        XmlSerializer xs = null;
        XmlNodeReader reader = null;

        try
        {
            Object settings = null;

            if (section == null)
            {
                return settings;
            }

            navigator = section.CreateNavigator();
            typeName = (string)navigator.Evaluate("string(@type)");
            sectionType = Type.GetType(typeName);
            xs = new XmlSerializer(sectionType);
            reader = new XmlNodeReader(section);

            settings = xs.Deserialize(reader);

            return settings;
        }
        finally
        {
            xs = null;
        }
    }
}

And there you have it! I hope this allows anyone with a similiar issue to resolve it and is clear enough to follow!

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