无需依赖注入即可访问配置 [英] Access to configuration without dependency injection

查看:32
本文介绍了无需依赖注入即可访问配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以在不使用依赖注入的情况下访问配置 (Microsoft.Extensions.Configuration).我看到的唯一示例是通过构造函数注入(使用 IOptions 或直接注入 Configuration).

I was wondering if there was a way to access Configuration (Microsoft.Extensions.Configuration) without the use of dependency injection. Only examples I see are through constructor injection (using IOptions or injecting Configuration directly).

我的困境是我有一个实用程序类——而不是一个服务——它有静态方法来动态地做事.在其中一些静态方法中,我想从 appsettings.json 动态检索几个属性.由于这严格来说是一个实用程序类,我不想将这个类注入到需要使用该实用程序中的一两个方法的每个其他类中.

My dilemma is that I have a utility class-- not a service-- that has static methods to do things on the fly. In a few of those static methods I would like to retrieve a couple of properties from appsettings.json dynamically. Since this is strictly a utility class, I don't want have to inject this class into every other class that needs to use a method or two from the utility.

关于如何在没有某种依赖注入的情况下访问 appsettings.json 的属性的任何想法.

Any ideas on how to access the properties of appsettings.json without some sort of dependency injection.

仅供参考:使用 c# 和 .net core 1.1

FYI: using c# and .net core 1.1

推荐答案

哇,这么多评论,为什么人们不回答问题而不是告诉别人他们不想做他们显然会做的事情.无论如何,希望这能让两个阵营都满意.

Wow, what a lot of comments, why don't people answer the question instead of telling someone they don't want to do what they obviously do. Anyway, hopefully this will keep both camps satisfied.

如果您采用带有单个公共构造函数的标准 AppSettings 类,该公共构造函数采用可用于填充所有 AppSettings 属性的 IConfiguration,这将保留依赖注入的能力.

If you take a standard AppSettings class with a single public constructor that takes an IConfiguration that can be used to populate all the AppSettings properties, this keeps the ability for Dependency Injection.

如果在构造函数的末尾我们设置了一个指向当前 AppSettings 实例的静态属性Current",这将允许我们从该点开始通过静态属性访问设置,而无需进一步注入.

If at the end of the constructor we set a static property 'Current' pointing to the current instance of AppSettings, this will allow us access to the settings from that point onwards via the static property without the need for further injection.

如果我们现在创建一个静态的 Default 'GetCurrentSettings' 方法来从 json 文件中获取设置,这可以用作默认实例化,这样如果调用 'Current' 并将其设置为 null,我们就离开并从文件中填充设置.这是我的意思的一个例子......

If we now create a static Default 'GetCurrentSettings' method to get the settings from a json file, this can be used as a default instantiation, so that if 'Current' is called and is set to null, we just go off and populate the settings from the file. Here's an example of what I mean...

public class AppSettings
{
    private static AppSettings _appSettings;

    public string AppConnection { get; set; }

    public AppSettings(IConfiguration config)
    {
        this.AppConnection = config.GetValue<string>("AppConnection");

        // Now set Current
        _appSettings = this;
    }

    public static AppSettings Current
    {
        get
        {
            if(_appSettings == null)
            {
                _appSettings = GetCurrentSettings();
            }

            return _appSettings;
        }
    }

    public static AppSettings GetCurrentSettings()
    {
        var builder = new ConfigurationBuilder()
                        .SetBasePath(Directory.GetCurrentDirectory())
                        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                        .AddEnvironmentVariables();

        IConfigurationRoot configuration = builder.Build();

        var settings = new AppSettings(configuration.GetSection("AppSettings"));

        return settings;
    }
}

这样你就可以在代码 AppSettings.Current.AppConnection 中的任何地方调用

So from this you would be able to call anywhere in code AppSettings.Current.AppConnection

如果它是使用 DI 实例化的,则将检索注入的版本,否则将从 appsettings.json 文件中获取默认版本.我怀疑它是否能让每个人都满意,我不确定我是否已经很好地解释了它,但希望它是有道理的.

If it's been instantiated using DI the injected version would be retrieved otherwise the default version would be taken from an appsettings.json file. I doubt it satisfies everyone and I'm not sure I've explained it very well, but hopefully it makes sense.

这篇关于无需依赖注入即可访问配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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