ipjohnson / Grace.DependencyInjection.Extensions

Grace Extensions for ASP.Net Core
19 stars 7 forks source link

Register named options as keyed #18

Closed cnshenj closed 4 years ago

cnshenj commented 4 years ago

Had to use conditional compilation since named options are not in Microsoft.Extensions.Options.ConfigurationExtensions that targets .NET Standard 1.x.

Maybe it's time to remove .NET Standard 1.x support?

ipjohnson commented 4 years ago

That makes sense, I’ll pull this in this weekend and look at dropping netstandard 1.0

cnshenj commented 4 years ago

Could you also publish a new version of Grace.AspNetCore.Hosting once the PR is accepted and completed. Thanks!

ipjohnson commented 4 years ago

@cnshenj I pushed a new version to nuget

ipjohnson commented 4 years ago

@cnshenj I've run into an issue with this PR so I've had to roll it back. That said I would like to solve the issue you're having.

what's the use case you want to solve that you feel keyed exports will help? Are you doing Locate<IOptions<T>>("key") or specifying it in an export configuration of some sort?

cnshenj commented 4 years ago

Here is my use case:

// These classes are in a library
public class ConnectionOptions {}
public class CatDb { public CatDb(ConnectionOptions connectionString) {} }
public class DogDb { public DogDb(ConnectionOptions connectionString) {} }

// ASP.NET Core configurations from a provider, e.g. a config file
services.Configure<ConnectionOptions>("Cat", this.Configuration.GetSection("Cat"));
services.Configure<ConnectionOptions>("Dog", this.Configuration.GetSection("Dog"));

// Trying to use keyed export but fails
block.ExportFactory<IExportLocatorScope, CatDb>(s => new CatDb(s.Locate<IOptionsSnapshot<ConnectionOptions>>(withKey: "Cat").Value));
block.ExportFactory<IExportLocatorScope, DogDb>(s => new DogDb(s.Locate<IOptionsSnapshot<ConnectionOptions>>(withKey: "Cat").Value));

If I have full control of source code, I can make CatDb use a specialized options class CatDbConnectionOptions. But ConnectionOptions, CatDb, DogDb are in a library that I can't change.

Maybe we can fix the issue you run into? Do you have more details?

ipjohnson commented 4 years ago

@cnshenj sorry I missed this. I'll try and take a look over the next couple days on how to address this and come up with a solution before I push the next version out (that rolls this back).

The answer maybe to register it both ways.