2 08 2015
Dependency injection in ASP.NET 5
ASP.NET 5 has dependcy injection available at framework level and ASP.NET 5 makes heavy use of it. Most of things surrounding controllers, views and other MVC components are implemented as services that web applications consume. This post is quick overview of dependency injection in ASP.NET 5 with some examples.
Services are registered when application starts. It happens in Startup class. There is method called ConfigureServices() and in the end of this method I usually define service mappings.
public virtual void ConfigureServices(IServiceCollection services)
// configure services
var settings = new Settings();
// initialize custom settings
Available scopes for services are:
- SIngleton – always return same instance,
- Transient – return new instance every time.
- Scoped – return same instance in current scope (it’s like singleton in current scope – think about requst scope by example).
- Instance – specific instance is returned every time and it’s up to you how it is actually ceated
Services registered during application start-up are available to all classes invoked through dependency injection.
Although we can inject services to whatever classes you need there are some things made very convenient for us. Let’s see now how injection works with controllers and views. Yes, views too.
Injecting services to controller
We don’t need custom controller factories anymore if we don’t use some other dependency injection container. Also we don’t have to dig around in system variables and classes to find settings we need. We can do it all through dependency injection. Here is the example how to provide environment information and some custom services to controller.
public class HomeController : Controller
private readonly IApplicationEnvironment _appEnvironment;
private readonly ShopContext _shopContext;
private readonly IProductService _productService;
public HomeController(ShopContext shopContext,
_appEnvironment = appEnvironment;
_shopContext = shopContext;
_productService = productService;
public IActionResult Index()
// ... more methods follow ...
The code here uses controller injection – the only injecton mode supported by ASP.NET right now. We don’t have to do anything special for dependency injection to happen. We just make sure we register our custom services at application startup.
Injecting services to views
It’s also possible now to inject services to views. There’s new syntax for this.
@inject ShopContext ShopContext
<!-- write out categories here -->
@inject tells to view engine that we want instance of ShopContext to be injected to view and we name it as ShopContext. Perhaps it’s not a good practice to name variable as type in view but still it communicates the purpose of variable well.
Framework level dependency injection in ASP.NET 5 is very transparent and configuration is simple. First register type mappings in application start-up and then we use constructor injection to get instances to our classes. This way we can use classes by interfaces and we don’t have to create instances in our own code. On MVC side we can use dependency injection for controller, views and view components.