使用依赖注入将配置代码保留在逻辑代码之外的方法 [英] Ways of keeping configuration code out of logic code using Dependency Injection

查看:236
本文介绍了使用依赖注入将配置代码保留在逻辑代码之外的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用设置(ApplicationSettingsBase)和依赖注入将所有配置文件代码保存在我的逻辑代码之外?

How can keep all the configuration file code out of my logic code using Settings (ApplicationSettingsBase) and Dependency Injection?

配置我的意思是客户特定的配置文件。

With configuration I mean a customer specific configuration file.

我每次需要注入配置类还是有其他模式?

Do I really have to inject a configuration class everytime I need it or is there another pattern?

将获得一些示例代码非常棒!

It would be great to get some sample code!

示例:

静态配置:

public static class StaticConfiguration
{
    public static bool ShouldApplySpecialLogic { get; set; }
    public static string SupportedFileMask { get; set; }
}

public class ConsumerOfStaticConfiguration
{
    public void Process()
    {
        if (StaticConfiguration.ShouldApplySpecialLogic)
        {
            var strings = StaticConfiguration.SupportedFileMask.Split(',');
            foreach (var @string in strings)
            {

            }
        }
    }
}

非静态配置

public interface IConfiguration
{
    bool ShouldApplySpecialLogic { get; set; }
    string SupportedFileMask { get; set; }
}

public class Configuration : IConfiguration
{
    public bool ShouldApplySpecialLogic { get; set; }
    public string SupportedFileMask { get; set; }
}

public class Consumer
{
    private readonly IConfiguration _configuration;

    public Consumer(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public void Process()
    {
        if (_configuration.ShouldApplySpecialLogic)
        {
            var strings = _configuration.SupportedFileMask.Split(',');
            foreach (var @string in strings)
            {

            }
        }
    }
}

非静态配置的静态上下文

public static class Context
{
    public static IConfiguration Configuration { get; set; }
}

public class ConsumerOfStaticContext
{
    public void Process()
    {
        if (Context.Configuration.ShouldApplySpecialLogic)
        {
            var strings = Context.Configuration.SupportedFileMask.Split(',');
            foreach (var @string in strings)
            {

            }
        }
    }
}


推荐答案

要实现的重要部分是配置只是驱动应用程序行为。

The important part to realize is that configuration is only one among several sources of values that drive your application's behavior.

第二个选项(非静态配置)是最好的,因为它使您能够完全将消费者与配置值的源解耦。但是,不需要该界面,因为配置设置通常最好建模为值对象

The second option (non-static configuration) is best because it enables you to completely decouple the consumer from the source of the configuration values. However, the interface isn't required, as configuration settings are normally best modeled as Value Objects.

如果您仍然想读取值从配置文件中,您可以从应用程序的组合根目录中执行此操作。使用StructureMap,它可能看起来像这样:

If you still want to read the values from a configuration file, you can do that from the application's Composition Root. With StructureMap, it might looks something like this:

var config = (MyConfigurationSection)ConfigurationManager.GetSection("myConfig");

container.Configure(r => r
    .For<Consumer>()
    .Ctor<MyConfigurationSection>()
    .Is(config));

这篇关于使用依赖注入将配置代码保留在逻辑代码之外的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆