What is the difference between transient, singleton and scoped in C#?

Photo by Shubham Dhage on Unsplash

What is the difference between transient, singleton and scoped in C#?

Michael Asaad's photo
Michael Asaad
ยทNov 6, 2022ยท

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Table of contents

  • Definition
  • Initial Setup
  • Implement the interfaces
  • Create Services
  • Add To Startup.cs
  • Conclusion

Definition

Transient, scoped and singleton are different declaring and configuring the lifetime of objects in dependency injection.

When you use transient it means that a new instance of the object is provided to every controller and service it is used in. Scoped objects are different across different requests but the same within the same request. Singleton on the other hand uses the same object for every request.

Let's get into a few examples.

Initial Setup

Let's say we have the following interfaces:

public interface IBaseInterface
{
  int DemoId { get; set; }
}

public interface ITransientInterface : IBaseInterface 
{
}

public interface IScopedInterface : IBaseInterface 
{
}

public interface ISingletonInterface : IBaseInterface 
{
}

Implement the interfaces

We have three interfaces called transient, scoped and signleton that are all inheriting from the base interface. These interfaces are implemented in our demo class below:

public class DemoClass : ITransientInterface, IScopedInterface, ISingletonInterface 
{
  public int DemoId { get; set; }
}

We can then use dependency injection in a service with these interfaces. For example:

Create Services

public interfaces IDemoService 
{
}

public class DemoService : IDemoService
{
    private readonly ITransientInterface _transientInterface;
    private readonly IScopedInterface _scopedInterface;
    private readonly ISingletonInterface _singletonInterface;

    public DemoService(ITransientInterface transientInterface, IScopedInterface scopedInterface, ISingletonInterface singletonInterface)     {
transientInterface = _transientInterface;
scopedInterface = _scopedInterface;
singletonInterface = _singletonInterface;
}

....

}

Add To Startup.cs

Once we have those in place we can register the interfaces in the startup.cs within the ConfigureServices method:

services.AddTransient<ITransientInterface, DemoClass>()
            .AddScoped<IScopedInterface, DemoClass>()
            .AddSingleton<ISingletonInterface, DemoClass>()
            .AddTransient<IDemoService, DemoService>()

Conclusion

So now you know how to use transient, scopped and singleton. If you want a new instance of your object to be created with each request then use transient. If you want different object instance across each request then use scoped and finally if you want to use the same object instance for the lifetime of the app then use singleton.

If you found this helpful then please comment below.

Until next time, happy coding! ๐Ÿ™‹โ€โ™‚๏ธ

Did you find this article valuable?

Support Michael Asaad by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
ย 
Share this