Reputation: 21
I'm in front of a Node project with NestJS, as I come from C# I set up the services structure as injectable scope request
In my view, single instance does not match a class because you are hostage to parameters in functions and cannot use everything that the class can offer such as setting attributes.
Architecture that I set up is too complex to be explained in a few words so I won't go into details, what I would like to know is what would be the problems of using Injectable scope request in all services?
Example https://github.com/diegodarossi/nestjs
Upvotes: 1
Views: 804
Reputation: 21147
This is a super opinionated question and I have a suspicion that there's some premature optimization happening on your part in terms of ruling out the possibility that it's totally fine for some of your services to be Singletons.
Please give some concrete reasons of patterns where Singleton is not appropriate for your use case and we can try to provide further guidance.
In terms of "problems" to your original question if you make everything Request scoped then you'll have to pay the performance cost of resolving and instantiating dependencies on each Request instead of during application bootstrap. You'll also have overall higher memory consumption and put more pressure on garbage collection as these resources need to be released after each request.
Whether or not this is an actual problem for your application is impossible for us to judge. You should just do some performance profiling to see if this is acceptable to you
Upvotes: 1