79292470

Date: 2024-12-18 21:06:10
Score: 1
Natty:
Report link

Why I should use AddScoped() for my repositories or services? Why not AddSingleton()? I know about differences between them, but dont understand why I shouldn't use singleton instances to avoid creating new objects for each requests. Can you explain it (preferably with examples :) )?

You shouldn't in my opinion. Who said you should? What triggered this question?

I create my service to work like input -> service.do_something() -> output, without storing any property in the service itself (kind of stateless apart for COMMON values, like stuff passed by the configuration). I think storig properties in a service to pass values between "steps" is exactly what them are NOT supposed to do (different story for helper class or any other auxiliary utils).
Less than service the repositorries. They are just pass-trought "service" to interact with the database.

In the end the application grows, at you will have services that needs some initial "work to do"... that is repeated millions of time while it can be done only once.
If a service will leave some memory leak (an d that will happen) you will notice your application to use more and more memory, and start from low memory usage on a new deployment.
"Scoped" services are disposed... but a mmeory leak is exactly some memory NOT released when the dispose of the service happen. So, it is possible that for each instantiation/disposiktion (= "scoped") the used memory of the app grows.

Reasons:
  • RegEx Blacklisted phrase (2.5): Can you explain
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): Why I
  • High reputation (-1):
Posted by: Alex 75