You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have the scenario where I want to decide at runtime which service to inject.
publicinterfaceIExampleService;publicclassExampleServiceA:IExampleService;
public class ExampleServiceB :IExampleService;
I see different solutions for this right now:
Use injected IServiceProvider
[ServiceProvider][Singleton<IExampleService>(Factory=nameof(ChooseExampleService))][Singleton<ExampleServiceA>]// No service for type 'releaser.Lib.ExampleServiceA' has been registered.[Singleton<ExampleServiceB>]// No service for type 'releaser.Lib.ExampleServiceB' has been registered.publicpartialclassExampleProvider(booluseA){publicIExampleServiceChooseExampleService(IServiceProviderprovider)=>useA?provider.GetRequiredService<ExampleServiceA>():provider.GetRequiredService<ExampleServiceB>();}
⚠️ This works as expected, unless I do not register the example services using the attributes, which will result in runtime errors.
✅ This allows Jab to gather the necessary information on service dependencies beforehand (i.e. in this case IExampleService depends on ExampleServiceA and ExampleServiceB during runtime), which is lost when using the plain IServiceProvider interface.
This could be a minimally invasive approach to solve this problem.
Let me know what you think of this idea. If you think this is out of scope, feel free to close this issue, it shall just be my suggestion. I just thought it might be fitting with the whole "build-time checking" and source generation aspects of this project.
Footnotes
Note that the factory currently has to be internal at most, because of the accessibility level of the IServiceProvider<T>. ↩
The text was updated successfully, but these errors were encountered:
This does make sense, I think injecting the Func is the way most of DI containers solve this.
Ah yes, you are right, somehow I forgot about the existence of Func<T>. That is definitely the better way to solve this regarding declared intent of the factory.
Problem
I have the scenario where I want to decide at runtime which service to inject.
I see different solutions for this right now:
Use injected
IServiceProvider
Use pre-generated instances
Proposed solution
Using your
IServiceProvider<T>
interface or other wrappers to lazily create instances of the service. 1Edit: maybe using something like
Lazy<T>
is better suited for this purpose.✅ This allows Jab to gather the necessary information on service dependencies beforehand (i.e. in this case
IExampleService
depends onExampleServiceA
andExampleServiceB
during runtime), which is lost when using the plainIServiceProvider
interface.This could be a minimally invasive approach to solve this problem.
Let me know what you think of this idea. If you think this is out of scope, feel free to close this issue, it shall just be my suggestion. I just thought it might be fitting with the whole "build-time checking" and source generation aspects of this project.
Footnotes
Note that the factory currently has to be internal at most, because of the accessibility level of the
IServiceProvider<T>
. ↩The text was updated successfully, but these errors were encountered: