Why should I use IoC Container (Autofac, Ninject, Unity etc) for Dependency Injection in ASP.Net Applications?

When we write code, we aim for SOLID Design Principals which make code adaptive to change.

  • S : The single responsibility principle
  • O : The open/closed principle
  • L : The Liskov substitution principle
  • I : Interface segregation
  • D : Dependence injection

In order to achieve first four - SOLI, we want to inject dependencies.

Is there any alternative solutions?

You can achieve dependency injection (DI) either manually (Poor Man's Dependency Injection) or using Inversion of Control (IoC) container (like Autofac, Ninject, Structure Map, Unity and so).

what should i do in a case of highload application

Using IoC container for DI is never been an issue for speed.

Mark Seemann said , "creating an object instance is something the .Net Framework does extremely fast. any performance bottleneck your application may have will appear in other place, so don't worry about it."

The bottom line is I personally use IoC container in every ASP.Net MVC and Web API projects. Besides, I hardly see any open source MVC and Web API application which does not use IoC container.


For understanding how DI works, take a look at this great article: http://www.martinfowler.com/articles/injection.html

I also recommend reading even half of this book by Mark Seemann: http://www.amazon.ca/Dependency-Injection-NET-Mark-Seemann/dp/1935182501/ref=sr_1_1?ie=UTF8&qid=1454620933&sr=8-1&keywords=mark+seemann

Unless you are trying to set a performance record I do not believe DI will have a noticeable effect on performance. We have been using SimpleInjector for the past year (it is one of the fastest out there) on a website that gets several million hits per day and the performance effect is almost unmeasurable.