-2

I am new to the concept of Unity/Dependency Injection.

My understanding is that you use DI to avoid tightly coupled class structure but I am struggling to see its benefits.

I followed "The Unity Container" section on this link http://www.refactorthis.net/post/2012/10/25/Dependency-Injection-and-Inversion-of-Control-Ioc-With-the-Microsoft-Unity-Container.aspx but I do not know why this is "better" - it introduces a lot more code and still, inside LoggingModule.cs I still have to have this line of code:

_iocContainer.RegisterType(typeof(ILogger), typeof(DBLogger));

Meaning there is still a dependency but I have just moved it into a secluded location. I still have to tell Unity which class I want to use

This question will probably be marked as non-constructive but I would like to be told the benefits and how to use Unity/DI correctly.

Thanks, Andrew

andrewb
  • 2,995
  • 7
  • 54
  • 95
  • It is IOC Container. It should has nothing to do with your understanding of DI, except it will only help to `wiring` your interfaces and objects for injection resolve. See this analogy for DI http://stackoverflow.com/questions/1638919/how-to-explain-dependency-injection-to-a-5-year-old – Fendy Aug 20 '13 at 08:34
  • Start [here](http://stackoverflow.com/tags/dependency-injection/info) to learn more about dependency injection. – Steven Aug 20 '13 at 10:06

1 Answers1

1

The benefit is that your services now depend on abstractions (ILogger) and the resolution of the abstraction is responsibility of the container. Besides, the declaration of your dependencies and which concretions to use and their life time is centralized in the Composition Root (the place where you register the dependencies in the container).

I suggest you to read a good book about dependency injection, there are several out there.

onof
  • 17,167
  • 7
  • 49
  • 85