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?
-
charkit: I think the question on its own is pretty platform-agnostic. Some platforms may force you to write one, since they've got no existing one, but other than that ... – Joachim Sauer Dec 22 '08 at 15:44
12 Answers
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.

- 9,260
- 3
- 35
- 41
-
7And then read his follow-up about why you shouldn't make your own: http://ayende.com/Blog/archive/2007/10/20/Dependency-Injection-doesnt-cut-it-anymore.aspx – Pat Nov 12 '10 at 15:53

- 59,778
- 26
- 187
- 249
-
-
@KenEgozi Your promotions, marketing and SEO teams need to up their act I reckon :D – Ruben Bartelink Feb 20 '14 at 21:06
-
1
-
1
-
@callumWatkins thanks for the alert; fixed. If I see this comment again with an ack and yours gone, I'll delete this... – Ruben Bartelink Mar 12 '17 at 19:33
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.

- 136,852
- 88
- 292
- 341
-
1yea why write your own. Just use this. Phil Haack (Manager of the MVC team) uses it also for Subtext. – PositiveGuy Oct 27 '10 at 02:45
If you are looking for lightweight & high performance IoC container, then you should check out Munq

- 808
- 9
- 21

- 64,778
- 30
- 169
- 213
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.

- 1,272
- 1
- 9
- 22
Autofac is excellent.
I've written one myself using less than 15 lines. Just two extensionmethods to a dictionary.

- 99,844
- 45
- 235
- 372
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.

- 1,817
- 15
- 23
IOC container is not hard to write, it is just a well managed global recursive factory with some potential additional features. Using dictionary, reflection and delegate to register and build a simple container...
The real question is why and how another new IOC container framework can bring benefits?
In most of cases, you think you need more performance? non existing features? But most of time, existing frameworks is just what you need and enough, unless you have become aware of all the nonsense that the framework has forced you to do to use it.
has the force of being disappointed by all the implementations of framework of ioc container of has features that are of the order of the antipattern, but also quirky and unreliable syntaxes and worse in ore of imposed couplings, I decided myself to experience it. This is why I made my own (very light) IOC container as open source.
You can check it here : Puresharp API .net 4.5.2+

- 772
- 5
- 11
I created my own IoC container that makes it easier to debug the creation of the object (even when you have no access to the container code). When the object is created, when pressing Step into (F11) you see the code to create the object. Full code can be seen here.

- 2,121
- 26
- 50

- 2,499
- 23
- 31
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.

- 14,173
- 2
- 42
- 60
-
1Dup of http://stackoverflow.com/questions/386535/c-code-your-own-ioc-container/386618#386618 – Ruben Bartelink Nov 13 '09 at 09:11
The thing is that there are so many IoC and DI libraries which makes you confused. When you develop something with one of them and you grow, you'll tightly couple your product with such tools and you'll need experts in those to continue the development. It's all about the politics of the company and the design complexity.
I myself planned to do it manually therefore no much hidden code. I know there are bunch of great open source IoC tools but who actually goes through the code and tries to understand?
Not about reinventing the wheel but sometimes it's good if you can your own custom wheel that fits your product better.

- 326
- 5
- 18