I'm trying to setup generic classes within IOptions settings. Example:
public class MyClass<T> : IMyClass
{
public MyClass(IOption<MyOptions<T>> options)
{
}
}
To be able to dynamically use settings from appsettings.json. While the set of options is the same, they should be under different schemas, so that each type defines its set of options i.e.:
"MyOptionsForClass1":
{
"Option1" : 1,
"Option2" : 1
},
"MyOptionsForClass2":
{
"Option1" : 2,
"Option2" : 2
},
And the options classes should look like this:
public class MyOptions<T>
{
public const string ConfigSchemaName = "DefaultOptions"
public int Option1 {get; set;}
public int Option2 {get; set;}
}
public class OptionsForClass1 : MyOptions<MyClass1>
{
public new const string ConfigSchemaName = "MyOptionsForClass1"
public int Option1 {get; set;}
public int Option2 {get; set;}
}
And then in ConfigureServices to add something like:
.AddScoped<MyOptions<MyClass1>, OptionsForClass1>();
...
.AddOptions<MyOptions<MyClass1>>()
.Bind(configuration.GetSection(OptionsForClass1.ConfigSchemaName);
Is this how this should be done? Are there any other more clean approaches for this?
Thank you!
