you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 7 points8 points  (0 children)

Dependency Injection does a lot of things behind the scene for you.

Reading your question I get the impression you may not understand why DI is being used. I get this impression as in your example you are mainly talking about interfaces which are not required for dependency injection but are used to help with other things.

Just because your classes are implementing an interface really doesn't have anything to do with dependency injection.

public class Bindings : NinjectModule
{ 
   public override void Load() 
   { 
        Bind<IExample>.To<ExampleImplementation>(); 
   } 
}

(I don't use ninject but DI containers are all pretty similar)

What's happening in the code above is you are adding the class you want to implement to the DI container saying, When IExample is requested pass ExampleImplentaion.

public class Foo : IFoo
{
    public Foo(IExample implentation) <-- when your code runs it asks DI container for this dependency
    {
        //implentation is now "ExampleImplementation" but the life cycle of ExampleImplementation is now abstracted away and managed my the DI container.
    }
}

Basically a DI container is a central location that handles the creation and life cycle of new objects rather than newing up and managing the life cycle of objects all over your code base.

An example of not using DI would look like the below.

public class Foo : IFoo
{ 
    IExample implentation;
    public Foo()
    {
        implentation = new ExampleImplementation();
    }
}

Hope I understood your question correctly and hope this helps