RSS icon
Carl Chester Lloyd

PostsAboutReading

Dependency Injection in DotNet Core

June 21, 2020

houseofcards

Dotnet core has some great built-in features for enabling dependency injection in your services, and I wanted to get more familiar with them. This article is the result of that. Hopefully you can get something out of this too.

What is Dependency Injection?

Dependency Injection or DI is a way to connect the various parts of a program in a way that is highly modular, and not very likely to break. Put different it is a way of implementing Dependency Inversion. Dependency Inversion is one of the SOLID principles where a program is written to be dependent on interfaces rather than classes. Dependency Injection allows one part of the code to make use of another part of the code without being overly dependent upon a particular implementation.

What are the built-in ways of using Dependency Injection?

Dotnet core allows registration of services with dependency injection on startup of the service. This is done in the Startup file under ConfigureServices.

public void ConfigureServices(IServiceCollection services)
{
	services.AddTransient<IProbabilityCalculator, DiceProbabilityCalculator>();
	services.AddTransient<IMathHelper, MathHelper>();
	services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

	services.AddSwaggerGen(c =>
		{
			c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "Dice Probability API", Version = "v1" });
		});
}

In the above example an IProbabilityCalculator, and an IMathHelper are registered with particular concrete implementation. However, you’ll notice that it says AddTransient which brings us to the idea that dotnet core allows for dependency injection with control over the lifetime of the service being injected.

LifeTimes

There are three lifetimes you can specify for dependency injection: Transient, Scoped, and Singleton. Transient specifies that a new instance is created each time a request is made to the service. A good time to use this is if you want each, and every call to the service to be independent of one another. This also ensures everything gets cleaned up, and makes for a good default choice for dependency injection. Scoped will create a new instance on each connection to the service by a client. Can be used for caches that you don’t want shared across different requests or for once per request operations. Singleton lasts for the lifetime of the service. A good time to use this to for caches that can be shared across requests.

Hosted Services

There is also AddHostedService. This is useful for adding background tasks to your APIs. Hosted services are similar to singleton services in that they last for the lifetime of the service, but whereas singleton will execute when called during the lifetime of the application a hosted service is will run on startup. The hosted service will startup when the application starts, and can execute until the application ends. This can be used to created ongoing background tasks or just for simple on startup tasks.


Carl Lloyd

Written by Carl Lloyd. He spends his time playing with technology, and learning new things.