ASP.NET 구성을 인터페이스에 바인딩
IOptions<T>
method.
The IOptions<T>
allows you register the instantiated configuration class.
A brief recap would be to load upload the config details say for example we are getting these details from the appsettings.json
For .NET 6 the appsettings.json
is loaded up automatically.
For this example the appsettings.json
would look like this
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"CustomConfigValue": {
"CustomProperty": "value here"
}
}
In order to bind the CustomConfigValue
node to class/record instance we would need to create a class/record
public class CustomConfigValue
{
public string CustomProperty { get; set; }
}
then to bind JSON node CustomConfigValue
we would do this in the Program.cs
builder.Services.Configure<CustomConfigValue>(builder.Configuration.GetSection(nameof(CustomConfigValue)));
Then in the class/controller constructor we set the value from IOptions<T>
to the a class property of the same type
private readonly CustomConfigValue _customConfig;
public CustomConfigClass(IOptions<CustomConfigValue> customConfigValue)
{
_customConfig = customConfigValue.Value;
}
Another approach In some case the use of IOptions<T>
isn't beneficial and there are a lot of posts about how to bind directly to object instances instead. From our previous example what we would be doing different is that we would be doing this in the Program.cs
CustomConfigValue customConfig = new ();
builder.Configuration.Bind(nameof(CustomConfigValue), customConfig);
builder.Services.AddSingleton(typeof(CustomConfigValue), customConfig);
In the class/controller constructor we would be able to access the CustomConfigValue
like this
private readonly CustomConfigValue _customConfig;
public CustomConfigClass(CustomConfigValue customConfigValue)
{
_customConfig = customConfigValue;
}
This can be abstracted away from the Program.cs
by creating a service extension then doing the work like this
public static class ConfigurationExtensions
{
public static void AddCustomConfigValue<T>(this IServiceCollection services, IConfigurationSection configSection) where T : class
{
T setting = configSection.Get<T>();
services.TryAddSingleton<T>(setting);
}
}
In the Program.cs
we do this to bind the configuration to the class object
builder.Services.AddCustomConfigValue<CustomConfigValue>(builder.Configuration.GetSection(nameof(CustomConfigValue)));
This is all well and good, and covers most use cases, but one thing I find that this method has is yes, you guess it, doesn't bind to an interface. I really love the idea of abstractions, I think it's a concept everyone should apply in every part of the life, but I digress.
In order to bind to an interface let's create another method in the extension class
public static void AddCustomConfigValue<InterfaceT, T>(this IServiceCollection services, IConfigurationSection configSection)
where T : InterfaceT
where InterfaceT : class
{
InterfaceT setting = configSection.Get<T>();
services.TryAddSingleton(setting);
}
We would add an interface
public interface ICustomConfig
{
string CustomProperty { get;}
}
Then have the CustomConfigValue implement the interface
public class CustomConfigValue : ICustomConfig
In the Program.cs
we would do this to bind the configuration to the object
builder.Services.AddCustomConfigValue<ICustomConfig, CustomConfigValue>(builder.Configuration.GetSection(nameof(CustomConfigValue)));
And in our constructor we would be able to access the configuration with
private readonly ICustomConfig _customConfigViaInterface;
public CustomConfigClass(ICustomConfig customConfigViaInterface)
{
_customConfigViaInterface= customConfigViaInterface;
}
You could do this to maintain some level of abstraction and aid testing via the binding of the object to an interface and having only get
access via the interface.
Another thing to consider is to use the IOptions<T>
but bind to a record. The idea behind this is to create an immutable configuration object, once instantiated properties can't be reassigned and enforced at compile time.
Reference
이 문제에 관하여(ASP.NET 구성을 인터페이스에 바인딩), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/ifetayo/binding-aspnet-configuration-to-an-interface-5g1g텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)