Click here to Skip to main content
14,825,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Hi Friends,

Hope all doing well.

I'm new to dependency injection. i got a doubt while reading about Ninject.

In Ninject wiki i saw an basic example for Dependency Injection. From that my doubt arises.


This is the link. https://github.com/ninject/ninject/wiki/Dependency-Injection-By-Hand[^]

C#
class Samurai
{
    readonly IWeapon weapon;
    public Samurai(IWeapon weapon)
    {
        this.weapon = weapon;
    }

    public void Attack(string target)
    {
        this.weapon.Hit(target);
    }
}
class Shuriken : IWeapon
{
    public void Hit(string target)
    {
        Console.WriteLine("Pierced {0}'s armor", target);
    }
}
class Program
{
    public static void Main() 
    {
        var warrior1 = new Samurai(new Shuriken());
        var warrior2 = new Samurai(new Sword());
        warrior1.Attack("the evildoers");
        warrior2.Attack("the evildoers");
    }
}


Below is the statement mentioned in that link.
"When classes are tightly coupled, they cannot be interchanged without altering their implementation. In order to avoid tightly coupling classes, we can use interfaces to provide a level of indirection."

Now my doubt is. If i want to create new class called Dress and inject to Samurai class. That time also i need to rewrite Samurai class know like below

C#
class Samurai
{
    readonly IWeapon weapon;
    readonly IDress dress
    public Samurai(IWeapon weapon, IDress dress)
    {
        this.weapon = weapon;
        this.dress = dress;
    }

    public void Attack(string target)
    {
        this.weapon.Hit(target);
    }
    
     public void Wear(){


     }
}

. Or else do i have any other option???
Posted
Updated 14-Dec-13 6:26am
v2

What is your expectation on DI?

DI does not magically extend functionality.
DI allows to pass dependencies explicitly from outside instead of implicitly instanciating the dependencies.
If you add in your Samurai class some new dependency, you have to provide means to pass them (DI) to the constructor.

Your question is asked the wrong way round. It should be:

"I have a Samurai class that has a new dependency added to Dress. Do I have to pass that dependency to the constructor to implement DI?"

Answer: "yes".

Cheers
Andi
   
If you are going to extend a Class to use another instance of another interface, then ... yes ... you will need to change the Class: if you didn't how would the Class ever "know" about, or be able to use, the added interface ?

However an option would be extend some larger-scope entity that the Class inherits from ... or is injected an instance of an interface of.

For example, you could have an interface called ISamuraiGear that could include both Weapon and Dress. The Samurai class would then get an instance of that injected. So, you could extend WarriorGear without adding another interface.

But, at the point an instance of the Samurai class would want to use/invoke some Property/Method of its Dress instance, yes, obviously the instance of the Samurai class will have to be changed to explicitly use/invoke the Dress instance.

imho, the article you cite is a terrible example of DI, and you can find better explanations here on CodeProject: [^].

My favorite explanation of DI is John Munsch's classic answer to the question "how do you explain dependency injection to a 5 year-old ?:"
When you go and get things out of the refrigerator for yourself, you can cause problems. You might leave the door open, you might get something Mommy or Daddy doesn't want you to have. You might even be looking for something we don't even have or which has expired.

What you should be doing is stating a need, "I need something to drink with lunch," and then we will make sure you have something when you sit down to eat.
[^]. The link is to StackOverflow; and the text is also quoted in "Dependency Injection in .NET" by Mark Seemann (Manning Press, 2011), Ch. 1, p. 4.

In that book Seemann states: "Dependency Injection (DI) is a set of related patterns and principles. It’s a way to think about and design code more than it’s a specific technology. The ultimate purpose of using DI is to create maintainable software within the object-oriented paradigm. ... DI in isolation is just a small thing, but it’s closely interconnected with a large complex
of principles and patterns for object-oriented software design."
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900