Hi đź‘‹
I've been asked "What are Transient, Singleton and Scoped?" many times and i wanted to explain these design patterns which are being used with dependency injection with C# sample use cases.
First let's see what these terms are 👇
Transient:
Whenever something is needed, it is created, and when it is no longer needed, it is discarded. It is comparable to purchasing a disposable item that you will only use once before discarding. This can be helpful in software when you don't want to constantly keep a large number of objects in memory.Singleton:
In lifetime of an application, only one instance of a specific object is created and used according to this design pattern. It's comparable to owning a single vehicle that you use consistently as opposed to getting a new vehicle each time you need to travel. When an object needs to be shared by several components of an application, this can be helpful in the context of software.Scoped:
This is a design pattern in which objects are created and used only within a specific "scope" or context, such as a particular task or operation. It's similar to having tools that you use for a specific project but that you don't need afterwards. This can be helpful in the context of software when you want to maintain object separation and organization while avoiding the reuse of objects across various application components.
Let's see them in code example 👨‍💻
Here are examples of how to register Transient, Singleton and Scoped services in a .NET Core application:
Assume that we have an interface called IMyService
and a concrete implementation of this interface called MyService
.
public interface IMyService
{
void DoSomething();
}
public class MyService : IMyService
{
public void DoSomething()
{
Console.WriteLine("Hello, world!");
}
}
- Transient objects are created every time they are needed, and then discarded when they're no longer needed.
services.AddTransient<IMyService, MyService>();
This code tells the dependency injection framework to create a new instance of MyService
every time an IMyService
object is requested.
- Singleton objects are created once and then reused throughout the lifetime of an application.
services.AddSingleton<IMyService, MyService>();
This code tells the dependency injection framework to create a single instance of MyService
that will be used throughout the lifetime of the application.
- Scoped objects are created and used within a particular scope, such as a specific web request.
services.AddScoped<IMyService, MyService>();
This code tells the dependency injection framework to create a single instance of MyService
for each web request. The same instance of MyService
will be used for all objects that are created during the processing of the request, and then discarded when the request is complete.
Note that in all of these examples, the MyService
class implements the IMyService
interface, so that it can be registered with the dependency injection framework and easily swapped out for a different implementation if necessary.
I hope you understood the most common patterns for dependency injection clearly.
Thank you for reading and please feel free to reach me out if you have any questions 🤗
Top comments (0)