We have an ASP.NET MVC application using IoC to inject service references into controllers and repository links into Services.
Controllers must have a Lifetime Transient, as they must be created for each request. If the entire IoC stack is updated for each request, this becomes a bit overhead. We have more dependencies than we would like, and one option would be to have more controllers, each of which has fewer dependencies on the stack. But, leaving that aside for the moment, my question is that if an object that is injected as a singleton has dependencies having a temporary lifetime, will these dependencies be essentially considered as singleton due to the fact that they belong to Singleton ?
In particular, if we have the following
RepositoryA (should be temporary because the current project enters the user context in the constructor) ServiceA (singleton) Controller (transient)
is created in this way:
public ServiceA(IRepositoryA repo) {} public ControllerA(IServiceA service) {}
Will RepositoryA essentially be instantiated once because ServiceA is instantiated?
I'm 99% sure that yes, but I just wanted to confirm the amount of refactoring I have to do here.
In addition, if the services and repositories did not have any instance variables depending on the user and the request, as a design approach, is there any reason not to use the Singleton lifetime for them?
source share