Dependency Injection occurs when a software component (in this case, the class) given their dependencies through their constructors, methods, or directly into the fields. Simply put, Dependency Injection is passing or setting of dependencies into a software component (Dependency Injection Container). In other words, if a class can not do his job without a dependency, then there was a dependency injection. Classes are diinject, not only can build the object, but also its behavior.
Dependency injection can be done through three ways :
- Constructor Injection
- Setter Injection
- Interface Injection
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.
Registering services
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
services.AddInstance(settings);
services.AddScoped<IFileClient, AzureFileClient>();
services.AddScoped<IMapperSession, MapperSession>();
services.AddScoped<IProductService, ProductService>();
}
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.
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,
IProductService productService,
IApplicationEnvironment appEnvironment)
{
_appEnvironment = appEnvironment;
_shopContext = shopContext;
_productService = productService;
}
public IActionResult Index()
{
return View();
}
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.
@model ProductCategoryMenuModel
@inject ShopContext ShopContext
<h1>@ShopContext.PageTitle</h1>
<ul>
<!-- write out categories here -->
</ul>
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.
Conclusion
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.
HostForLIFE.eu ASP.NET 5 Hosting
HostForLIFE.eu revolutionized hosting with Plesk Control Panel, a Web-based interface that provides customers with 24x7 access to their server and site configuration tools. Plesk completes requests in seconds. It is included free with each hosting account. Renowned for its comprehensive functionality - beyond other hosting control panels - and ease of use, Plesk Control Panel is available only to HostForLIFE's customers. They offer a highly redundant, carrier-class architecture, designed around the needs of shared hosting customers.