Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection and Class Inheritance

I feel like this is something I should already know, but I'm just not firing on all engines today...

I have a base class with a single ctor that takes an implementation of an interface as it's only parameter. I'm using a DI framework and have my component registrations all set up and working fine.

When I inherit from this base class, unless I pass in a value to the base constructor, I have to define a parameterless ctor, which bypasses the DI.

So right now I have:

public class MyObjectBase
{
    IMyRequiredInterface _InterfaceImpl;
    public MyObjectBase(IMyRequiredInterface interfaceImpl)
    {
        _InterfaceImpl = interfaceImpl;
    }
    ...
}

public class AnotherObject : MyObjectBase
{
    public AnotherObject()
    {
    }
    ...
}

So, out of the gate this fails. I get errors when AnotherObject is instantiated indicating that there is no base class ctor that takes 0 parameters. Ok, I get that. But now I have a choice: either modify the descendant class ctor to take a similar parameter and pass that value on to the base ctor, or wire up a ctor chain in the base class that forces me to bypass DI and create a concrete implementation of the required interface and pass it in as part of the parameterless ctor declaration.

The goal is to meet the requirement of the base class without the descendant classes knowing anything about it.

Maybe I'm going about this all wrong, but it's bugging me. Any thoughts on a better way to handle this? I feel like I've got to be missing something simple...

like image 975
nkirkes Avatar asked May 25 '09 19:05

nkirkes


People also ask

What is difference between inheritance and dependency injection?

One criticism of inheritance is that it tightly couples parent class with child class. It is harder to reuse the code and write unit tests. That's why most developers prefer dependency injection as a way to reuse code. Dependency injection is a way to inject dependencies into a class for use in its methods.

What is inheritance dependency injection OOP?

In object-oriented programming (OOP) software design, dependency injection (DI) is the process of supplying a resource that a given piece of code requires. The required resource, which is often a component of the application itself, is called a dependency.

What is inheritance dependency?

Inheritance forces dependency between a superclass and its subclasses. But if this dependency is well designed, not only it is harmless, it could actually be useful and make code conceptually cohesive. For example, when you see an instance of subclass, you can assume you can treat that instance like its parent.

Can classes be inherited?

The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive.


1 Answers

The correct approach is:

public class AnotherObject : MyObjectBase {
    public AnotherObject(IMyRequiredInterface interfaceImpl) : 
        base(interfaceImpl) {
    }
}

You specifically asked for an approach other than this approach. Why?

The goal is to meet the requirement of the base class without the descendant classes knowing anything about it.

That's generally the wrong thing to do. Why do you want to do it?

Update:

Based on your later comment, you should probably use (and configure your container to use) property injection instead of constructor injection. That will get you all of your requirements.

like image 131
yfeldblum Avatar answered Oct 05 '22 12:10

yfeldblum