Home  /  Questions  /  Question

72   96.0
Jun 30, 2010

What are the benefits and dangers of IoC (Inversion of Control)?

What I find is that the Inversion of Control pattern (also called DI - Depenency Injection) is hard to grasp.

I can see the mechanics, but what problems does it try to solve?
IoC has is a lot of coding overhead, but I am not clear about the exact benefits.

A follow up question. Do you take IoC to Production or is it only for Testing?
I am asking because there is the risk of deploying incorrect xml configuration files. 
Does anyone ever talk abou this risk?

1,128   99.9
Jul 02, 2010
What problems does it try to solve?
  • Decoupling components and layers in the system
  • Alleviates a component from being responsible for managing it's dependencies
  • Swap dependency implementations in different environments.
  • Allows a component be tested through mocking of dependencies.
  • Provides a mechanism for sharing resources throughout an application.
  • Makes it easier to test your code. Without it, the code you are testing is hard to isolate as it will be highly coupled to the rest of the system.
  • Useful when developing modular systems. You can replace components without requiring recompilation.

Do you take IoC to Production or is it only for Testing?
I certainly use it on production as well as for testing. Note that there are DI frameworks that doesn't use Xml (Ninject be one of them).

 1 comment
If I wanted to add Ninject to this project and remove DataAccess which layer would I put it in and how? Example I want to remove private static readonly IAuditDao _auditDao = DataAccess.AuditDao; from the service layer would I add Ninject in service in place of private static readonly IAuditDao _auditDao = DataAccess.AuditDao;? Or do I add Ninject in Presentation layer? Thanks Dennis --- Dennis Tucker  Jun 11, 2012