1

After I've read this article about dependency injection Here I still do not have a clear understanding on how to read the appsetting in other than a controller classes.

Lets say for instance I have a helper class with a bunch of static methods that I'm planning to use, I do not create an instance of this class, how do I read setting values to use inside the methods of this class?

2 Answers 2

2

I used to create helper class to read data from appsettings.config in one of my applications:

public static class ConfigValueProvider
{
    private static readonly IConfigurationRoot Configuration;

    static ConfigValueProvider()
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

        Configuration = builder.Build();
    }

    public static string Get(string name)
    {
        return Configuration[name];
    }
}

However later I reviewed my application to get away from static methods which depends on application config in order to make my application testable.

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

3 Comments

How static methods were making it untestible? Also, how do you read nested values with this approach?
You cannot mock static methods, so you might face issues when trying to test classes which uses static methods internally. For using nesting, you will need to extend static constructor, I have not done it, however you can check source code for asp.net core started app(maybe decompile it)
One possibility is the extension method
2

You should use services.Configure as below:

   public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
          services.Configure<JSonAsClass>(Configuration.GetSection("MySectionName"));
          services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

then you can inject JSonAsClass inside any class you want to use it:

        private JSonAsClass jSonAsClass;
        public MailService(IOptions<JSonAsClass> jSonAsClass)
        {
            this.jSonAsClass = jSonAsClass.Value;
        }

2 Comments

this approach requires instantiation of the class, is there a way to do it with static methods?
I am not sure if you can find something similar to .net framework but it is a modern approach to read setting. By inject it you make your code easy to test and maintain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.