Designing and coding

Menu

Your Dependency Injection ready ASP.NET : ASP.NET 5

Dependency Injection has become one the key design patterns of the current projects. It helps in writing loosely coupled code which makes the code easily testable and maintainable. As per wiki

Dependency injection is a software design pattern that implements inversion of control for software libraries. Caller delegates to an external framework the control flow of discovering and importing a service or software module specified or “injected” by the caller.

In this post, we will see that how easily we can implement this pattern with ASP.NET 5 projects and leverage various available options .

ASP.NET 5 got completely redesigned and made DI friendly. It provides a by default container which can be used while writing the application. It also enables us to use third party containers with it and you may need a adapter to use with ASP.NET.

In ASP.NET 5, there is a new file added with the name startup.cs, which is the entry point of your ASP.NET application. It has two methods

1- public void ConfigureServices(IServiceCollection services)

2- public void Configure(IApplicationBuilder app)

ConfigureServices is a global method where all the services should be registered. Then all these services are available through out the application. Let’s first see that what all methods are available

Now let’s discuss one by one.

AddTransient – Registering a service via this method will create a new service instance each time accessed. Let’s see it via an example.

In this example, I have created a simple MVC application from an ASP.NET 5 Empty Project. Added on HomeController and Index view. I created a Repository as follows

In the above repository, I have a private integer variable which I am updating and returning via the method. This repository I will be using two or more places to show you the behavior. I have used it in my HomeController as

Here I used AddTransient to register the Repository. Now let us run this code

We can see here that we see 0 at both places. It means there are two instances get created for the repository as expected.

AddScoped – Registering a service via this method can be really useful if we want to use the same instance while processing a particular request. Say we are using the service at two places as above – one in Taghelper and other in controller. The same instance will be available. Let’s see it via an example

All the above code would be same, the only change would be the way we register in ConfigureServices method as

From the above screen, we can say that in a request, same instance of repository is used because here the counter is maintaining the state.

AddSingleton – As the name suggests, here on first request the instance of Repostory is created and the same will be used in across all requests. Let’s change the code for registering it as

Here we can see that state is maintained across multiple request.

AddInstance – This is similar to AddSingleton but the only difference is here that here the responsibility to create the instance is upto the developer.So lets create the instance and add that in Startup.cs as

Here I created the instance in constructor and added the instance.I purpose fully called the method in constructor to change the state of repository instance. Now lets run the code

Here we can see that the same instance (with same state) is used in across requests.

One of the classic example of this scenario would be a to create Configuration object.

Note – For the sample, Visual Studio 2015 CTP6 is used. In final release the feature may get changed slightly.

In this post, we discussed the default DI container available with ASP.NET 5. Demo is attached with this post. I created different folder named startupfiles which contains the startup.cs for all four scenarios. Use those files as we discussed to run each scenario.