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?
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.
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.
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.
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.
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.
Here's another implementation. 110 lines of code and a complete implementation of the Common Service Locator. (self promotion!)
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.
If you are looking for lightweight & high performance IoC container, then you should check out Munq
Cheers
Autofac is excellent.
I've written one myself using less than 15 lines. Just two extensionmethods to a dictionary.