Dependency injection (DI) is an important topic to consider when building a .NET application (or any for that matter). It is difficult to argue the benefits… testability, loose coupling, and so on. If only choosing an IOC container were as simple as deciding to use DI :).
Introduction
Over the years, StructureMap has been my go to container. It has a lot of nice registration features and the performance seemed good enough. I’ve also used Unity, Autofac, and Ninject which offer similar features and performance.
This time, I decided to take a bit of a different approach…
Performance & Features
When looking for an IOC container, this link always seems to come to the top of my google searches. The analysis is in-depth and there is a great comparison of feature sets and performance. I highly recommend scrolling through!
After taking a close look, I realized the containers I usually gravitate towards were either average or slow from a performance perspective. I have worked on a lot of high transaction applications so for me, average performance won’t do!
Container | Singleton | Transient | Combined | Complex |
---|---|---|---|---|
Autofac 3.5.2 | 713 | 1650 | 4515 | 12625 |
DryIoc 2.4.3 | 31 | 43 | 62 | 91 |
Ninject 3.2.2.0 | 5224 | 16579 | 46445 | 136712 |
StructureMap 4.2.0.402 | 1413 | 1546 | 4181 | 10955 |
TinyIoC 1.3 | 455 | 1576 | 7081 | 29000 |
Unity 4.0.1 | 2531 | 3857 | 10225 | 28572 |
A Decision
It seemed as though I was faced with a decision.
Should I use a feature rich container like StructureMap or Ninject at the cost of performance? Or should I go all in for speed and use a container like DryIoc?
Paper or plastic? Debit or credit? Features or speed?
And then it dawned on me…
Features AND Performance
I tend to use my IOC containers in a relatively simple way. There are a few pieces that I rely on and would never want to implement myself.
- Instantiating objects (efficiently)
- Handling life-cycles
- Managing dependencies
The remaining pieces are where the convenience features lie.
- Type Scanning
- Convention based registration
- etc
As it turns out, implementing these features is actually quite simple. Being the control freak that I am, I love the flexibility this allows. With this in mind, I decided to use the absolute fastest container and handle all the type registration on my own.
Check out my next post on how I implemented DryIoc!