3

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.

Dale K
  • 25,246
  • 15
  • 42
  • 71
ARJUN PATEL
  • 67
  • 1
  • 1
  • 5
  • 2
    Does this answer your question? [AddTransient, AddScoped and AddSingleton Services Differences](https://stackoverflow.com/questions/38138100/addtransient-addscoped-and-addsingleton-services-differences) – Ramil Aliyev 007 Apr 22 '21 at 06:04

2 Answers2

13

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 .

Brando Zhang
  • 22,586
  • 6
  • 37
  • 65
  • Thank you for your explanation.. i got scoped and transient but please you can explain singleton with different example.. thank you in advance – ARJUN PATEL Apr 22 '21 at 06:43
  • 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. – Brando Zhang Apr 22 '21 at 06:45
  • For a class that has got utility function for example MeterToKm(int mtr) - should this be scoped or singleton? – variable Jan 12 '22 at 18:20
  • 1
    A singleton isn't a good choice for sqlconnection ? we need to create the connection once right ? – Neyoh Feb 17 '22 at 17:28
  • @variable - In that situation, I would just make it a static helper class – scottdavidwalker Sep 23 '22 at 06:34
  • @Neyoh 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. – Raphael Schmitz Apr 01 '23 at 09:50
1

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.