-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce IServiceActivator<T> #79458
Comments
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection Issue DetailsIs there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.With ASP.NET's dependency injection, I have to specify my dependencies in the constructor. This is not possible when I'm injecting a dependency some of whose constructor arguments I only know during a class's method call. For example, I might have a
Because some of the arguments to the constructor are only known during the method call, it's not possible to specify them in the parent class's constructor and therefore not possible to pull in Describe the solution you'd like
Why not introduce an interface
It would also allow unit tests to create an
|
This isn't an API proposal, so I don't know the shape of |
Yes, I suppose it would be The other thing is that using the |
Maybe a better name would be |
have you tried ActivatorUtilities ? |
My original post talks about ActivatorUtilities. |
a sorry missed that. |
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
With ASP.NET's dependency injection, I have to specify my dependencies in the constructor. This is not possible when I'm injecting a dependency some of whose constructor arguments I only know during a class's method call. For example, I might have a
GameTurnManager
class that takesgameState
to determine the state of the game's play field to use when performing various operations on the game's state. However, my API call might specify the game ID to use as part of the API call:Because some of the arguments to the constructor are only known during the method call, it's not possible to specify them in the parent class's constructor and therefore not possible to pull in
GameTurnManager
through constructor DI.Describe the solution you'd like
ActivatorUtilities.CreateInstance<GameTurnManager>(IServiceProvider, gameState, playerId)
can be used during the method to get theGameTurnManager
from DI whilst providing certain arguments at the time of instance creation. However, this does hide theGameTurnManager
dependency that the parent class has. It would be nice to expose that dependency without having to instantiate the dependency until it's needed and the extra arguments are known.Why not introduce an interface
IServiceActivator<T>
which can be used for precisely that? When a constructor tookIServiceActivator<T>
, theIServiceProvider
would provide an instance of a class that can be used to activateT
and provide extra paramaters not provided by theIServiceProvider
. This would give the benefit that the dependency onGameTurnManager
would be exposed:It would also allow unit tests to create an
IServiceActivator<IGameTurnManager>
which could provide a mockGameTurnManager
if needed.The text was updated successfully, but these errors were encountered: