Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

It is kind of theoretical question.

I am already using Unity DY with Service(Facade) pattern on Business layer. I is pretty simple to use it but...

there is obviously performance and memory overhead in every small transaction. Instead of creation of DataContext (read it like "sql-connection") i am create several service objects by unity.

Example: simple operation "GetAllArticles" cause creation of

Useless:

  • UserService (for permission checks)
  • ArticleService (for Article Crud operation)

And usefull:

  • DataContext (for the articleService)
  • ArticleViewModels.

But what if is an HightLoadApplication, and billions of people over the world, trying to get article from my super site? What about garbage collector and server's cpu temperature?

So:

  • Am i understand work with unity(or any other) correct?
  • Is there any alternative solutions?
  • what should i do in a case of highload application

I will be happy to hear your opinion and experience, even if it is not panacea or "best practice".

like image 548
tmt Avatar asked Feb 04 '16 21:02

tmt


2 Answers

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.

like image 137
Win Avatar answered Nov 16 '22 16:11

Win


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.

like image 4
FailedUnitTest Avatar answered Nov 16 '22 16:11

FailedUnitTest