I have two classes, A & B. B is inheriting from A, I want to inverse the dependency.
Class A { }
Class B : A { }
Class B is inheriting from A. It means B has some dependency from A.
What will be the correct way to inverse the dependency?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Inheritance is a concept implying tight coupling between classes.
In order to use Dependency Injection you need to create some “Seams”, as Michael Feathers calls them in Working Effectively with Legacy Code. Here you can find a definition of Seam:
By this definition, there is no Seam in your example, which is not necessarily a bad thing. The question is now, why do you feel the need to do Dependency Injection in this place?
If it’s for the sake of example, don’t do Dependency Injection here. There are places where it does not really make sense to apply it: if you have no volatility, why would you do it?
If you do really feel the need to do something similar in your project though, you probably want to decouple the volatile concepts out of your inheritance hierarchy and create a Seam for these parts: you could have an interface to abstract these concepts, which at this point can be effectively injected into your client class.