OptionsBuilder to chain configuration calls
Hi, I'm not sure if there is already built-in support for this scenario, so maybe the suggestion is irrelevant, but we'd like to have services added to the service collection, as well as configure them in the process. Today this would look like:
services.AddTransient<IFooService, FooServices>();
services.Configure<FooServiceOptions>(myConfiguration);
services.Configure<FooServiceOptions>(options => ...);
services.Configure<FooServiceOptions>(options => ...);
// then add logic so that an IHostedService initializes an instead of FooService
For our users, we could create an extension method to add the FooService as well as specify different overloads to be able to configure in all the different ways that the Options infrastructure support, but that means A TON of extension method overloads, and even adding a reference to Microsoft.Extensions.Options.ConfigurationExtensions just in case the user happens to use that. Also if some external library adds more overloads to .Configure, we can't support those.
Alternatively, one can create an extension method that just adds FooService to the collection, and then the user has to explicitly call Configure<FooServiceOptions> multiple times. The main problem here is discoverability: how does the user know that he needed to configure FooServiceOptions when using FooService.
This approach also explodes when we talk about named options.
This challenge could be overcome if the Options package provides an IOptionsBuilder<TOptions> interface that allows calls to Configure to be chained. Only if the user references Microsoft.Extensions.Options.ConfigurationExtensions then the `.Configure(IConfiguration) overload would light up, and so on.
This way our users should be able to write:
services.AddFooService("myName")
.Configure(myConfiguration)
.Configure(options => ...);
Implementation should be very straightforward: OptionsBuilder<TOptions> would just have to hold the name that we are configuring, as well as a reference to IServiceCollection, so that it can forward the calls to it.
Does it make sense? I'm thinking of adding infrastructure like this to configure the many services in Orleans, but ideally this would be part of the main Options package. I can send a PR if you think it's a good idea.
This is a more flexible pattern which would be very good to have. And it would benefit all applications/software which tries to integrate with Options
Any comments about this? Is this a valid approach or is there another way to achieve this that I missed? Even if we don't want to do this in this repo for now, do you think it would be OK if I implement it in the Orleans infrastructure?
Sorry, just looking at this now, so you are basically just asking for some sugar to make it easy to expose options configuration in a consistent manner? We currently do this in a fairly ad-hoc manner in each repo where each repo/feature area decides what is appropriate, with most just having a single
services.AddXyz(Action<XyzOptions> o)
where they return IServiceCollection for chaining purposes. As you pointed out you can already continue to Configure<XyzOptions>. Is the main motivation to tidy up the subsequent Configures?
my main motivation is to allow several Configure calls to be chained, even if some of them involve IConfiguration, and not just Action<XyzOptions> (or in the future some other overload that is not part of the main Options package).
It would be a little weird to do something like this:
services.AddNamedXyz("name", Configuration.GetSection("Something"))
.Configure<XyzOptions>("name", options => { ... }));
It is not entirely straightforward to chain Configure calls, especially when we start dealing with named options, but even when not, it might not be entirely evident what is the TOptions to use to further configure the options.
BTW, having an OptionsBuilder, as suggested, doesn't dictate whether the options builder is returned, or is passed into the delegate (and hence you can return the original IServiceCollection to continue chaining).
@davidfowl any opinions on this? But regardless, changing all of our AddXyz() to return something different would be a breaking change so this is something that would have to wait to 3.0
But in the mean time there's nothing preventing you from having your frameworks AddXyz() return an options builder that lets you chain as you desire.
Feel free to submit a PR to options itself for what you'd like built in and we can take a look
I do like the benefit of scoping the resulting builder to a specific named options instance, so you don't have to pass the name of the options instance being configured to every Configure call
Created a PR for it: https://github.com/aspnet/Options/pull/238