How Can You Add The Dependency Injection into ASP.NET Core?
How to Add Dependency Injection in ASP.NET Core?
DotNet

How to Add Dependency Injection in ASP.NET Core?

In one of the Microsoft community stand-ups, there was a question by one of the community members: Ways to use dependency injection in ASP.NET Core. This blog post from well known ASP.NET Development firm is the answer to that question.

Yes, you all know that we are Microsoft Certified Partners and hence we keep an eye on each and every step of Microsoft. Here, in this blog post we will see how dependency injection can be used in ASP.NET Core and how it can be configured to meet the needs?

Dependency Injection in ASP.NET

First of all, let’s understand what Dependency Injection is all about?

Meaning:

Dependency Injection is an application design which allows class dependencies to be automatically added to an instantiated object. Total effect is a loose coupling between classes with dependencies that are assigned to a class as constructor parameters or properties.

These are referred to an interface in such a way that any concrete object which implements the interface can be easily passed in. Take a look at following design:

Design of a Tax Calculator by Git Hub:

public class TaxCalculator {
private readonly ITaxingAuthorityRules _Rules;
public TaxCalculator(ITaxingAuthorityRules rules) {
_Rules = rules;
}

public decimal CalculateTaxesDue(decimal income, decimal donations, decimal taxPaid) {
var taxRequired = _Rules.CalculateTax(taxableIncome, donations);
return taxRequired - taxPaid;
    }
}

This simple tax calculator task needs ITaxing AuthorityRules object that can be passed in order to get it created. This interface provides a method called “Calculate Tax” that returns a decimal.

Dependency Injection in ASP.NET Core:

With ASP.NET Core, dependency injection is the base of framework. All the classes represented by the framework can be done throughout the container service which is maintained by the framework in the container and configured by default in the Startup/Configure Services method.

This method looks like following in RC1 template:


public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddIdentity()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
services.AddMvc();

// Add application services.
services.AddTransient();
services.AddTransient();
}

Here, the identity configuration, MVC framework and mappings for an IEmail Sender and ISMS sender can be configured for dependency injection containers who are able to inject into other classes that it creates.

The IService Collection object passed into this method has several extension methods which is connected to it and enables simple mapping and registration of services.

With AddTransient method; container can create new instance of designated class for every instance of each created class that requires it. Add Singleton then creates one instance and passes it into each created class which requires it.

The Configuration that has developed in the Constructor of Start up class to the container with the Statement in Configure Services methods to read as follows:

services.AddSingleton(_ => Configuration);

Take Away

Dependency Injection is the design pattern for your classes. ASP.NET Core makes it easy to get started with design pattern by shipping the container which can be used with the application.

Further, the application’s controllers, views and other classes represented by framework with parameters on constructor method can have those types automatically created and passed in to your class.

So, next time when you feel the need to use dependency injection in ASP.NET; do follow this guide.

For more such tips related to ASP.NET, stay tuned with us.

Nitin Suvagiya

Nitin Suvagiya

He is working with Softqube Technologies as Director, since 2009. He has over 15+ years of experience in Microsoft Technologies, working as a CEO and also providing consultation in DevOps as he is DevOps certified. He has good expertise in UX/UI designing in any application.