Skip to content
This repository has been archived by the owner on Nov 7, 2018. It is now read-only.

Extension methods for more concise registration of IConfigureOptions/IPostConfigureOptions #222

Closed
tuespetre opened this issue Aug 14, 2017 · 1 comment
Assignees
Milestone

Comments

@tuespetre
Copy link

services.AddSingleton<IConfigureOptions<MvcOptions>, MyConfigureMvcOptions>() seems unwieldy and non-obvious. I believe a friendlier, more discoverable alternative would be services.Configure<MvcOptions, MyConfigureMvcOptions>().

public static IServiceCollection Configure<TOptions, TConfigureOptions>(this IServiceCollection services)
    where TOptions : class
    where TConfigureOptions : class, IConfigureOptions<TOptions>
{
    return services.AddSingleton<IConfigureOptions<TOptions>, TConfigureOptions>();
}

public static IServiceCollection Configure<TOptions, TPostConfigureOptions>(this IServiceCollection services)
    where TOptions : class
    where TPostConfigureOptions: class, IPostConfigureOptions<TOptions>
{
    return services.AddSingleton<IPostConfigureOptions<TOptions>, TPostConfigureOptions>();
}
@HaoK
Copy link
Member

HaoK commented Aug 14, 2017

We used to have sugar for this early in 1.0 which we removed near 1.0 RTM, we actually allowed registration of a global setup class, and would reflect on it registering all IConfigureOptions it implemented.

i.e.

  services.RegisterOptionsSetup<MyConfigure>(); // Reflects and registers all of the appropriate

  class MyConfigure : IConfigureOptions<MvcOptions>, IConfigureOptions<RazorOptions>, IPostConfigureOptions<MvcOptions> {
     // implement everything you want in one class
  }

We could bring something similar back

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants