Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help getting DI/IoC in house

I am trying to introduce DI/IoC programming methodology into our development group, but one of the developer asked the following question:

Why do we need it? Is there any concrete example that can show me the benefit of using DI/IoC framework like windsor castle?

Therefore, I am asking if there are any case study or article out there that prove DI/IoC can benefit in a enterprise-level .NET website?

Thanks in advance

Update: I am aware of all the benefit of DI/IoC brings, but I have yet to see a complete example on the web that goes through a entire process of creating a app utilizing DI/IoC and benefit from it. Again, any article or links would be appreciated.

like image 353
Herman Avatar asked Sep 10 '09 04:09

Herman


People also ask

Is DI and IoC both are same?

Inversion of Control(IoC) is also known as Dependency injection (DI). The Spring container uses Dependency Injection (DI) to manage the components that build up an application and these objects are called Spring Beans. Spring implements DI by either an XML configuration file or annotations.

Why IoC is required?

The IoC container is a framework used to manage automatic dependency injection throughout the application, so that we as programmers do not need to put more time and effort into it. There are various IoC Containers for . NET, such as Unity, Ninject, StructureMap, Autofac, etc.

Is dependency injection Same as IoC?

Dependency Injection is the method of providing the dependencies and Inversion of Control is the end result of Dependency Injection. IoC is a design principle where the control flow of the program is inverted. Dependency Injection is one of the subtypes of the IOC principle.

Why do I need an IoC container as opposed to straightforward DI code?

An IoC container is closely related to dependency injection (DI) where you inject objects into a class instead of creating the objects inside the class. You can basically think of an IoC container as a tool that helps you do this in a more flexible and customizable way.


2 Answers

I'm working on a project where I can see at least three benefits to Dependency Injection and Inversion of Control:

  1. The flexibility DI and, to a lesser extent, IoC allow as it pertains to unit testing. We can zero in on a particular aspect of code (or system under test) and test this bit of functionality without needing to prep a database table or be subject to the whims of sections of code we're not concerned with at the moment.

  2. Injecting dependencies via IoC is a fairly seamless, automatic thing, and it allows people to work on logic without requiring that the underlying support classes are complete. For example, I can write a web page that shows a list of users without having written any code to retrieve that information from the database. That can be written by someone else, possibly in parallel, so more work can get done in less time.

  3. On one of my current projects, I want to have the ability to demo the web user interface and back-end processing to one of the stakeholders. This is made so much easier by DI and IoC because I can have a collection of fakes that supply the exact data I need to conduct the demo. This way, I'm not freaking out the day before with making sure database tables are populated the way I expect them to be.

DI encourages a loose coupling between a particular class and its dependencies, while IoC allows us to dynamically configure which implementations of these dependencies are injected into classes that use them. The latter is important with respect to #3, because my web application will most be configred with IoC based on settings I've made to the web.config file. I will need to change only that file when we go to production and begin to use non-fake classes.

like image 131
David Andres Avatar answered Sep 28 '22 15:09

David Andres


To name the few particular benefits:

  • Cleaner code, that carries the essence of your business logic in it, not the infrastructural thickness
  • DI allows you to create more modular applications by providing a mechanism for decoupling your app layers
  • IoC allows to externalize application wiring/bootstrapping and provides centralized(sort of) resource management(to some extent). Implication - you have more time to concentrate on the actual functionality/business logic

Some good info about IoC and DI can be read here: http://www.theserverside.com/tt/articles/article.tss?l=IntrotoSpring25

Granted it is about Spring Framework, general DI concepts still apply.

like image 34
Alex N. Avatar answered Sep 28 '22 15:09

Alex N.