In this part of the vNext tale, I am gonna tell you about dependency injection in ASP.NET 5 (vNext) stack.
Default Dependency Injection container
First, let's see what is shipped with ASP.NET 5 by default. There is already simple DI container. It gives you a possibility to register service with three different lifetimes: scope, singleton and transient. For now it supports only constructor injection. Let's see how to use it and what is the difference between these lifetimes.
Create empty ASP.NET 5 project. Create simple service interface IService.cs:
public interface IService
{
int GetCounter();
}
and implementation SimpleService.cs:
public class SimpleService : IService
{
private int counter;
public int GetCounter()
{
return counter++;
}
}
Now create controller to test our service ServiceController.cs:
public class ServiceController : Controller
{
private readonly IService service;
public ServiceController(IService service)
{
this.service = service;
}
public int Get()
{
return service.GetCounter();
}
}
It is time to register our service in DI container, open Startup.cs and edit:
Singleton
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton<IService, SimpleService>();
}
Start your application and go to http://localhost/service/get, as you can see, number is increasing every time you reload the page, it is because our service was created when we first-time requested API method and all further requests will use the same instance of service.
Scope
Change registration code as below:
services.AddScoped<IService, SimpleService>();
Now, after each page reload you will see '0'. It is because service is creating per each request.
Transient
Change registration code:
services.AddTransient<IService, SimpleService>();
An output will be the same as in previous example. An object registered as Transient will be instantiated every time it is requested. To see this difference let's modify our controller's constructor:
public ServiceController(IService service, IService service2)
{
service2.GetCounter();
this.service = service;
}
Now it takes two services. In case of Transient registration, we will have two different instances, but if you specify Scope lifetime you will have the same instance in both parameters.
Use third-party DI containers (Autofac)
You can also use third-party dependency injection containers with ASP.NET 5. I will show you how to do that with Autofac. Why Autofac? It already has support for DNX Core and APS.NET 5.
First modify project.json, add Autofac in dependency block:
"Autofac": "4.0.0.0-alpha2",
"Autofac.Dnx": "4.0.0-alpha1"
Now, create Autofac module AutofacModule.cs:
public class AutofacModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder
.Register(c => new SimpleService())
.As<IService>()
.InstancePerLifetimeScope();
}
}
Modify Startup.cs:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// Create the Autofac container builder.
var builder = new ContainerBuilder();
// Add any Autofac modules or registrations.
builder.RegisterModule(new AutofacModule());
// Populate the services.
builder.Populate(services);
// Build the container.
var container = builder.Build();
// Resolve and return the service provider.
return container.Resolve<IServiceProvider>();
}
Do not forget to change ConfigureServices signature, it should return IServiceProvider. Start you app and you will see the same result as Scope example.
This is how you can use dependency injection in APS.NET 5 (vNext). In the next part, I am going to tell you how to use Automatic migrations with NHibernate.