Hi (again)
I am trying to get used for generics. Playing with ASPNet Core 3.0
In my scenario I have configured a startup filter for configuration validation. Specifically I want to validate IdentitySettings that are holding in appsettings. Well.
I expose the working code and I would like to know a solution to make it generic and reusable.
In configure method I am doing
builder.ConfigureServices((services) =>
{
services.Configure<IdentityStartupSettings>(Configuration, binder => new IdentityStartupSettings());
services.AddSingleton(resolver =>
{
var identityStartupConfig = resolver.GetRequiredService<IOptions<IdentityStartupSettings>>().Value;
return identityStartupConfig;
});
services.AddConfigValidation<IdentityStartupSettings>();
IdentityStartupSettings is my POCO class that has settings definition. I am using DataAnnotations.
AddConfigValidation is an extension method I did to perform validation service injection
public static IServiceCollection AddConfigValidation<T>(this IServiceCollection services)
where T : IValidateConfig
{
services.AddTransient<IStartupFilter, ValidateConfigFilter>();
services.AddSingleton<IValidateConfig>(provider => provider.GetRequiredService<T>());
return services;
}
Now the questions:
First is simple. When I am doing services.Configure<IdentityStartupSettings>(Configuration, binder => new IdentityStartupSettings()); I am not sure if binder => new IdentityStartupSettings() means that it with bind to an instance of IdentityStartupSettings.
I would like to know a scenario where it parameter is more interesting that here. I am curious.
Second.
I would like to transform the code for being fully generic. Where I can overload AddConfigValidation<T>(this IServiceCollection services) to accept a second parameter, the instance of IConfiguration (or IConfigurationSection in my case) then being: AddConfigValidation<T>(this
IServiceCollection services, IConfiguration config)
And perform Configuration in the same method:
The part where I am doing:
services.Configure<IdentityStartupSettings>(Configuration, binder => new IdentityStartupSettings());
services.AddSingleton(resolver =>
{
var identityStartupConfig = resolver.GetRequiredService<IOptions<IdentityStartupSettings>>().Value;
return identityStartupConfig;
});
being inside, something like this (what is not working):
public static IServiceCollection AddConfigValidation<T>(this IServiceCollection services, IConfiguration config)
where T : IValidateConfig
{
services.Configure<T>(config);
services.AddSingleton(resolver =>
{
var configValue = resolver.GetRequiredService<IOptions<T>>().Value;
return configValue;
});
services.AddTransient<IStartupFilter, ValidateConfigFilter>();
services.AddSingleton<IValidateConfig>(provider => provider.GetRequiredService<T>());
return services;
}
First error, the line services.Configure<T>(config); is asking for an Action instead of an IConfiguration instance. What is not happening when I do it outside.
Maybe you can give me some guidance.
So pleased, thanks in advance.