How to Add Dependency Injection in ASP.NET Core?
April 7, 2016
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?
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.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
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.
Share on