feat(VariantBasedInjection): LazyVariantServiceProvider with Keyed DI#605
feat(VariantBasedInjection): LazyVariantServiceProvider with Keyed DI#605Stepami wants to merge 9 commits into
Conversation
VariantServiceProvider uses sp to get keyed services BREAKING CHANGE: behavioural: keyed services registration needed unless we override descriptors for backward comp
|
@microsoft-github-policy-service agree |
rename keyedServiceProvider to serviceProvider for clarity
|
I just thought that to avoid BC i could introduce |
keyed di impl in a separate class Closes microsoft#564
keyed di impl separate class
Extension method impl && tests Closes microsoft#564
Added notes about impls handling to highlight difference with lazy one
|
@zhiyuanliang-ms could you please take a look? Managed to avoid BC through making Keyed DI implementation as the new feature |
use DI package as transitive
|
Hey, @Stepami Thank you for your contribution! The implementation looks good to me overall. Instead of introducing a new API like builder.Services.AddFeatureManagement()
.WithVariantService<ICalculator>("Calculator", useKeyedService: true);My only concern is around compatibility. Keyed services are a .NET 8 DI feature, as I mentioned in this comment, while we still have customers running on .NET Framework. We have been hitted by a similar compatibility issue before #435 From a compilation perspective, using the keyed DI APIs should be fine since the 8.x DependencyInjection abstractions package supports netstandard. My concern is runtime compatibility. Although Because of that, this is not necessarily a .NET Framework issue by itself. It can work on .NET Framework if the application uses a compatible Microsoft.Extensions.DependencyInjection 8.x service provider. The risk is for consumers using a custom provider. I think the existing non-keyed behavior should remain the default, and the keyed DI path should be explicitly opt-in with clear validation/error messaging when the current service provider does not support
|
|
Hey, @zhiyuanliang-ms Thanks for the feedback! I like your proposal. To make things clear i have a few questions:
|
|
Hey, @Stepami
I think we should add a new overload. Because replacing the existing one with
To be honest, I don't have a perfect answer now. My current thinking is that best effort is probably enough. If users explicitly enable the keyed-service path, the happy path is straightforward: they are expected to register their services with AddKeyedXXX and use a service provider that supports keyed services. In that case, everything should work as expected. The unhappy path is where the user enables useKeyedService: true, but the actual runtime service provider does not support |
- introduced new WithVariantService overload which replaced WithLazyVariantService ext method - added type check for sp to be keyed in case of lazy variant sp Closes microsoft#564
|
I decided to express the intentions around compatibility through typing in Now ctor accepts |
|
Hey, @Stepami Could you take a look at #606 Yesterday, I discussed your PR with @jimmyca15 and we realized that we can have the current |

new class
LazyVariantServiceProvideruses sp to get keyed servicesWhy this PR?
Keyed DI can be used in FeatureManagement library as package-provided solution despite netstandard targeting
Visible Changes
LazyVariantServiceProviderclassWithLazyVariantServicedi extension method