0

I am inviting criticism and feedback. A roast if you will. What I have done here feels wrong and I want to know why.

To create a static settings class in .net core that can returns settings from the appsettings.json file. It does work but it uses ConfigurationBuilder on every accessing of settings.

public static class GeneralSettings
{
    private static IConfigurationRoot Configuration = StartConfig();

    private static IConfigurationRoot StartConfig()
    {
        var configBuilder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables();

        return configBuilder.Build();
    }

    public static string ContextUserID
    {
        get
        {
            string result = 
                Configuration.GetSection("AppSettings").GetSection("ContextUserID").Value;

            return result;
        }
    }
}
6
  • I would ask yourself why this class needs to be static. Commented Jul 16, 2019 at 19:46
  • Ease of access in other static classes Commented Jul 16, 2019 at 20:03
  • Which version of ASP.NET Core are you using? Commented Jul 16, 2019 at 22:28
  • 2.2. I know how to inject it the normal way. Just in case that's what you are getting at. I just thought I'd post this here as a curiosity. It can be used in static class library classes in a pinch. Commented Jul 17, 2019 at 1:59
  • I was curious as to why you're even building the configuration yourself, instead of using the prebuilt version, but I guess that's something to do with statics here too. It's so easy to bind configuration to a POCO in ASP.NET Core and pass it around. Microsoft just doesn't like statics anymore... Commented Jul 17, 2019 at 7:54

1 Answer 1

1

Given the above code, you're rebuilding the configuration every time its called. You may as well make it a singleton. And since singletons are bad, well something else is probably wrong. Your instinct this feels wrong is RIGHT!

  1. Avoid static classes until you know they are static.
  2. Usually, common 'helper' classes like this violate one or more SOLID principles. (This is probably why you have that feel of the code being 'wrong')

Read more on how 'helper' classes like this do not adhere to SOLID principles, and what those principles are in this blog article

If instead of a static class, were you to leverage .NET Core's built in dependency injection, we could easily turn this abstraction into a bit of code that adheres to SOLID principles. That of course doesn't solve your problem of being able to use your new IContextSettings in another static class, but does enable you to use this interface as a first class citizen directly where you need the ContextUserID, and you would simply use dependency injection in your Controller, or PageModel

public interface IContextSettings
{
    string ContextUserID { get; }
}

public class ContextSettings : IContextSettings
{
    private IConfiguration configuration;

    public ContextSettings(IConfiguration configuration)
    {
        this.configuration = configuration;
    }
    public string ContextUserID => configuration.GetSection("AppSettings").GetSection("ContextUserID").Value;
}

Usage (RazorPages Example)

public class IndexModel : PageModel
{
    private readonly IContextSettings settings;

    public IndexModel(IContextSettings settings)
    {
        this.settings = settings;
    }
    public IActionResult OnGet()
    {
        var userId = settings.ContextUserID;
        return Page();
    }
}

Feels right... right?

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Adam. I have leveraged the dependency injection in Core to make a singleton available. You are right, it does rebuild it every time it's used. It's positively outrageous. One benefit it has is that it can be used in static class library classes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.