I have a code base where constructor dependency injection is used a lot, mainly because it makes testing much easier. So there’s a lot of code like this:
class Client
{
public:
Client(DependencyBase& s) : s_{s}{}
private:
DependencyBase& s_;
};
The problem with this approach is that now parent context must construct and own all dependencies although it has no use for them. It also makes the code ugly as constructor initializer or argument lists may become very long. Is there a better way to structure the code to avoid this problem? Unfortunately dynamic memory allocation is not allowed...
Thanks for any advices!