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

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

问题描述

我想知道是否存在一种无需使用依赖项注入即可访问Configuration(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.

如果您使用一个带有一个IConfiguration的公共构造函数使用一个标准AppSettings类,该IConfiguration可用于填充所有AppSettings

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文件中的设置,可以将其用作默认实例化,这样,如果调用了当前并将其设置为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天全站免登陆