If I have a base class with services injected via constructor dependencies: Is it possible to declare the constructor of the subclass without using : base (params)
?
public MyBaseClass
{
private IServiceA _serviceA;
private IServiceB _serviceB;
private IServiceC _serviceC;
public MyBaseClass(null, null, null)
public MyBaseClass(IServiceA serviceA, IServiceB serviceB, IServiceC serviceC)
{
_serviceA = serviceA;
_serviceB = serviceB;
_serviceC = serviceC;
}
}
And a subclass with some extra dependencies injected:
public MySubClassA : MyBaseClass
{
private IServiceD _serviceD;
public MySubClassA (null, null, null, null)
public MySubClassA (IServiceA serviceA, IServiceB serviceB,
IServiceC serviceC, IServiceD serviceD)
: base (serviceA, serviceB, serviceC)
{
_serviceD = serviceD;
}
}
The problem here is that I have multiple subclasses, only 10 or so right now, but the number will increase. Every time I need to add another dependency to the base class, I have to go through each and every subclass and manually add the dependency there as well. This manual work makes me think that there is something wrong with my design.
So is it possible to declare the constructor of MyBaseClassA
without having the services required by the base class in the sub classes' constructor? eg so that the constructor of MyBaseClassA
only has this much simpler code:
public MySubClassA (null)
public MySubClassA (IServiceD serviceD)
{
_serviceD = serviceD;
}
What do I need to change in the base class so that the dependency injection occurs there and does not need to be added to the sub classes as well? I'm using LightInject IoC.