Access App key data from class libraries in .NET Core / ASP.NET Core

To access App Keys in a class library, do we need to do the following code in every class library and class where we need to access a AppKey?

public static IConfigurationRoot Configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();

This is what I found in Microsoft docs, but this looks very redundant.

Startup class in a project as below

 public class Startup
    {
        public IConfigurationRoot Configuration { get; set; }

        public Startup()
        {
            var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json");

            Configuration = builder.Build();
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddEntityFramework().AddEntityFrameworkSqlServer()
                .AddDbContext<DbContext>(options =>
                    options.UseSqlServer(Configuration["Data:MyDb:ConnectionString"]));
        }
    }

Then how should I inject this “IConfigurationRoot” in each class of a project. And do I have to repeat this Startup class in each class Library? Why is this not part of .NET Core Framework?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The recommended way is to use the options pattern, provided by Microsoft and used heavily in ASP.NET Core.

Basically you create a strong typed class and configure it in the Startup.cs class.

public class MySettings 
{
    public string Value1 { get; set; }
    public string Value2 { get; set; }
}

and initialize it in the Startup class.
// load it directly from the appsettings.json "mysettings" section
services.Configure<MySettings>(Configuration.GetSection("mysettings"));

// do it manually
services.Configure<MySettings>(new MySettings 
{
    Value1 = "Some Value",
    Value2 = Configuration["somevalue:from:appsettings"]
});

then inject these options everywhere you need it.
public class MyService : IMyService
{
    private readonly MySettings settings;

    public MyService(IOptions<MySettings> mysettings) 
    {
        this.settings = mySettings.Value;
    }
}

Method 2

By the principle of Information Hiding in Object-Oriented Programming, most classes should not need to have access to your application configuration. Only your main application class should need to directly have access to this information. Your class libraries should expose properties and methods to alter their behavior based on whatever criteria their callers deem necessary, and your application should use its configuration to set the right properties.

For example, a DateBox shouldn’t need to know how timezone information is stored in your application configuration file – all it needs to know is that it has a DateBox.TimeZone property that it can check at runtime to see what timezone it is in.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x