c# - Access App key data from class libraries in .NET Core / ASP.NET Core -
to access app keys in class library, need following code in every class library , class need access appkey?
public static iconfigurationroot configuration = new configurationbuilder().addjsonfile("appsettings.json").build();
this found in microsoft docs, looks redundant.
startup class in project 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 inject "iconfigurationroot" in each class of project. , have repeat startup class in each class library? why not part of .net core framework?
by principle of information hiding in object-oriented programming, classes should not need have access application configuration. main application class should need directly have access information. class libraries should expose properties , methods alter behavior based on whatever criteria callers deem necessary, , application should use configuration set right properties.
for example, datebox shouldn't need know how timezone information stored in application configuration file - needs know has datebox.timezone property can check @ runtime see timezone in.
Comments
Post a Comment