I read this article: http://peterspattern.com/dependency-injection-and-class-inheritance/ (great article btw)
Now, I have an ASP.NET Core web application, and I am thinking about putting all my services inside another class and use the built in DI to resolve this. Only reason I want to do this is to not have to manually update every constructor each time there is a need to add a new service to some class.
For example, instead of:
public class Foo {
IServiceOne _serviceOne;
IServiceTwo _serviceTwo;
public Foo(IServiceOne serviceOne, IServiceTwo serviceTwo) {
_serviceOne = serviceOne;
_serviceTwo = serviceTwo;
}
}
I can do this:
public interface IMyServices {
IServiceOne ServiceOne {get;}
IServiceTwo ServiceTwo {get;}
}
public class Foo {
IServiceOne _serviceOne;
IServiceTwo _serviceTwo;
public Foo(IMyService services) {
_serviceOne = services.ServiceOne;
_serviceTwo = services.ServiceTwo;
}
}
Edit: (I have left some parts out, like the class that implements IMyService and the registration of the service in Startup.cs)
Now, the application I am working on is quite large and the class that would be containing all the services could possibly contain 20-30 services.
My question is: is this a good idea? My concerns are with performance. Passing in all services in all constructors even if they are not needed feels like a bad idea. But it would save us tons of time if there would be only one class to update with a new service, then that service is available in all classes.
Is there perhaps someway to lazy load services when they are actually requested, or is that done by default?