'When to use following Transient, scoped and singleton

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.



Solution 1:[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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user11137380