views:

188

answers:

6

Hi

I don't have much knowledge on IoC/DI frameworks in .net framework. Can anyone give me links that explans IoC/DI in detail with few example in C#? I want go through it and get more idea about these frameworks.

So that I can get the knowledge, where and How can I use these frameworks are useful in implementing the project.


Thanks
nrk

+1  A: 

Your best bet is too look at one of the IOC/DI website

Spring.net http://www.springframework.net/

Castle Windsor http://www.castleproject.org/container/index.html

Structure Map http://structuremap.sourceforge.net/Default.htm

Good articles on comparison of IOC

http://blog.ashmind.com/index.php/2008/08/19/comparing-net-di-ioc-frameworks-part-1/

http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx

I hope that helps. I personally have used Spring.Net and Castle Windsor - the latter probably easier to understand and use.

Have a look at this question as well. http://stackoverflow.com/questions/770039/examples-of-ioc-containers

Aim Kai
A: 

It's all about loose coupling.

Imagine you have a class foo that is dependant on class bar

public class Foo
{
      public Foo(Bar bar)
      {
      }
}

Here we have to pass an instance of bar into the class to instantiate it. What happens if we want to have two differant implementations of Bar, one for interacting with a DB and one for testing?

Well we would create and interface IBar and use that instead, then we can use two concrete implementations.

Now consider that we have lots of complex dependencies in lots of classes which are being called in lots of places, if we wanted to change the implementation we would have to change the code in every place a an object implementing IBar is created, that could be a lot of work.

Instead we can use dependancy injection like this.

IUnityContainer myContainer = new UnityContainer();
myContainer.RegisterType<IBar, Bar>();
Foo foo = myContainer.Resolve<Foo>();

Here the unity framework has inserted a Bar object into the Foo's constructor, if you changed the type registration in one place, you change how Foo objects will get resolved all over the place.

When you have complex dependencies that may change, DI is essential!

Paul Creasey
+1  A: 

Aim Kai has mentioned some very good resources on concrete IOC Container implementations and corresponding tutorials, however they are narrowly focused on the IOC Container beeing discussed and less of a general introduction/tutorial.

Personally, I like the introduction Rob Connery wrote best.

Johannes Rudolph
Yep Johannes has provided a good link here :) missed that one!
Aim Kai
+1  A: 

TekPub (also by Rob Conery) has a nice intro video in the Concepts series about Dependency Injection

http://tekpub.com/view/concepts/1

Enjoy!

TJB
Just watched this myself - very good intro to DI and IOC, recommended
Craig McGuff
+3  A: 

For a general introduction to the concept of DI, including comprehensive examples in C#, you may want to read my book Dependency Injection in .NET.

Mark Seemann