Dependency injection everywhere

By Dawid on (tags: ASP.NET, dependency, injection, IoC, next, razor, categories: code)

Just a few years ago there was just few people who known what dependency injection is. Today it is almost everywhere. Recently I’ve discovered that it can be also used in Razor views – thanks to ASP.NET 5.

I will try to show you how we can use that feature on few examples bellow.

Probably each of you have been populating drop downs in Razor views with data send from controller. I’ve been doing loot of time in following way:

And here we have Razor view code:

 

And that’s all – new approach, more generic (we can use the same service in different parts of the project),  more dependency injection.