When to use following Transient, scoped and singleton
Asked Answered
H

2

7

I read some articles about this and I get to know how to use Transient, Scoped, and Singleton but I am confused when to use one of these.

What I am understood:

Singleton: In situation when you need to store number of employees then you can create singleton cause every time you create new employee then it will increment the number so in that situation you need singleton.

Scoped: For example you are playing game in which number of life is 5 and then you need to decrease the number when player's game over. And in every new time you need new instance because every new time you need number of life is 5.

Transient: when to use Transient??

Please correct me if I am wrong. And give the better example of all of them if possible.

Hymn answered 22/4, 2021 at 4:26 Comment(1)
Does this answer your question? AddTransient, AddScoped and AddSingleton Services DifferencesPlato
R
20

As far as I know, the Singleton is normally used for a global single instance. For example, you will have an image store service you could have a service to load images from a given location and keeps them in memory for future use.

A scoped lifetime indicates that services are created once per client request. Normally we will use this for sql connection. It means it will create and dispose the sql connection per request.

A transient lifetime services are created each time they're requested from the service container. For example, during one request you use httpclient service to call other web api request multiple times, but the web api endpoint is different. At that time you will register the httpclient service as transient. That means each time when you call the httpclient service it will create a new httpclient to send the request not used the same one .

Regolith answered 22/4, 2021 at 5:35 Comment(6)
Thank you for your explanation.. i got scoped and transient but please you can explain singleton with different example.. thank you in advanceHymn
singleton is used for normal service. For example. some memory cache, you may read some image or data from disk into redis, this action will not change during application running.Regolith
For a class that has got utility function for example MeterToKm(int mtr) - should this be scoped or singleton?Messeigneurs
A singleton isn't a good choice for sqlconnection ? we need to create the connection once right ?Millhon
@Messeigneurs - In that situation, I would just make it a static helper classKodak
@Millhon A year later, but for anybody who still has that question: Anything that connects to a DB could just stop at any point, e.g. because it crashed. That's why DBs (should definitely) have a timeout for inactive connections. Which means singleton scope will not work. For this reason, ORMs usually provide a lightweight connection object that you can just create for every request without problem.Sweepstake
F
2

Note, Microsoft provides the recommendations here and here.

When designing services for dependency injection:

  • Avoid stateful, static classes and members. Avoid creating global state by designing apps to use singleton services instead.
  • Avoid direct instantiation of dependent classes within services. Direct instantiation couples the code to a particular implementation.
  • Make services small, well-factored, and easily tested.
Fleer answered 2/2, 2022 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.