[V6] Singleton per-request? #4498
-
Is there a way to bind singleton per-request? // scope.ts
export default class Scope {
value = 0
}
// provider.ts
this.app.container.singleton('scope', async () => {
return new Scope()
})
// routes.ts
// first call this
router.get('/', async () => {
const scope = await app.container.make('scope')
scope.value = 5
})
// then call localhost/test2
router.get('/test2', async () => {
const scope = await app.container.make('scope')
console.log(scope.value) // curentlly it will print 5 but I want to have 0
})
Is there a way we can make singleton per-request through IoC container? Update: I know how to do that using middleware, I'm just wondering if there is a way to do like in the nest |
Beta Was this translation helpful? Give feedback.
Answered by
RomainLanz
Apr 8, 2024
Replies: 1 comment 5 replies
-
Middleware is the way to go. As per me, its a bit more explicit and easy to reason about, since you can clearly understand the flow how and when a binding is registered in the container. Lemme know if you have any specific struggles with the middleware approach? |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can bind an injectable value in your middleware.
Then: