Comparing DI frameworks with Sitecore ReflectionUtil

As a follow up on my last post about IoC / DI containers and how to use the Sitecore API to perform inversion of control using ReflectionUtil, I’ve made a quick speed comparison of some common DI frameworks, Sitecores ReflectionUtil and the new keyword as the best case for how quick it is to instantiate an object.

The results

The table below shows how long time it took to resolve and instantiate 1000 instances of a type without using constructor or parameter injection and without any lifetime management.

Ninject 27.360421582498
Unity 2.73812495641971
ReflectionUtil 0.19632199878594
Simple Injector 0.0757655566157367
new Keyword 0.0386374153259534

I ran the test 10 times and the results that I show here are the median, ranking from worst in top to best in bottom.

The loser is..

Ninject with no comparison. Wow that was so slow that I almost couldn’t believe it. Cool name though.

And the winner is..

Clearly the new keyword as expected, with Simple Injector as a runner up.

The morale

Inversion of Control using Dependency Injection is cool and it has many usages.

But only use it when you actually have a good reason to do so.

Please think before introducing a dependency on a third party framework.

Is the framework really needed to solve any real world issues or are you just adding complexity for the sake of technology?

If you need simple inversion of control for testing interfaces or allowing people to change your precompiled implementation etc. then you can use what is already present in the Sitecore API, good old ReflectionUtil. There is no need to introduce a new dependency on a third party framework. Life time management can be achieved in few lines of code as shown in my previous post.

Simple Injector really impressed me in regards to performance but I did not find it easy to use. I tried to pass in an object to the constructor which probably is a really simple thing to do when you first know how to do it, but the framework still lost a few points in my book due to the initial learning curve.

That said then Simple Injector is probably the IoC / DI framework that I will recommend when there is a good reason to use a DI framework at all.

Anders Laub Christoffersen

Anders has been working with Sitecore for over a decade and has in this time been the lead developer and architect on several large scale enterprise solutions all around the world. Anders was appointed the title of Sitecore Technical MVP in 2014 and has been re-appointed the title every year since then.

6 thoughts on “Comparing DI frameworks with Sitecore ReflectionUtil

  1. Its a pity ninject is so slow. One thing I really liked about ninject was that I didn’t have to have the reference to the DI container all over my code in an MVC app. I’m not sure how simpleinject works, but I believe using ReflectionUtil will require you to lookup (resolve) the implementation everywhere you need to inject it in, right?

    • There is an extension for Simple Inject both for MVC and WebApi. I guess it is similar to the extensions that exists for ninject, not sure, I never used any of them.

      ReflectionUtil does not have anything smart for ASP.NET MVC or WebApi. The class was made long before these frameworks and I doubt it has changed at all since Sitecore 5.3 🙂

      It would be quite easy though to implement automatic type resolving using ReflectionUtil..

      • I’m sure it can 🙂 I liked that I didn’t have to do it – I’m just lazy 🙂 Cccasionally I encounter them in other solutions, at that point I have to make decisions on whether to keep it, rip it out, change it, etc :-/ But I try to stay away from those frameworks as much as I can as well…

Leave a Reply

Your email address will not be published. Required fields are marked *