routing-controllers icon indicating copy to clipboard operation
routing-controllers copied to clipboard

Creating service per HttpRequest

Open TKul6 opened this issue 4 years ago • 1 comments

Hi,

I have routing-controllers over Express server. I inject services to the controllers, and I wonder if there is a way to create a service per HTTP request?

A more concrete example: I have a cache service that store data based on the locale of the request. In order to get the right locale I need to somehow get the the locale from the request.

I'd like to avoid manually sending the request every time I execute GET / SET / DEL.

I thought about creating a middleware , inject the service the service there and manually set the active request there. To do that however, I need some way to create a service per request to make sure there will be no collision between requests.

Do you have any idea how can I achieve this?

TKul6 avatar Jul 06 '20 11:07 TKul6

@TKul6 Services are singletons by design but even if we create a new instance per request that won’t solve your usecase, since you want a service per locale and not for every request. I think you would be better off with a factory in a before middleware and attach that to your request. Then you can use it directly without injecting the service in each controller.

attilaorosz avatar Mar 04 '22 20:03 attilaorosz