views:

3496

answers:

10

Has anyone out there written their own IOC Container in C#? Or do the vast majority of folks use the various frameworks such as Spring. What are the pro's and con's of each?

+2  A: 

Someone has wrote one in C# : http://ninject.org/.

It's open source so you can get the code and see how this guy did it.

Daok
yea why write your own. Just use this. Phil Haack (Manager of the MVC team) uses it also for Subtext.
CoffeeAddict
+8  A: 

It's a good excercise to write your own but in the end you might want to use an existing container. You could start with this one in 15 lines of code.

Cristian Libardo
+2  A: 

Unless there's a very good reason I wouldn't go reinvent the wheel and implement a IoC container myself, specially because there are are a lot of good options like Unity, Ninject or Spring.net.

If you need/want to remove the dependency to any of these IoC containers you may try out the Common Service Locator interface.

t3mujin
+1  A: 

James Kovacs presents a dnrTV episode on this subject here. Here also wrote an article. However during the article he mentions that you would probably want to use one of the pre-built ones. Since there are many diverse looks for them. Ninject, StructureMap, Autofac use a fluent interface. Spring, Castle Windsor, and Unity are more XML config driven. Castle Windsor can also use boo as an interface. Many have hooks to other frameworks such as Unity to EntLib or Castle Windsor to Monorail and the rest of the Castle Project.

So unless you really need or want something that is not provided by the IOC frameworks available, then why not use one of them.

Thedric Walker
A: 

Ayende also wrote about writing your own IoC container in his blog post Building an IoC container in 15 lines of code, I believe he's of the same opinion as everyone else though: don't build your own if you don't have to.

James Gregory
Dup of http://stackoverflow.com/questions/386535/c-code-your-own-ioc-container/386618#386618
Ruben Bartelink
+3  A: 

Here's another implementation. 110 lines of code and a complete implementation of the Common Service Locator. (self promotion!)

Steven
Service Locator is an anti pattern.
Finglas
@Finglas: I agree that the Service Locator pattern is an anti-pattern. As a matter of fact, I always advise developers to use dependency injection (DI) when they have a choice. However, please understand that the Common Service Locator does not dictate how to use it. It depends on the actual implementation. And the Simple Service Locator (SSL) (an implementation of CSL) is very well capable of doing DI. The name of SSL is perhaps a bit unfortunate, but was chosen because it is an implementation of CSL.
Steven
Nothing is an anti pattern unless it is misused. All these techniques and patterns have their place if used appropriately and pragmatically.
Stimul8d
A: 

I have written an IoC / DI Container in c# that implements the Common Service Locator. I wrote it mostly for learning purposes, but when I completed it, I decided to make it open source. If any of you would like to try it out, I just released version 1.1.3 of LogikBug's IoC Container and it can downloaded here.

sbaker
A: 

If you are looking for lightweight & high performance IoC container, then you should check out Munq

Cheers

Ramesh Vel
A: 

Autofac is excellent.

I've written one myself using less than 15 lines. Just two extensionmethods to a dictionary.

jgauffin