I have the following extension method to add themes support to my application:
public static void AddThemes(this IServiceCollection services, Action<ThemesOptions> setup) {
services.Configure(setup);
}
Where ThemesOptions is defined as:
public class ThemesOptions {
public IEnumerable<string> Themes { get; set; }
}
Now in my application's startup ConfigureServices method I can say:
services.AddThemes(options => {
options.Themes = Configuration.GetSection("Themes").Get<ThemesOptions>().Themes;
});
I'm not sure that I like that I have to set every property for the options. Alternatively I tried:
services.AddThemes(options => {
options = Configuration.GetSection("Themes").Get<ThemesOptions>();
});
And:
services.AddThemes(options => Configuration.GetSection("Themes"));
However when I inject IOptions<ThemesOptions> the Themes property is null.
An alternative to I changed my extension method to:
public static void AddThemes(this IServiceCollection services, IConfiguration configuration) {
services.Configure<ThemesOptions>(configuration.GetSection("Themes"));
}
Now I can say the following within my application's Startup ConfigureServices method:
services.AddThemes(Configuration);
This worked fine, however, I feel the problem with this approach is that the extension method only allows the options to be set from the configuration.
I'd appreciate it if someone could confirm whether my first solution is correct and if it can be improved upon.