proper use of dependency injection
NickName:benmartin101 Ask DateTime:2020-01-25T03:08:50

proper use of dependency injection

I've been using dependency injection, and I noticed that in the constructors (that you are injecting to), the parameters are usually an interface or a base class. I think I understand the reason why the parameters is either interface or base class. However, I realize there are times where the actual class/object that I'm trying to inject could use a method that neither the interface or base class should have (since only the actual class/object I'm instantiating should have that method). But if that's the case, that means I am unable to call that method if the parameters injected is the interface or base class. Should I just create another interface that would contain that method I want on the actual class/object I'm trying to instantiate? I have an example below:

public interface IAnimal
{
   void Eat();
}

public abstract class Mammals : IAnimal
{
   public void Eat()
   {
      //eating code
   }

   public void Sleep()
   {
      //sleep code
   }
}

public class Whale : Mammals
{
   public void Swim()
   {
      //swim code
   }
}

public class Dog : Mammals
{
   public void Run()
   { 
      //run code
   }

}

//Injecting here
public class Class1
{
    private readonly Mammals x;
     public Class1(Mammals x)
     {
         this.x = x;
     }

     public class Method1()
     {
         x.Eat();
         x.Sleep();

         //I can't call swim() unless I do below:
         //Whale y = x;
         //y.Swim();
     }

}

I've always thought that using Interface or base class (in depedency injection) allows you to make cleaner code because if I decide to change the class/object I'm injecting, I don't have to make any changes to the codes because whatever object I inject will either have the interface or has the base class. But if I add that code allowing me to use the Swim(), and later decide to inject the Dog class, I would have to change the code to use Run() instead. I'm trying to fully understand dependency injection and the proper way of utilizing it. I've noticed my codes definitely look cleaner because of it. How should I go about the issue of the above code?

Copyright Notice:Content Author:「benmartin101」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/59902340/proper-use-of-dependency-injection

Answers
Jorn Vanloofsvelt 2020-01-24T19:15:48

If this class is really dealing with any kind of mammal, and you want to make some of them swim, then you can test if the injected object implements ISwim.\n\nFor example (mammal as ISwim)?.Swim();",


More about “proper use of dependency injection” related questions

proper use of dependency injection

I've been using dependency injection, and I noticed that in the constructors (that you are injecting to), the parameters are usually an interface or a base class. I think I understand the reason w...

Show Detail

Proper dependency injection in Android with(out) DI containers (Dagger 1)

I am currently developing (actually structuring) an Android app with Bluetooth connection & HTTP communication with RESTful service. I happened to come across a "dependency injection" framework

Show Detail

When to use Dependency Injection

I've had a certain feeling these last couple of days that dependency-injection should really be called "I can't make up my mind"-pattern. I know this might sound silly, but really it's about the

Show Detail

When to use Dependency Injection

I've had a certain feeling these last couple of days that dependency-injection should really be called "I can't make up my mind"-pattern. I know this might sound silly, but really it's about the

Show Detail

Dependency injection and contextual dependency injection

i don't know what is dependency injection .Is there any difference between dependency injection and contextual dependency injection .Can any one help me

Show Detail

Java Dependency Injection with Multiple Implementations

My question: is there a way to use javax.inject (or any other Java injection framework) for a consumer of a Provider to use multiple implementations at runtime if the number of implementations is u...

Show Detail

Little bit confused with Dependency Injection

I was reading http://symfony.com/doc/master/components/dependency_injection/introduction.html and I ended up confusing myself more then answering questions. I want to use a DI in my project so I ca...

Show Detail

how to implement dependency injection container and dependency injection

I want to know how to implement a dependency injection container using php(for study). I get the concept of dependency injection and i can implement it but I can only factor out until controllers o...

Show Detail

Association Injection or Dependency Injection?

I'm studying the dependency injection pattern. I have read many examples, among which one typical example is to use XxxService/XxxRepository as example. But from my opinion, according to the UML co...

Show Detail

What is dependency injection?

There have been several questions already posted with specific questions about dependency injection, such as when to use it and what frameworks are there for it. However, What is dependency inject...

Show Detail