This page looks best with JavaScript enabled

Using Dependency Injection in ASP.NET

 ·  ☕ 2 min read

Dependency Injection is a tool we can use to automatically provide objects with services they need without having to explicitly declare what is needed inside of the objects themselves. This allows us to more easily reconfigure objects to fit our needs and allows us to adopt our code more easily to the environments we’re running it in. Dependency Injection is a form of the inversion of control (IoC) software design pattern.

In our example we’re injecting an ICalculator into our application and implementing that with Calculator:

public interface ICalculator {
    int Value { get; }
    void Operation(int number);

public class Calculator : ICalculator {
    public int Value { get; private set; }
    public void Operation(int number) {
        Value += number;

There are three scopes you can use when adding services to inject into your code:

  • Transient: A new copy of your service will be constructed every time it is requested by an object.
  • Scoped: Your service will be instantiated once for the entire execution scope - usually covering a single web request. Every reference to the service within that scope will share the same copy of the service.
  • Singleton: Your service will be instantiated once - the first time it is requested - and then that instance will be reused everywhere else it is needed for the entire time your application is running.

You can learn more about using .NET’s Dependency Injection features at these links:

If you’re interested in using Dependency Injection in Spring Boot and Java you can learn more about that here:
If you’re interested in learning more about how to use Dependency Injection in your Unity games you can learn more about that here:

Join the World of Zero Discord Server:

Sam Wronski
Sam Wronski
Maker of things and professional software engineer. Lets make something awesome together!