31

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?

apaderno
  • 28,547
  • 16
  • 75
  • 90
Peanut
  • 18,967
  • 20
  • 72
  • 78
  • 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 Answers12

17

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
  • 9,260
  • 3
  • 35
  • 41
  • 7
    And 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
9

I liked this 33 line container implementation from Ken Egozi inspired by Ayende's 15 liner

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
4

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.

Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
2

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 out IInject, it can downloaded here.

sbaker
  • 89
  • 1
  • 7
2

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

MarwaAhmad
  • 808
  • 9
  • 21
RameshVel
  • 64,778
  • 30
  • 169
  • 213
2

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,272
  • 1
  • 9
  • 22
1

Autofac is excellent.

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

jgauffin
  • 99,844
  • 45
  • 235
  • 372
1

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
  • 1,817
  • 15
  • 23
1

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+

Tony THONG
  • 772
  • 5
  • 11
0

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.

vanlooverenkoen
  • 2,121
  • 26
  • 50
Alex Siepman
  • 2,499
  • 23
  • 31
0

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
  • 14,173
  • 2
  • 42
  • 60
0

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.

Yasser Jarouf
  • 326
  • 5
  • 18