thiagobustamante / typescript-ioc

A Lightweight annotation-based dependency injection container for typescript.
MIT License
524 stars 64 forks source link

Example of how to use scopes? #48

Closed cosmoKenney closed 4 years ago

cosmoKenney commented 5 years ago

I'm using typescript-ioc in a SPA app. And one of my parent views has data that needs be shown by a child view. What I want to achieve is during the lifetime of the parent view, all child views get the same instance of this data object. But when the parent view is destroyed I need the data object to be removed from DI. That way when the user again navigates to the parent view, there will be a new/fresh instance of the data object added to DI for the children again. And any old instance of the data object just need to go away.

I am thinking that scopes are what I want here, but I'm not sure how to make it work. Can you point me to an example?

thiagobustamante commented 4 years ago

It is not supported in the current version, but I am working to support it in version 3.

The current version only offer Local and Singleton scopes

thiagobustamante commented 4 years ago

Hi @cosmoKenney, I think that the new @InRequestScope can solve your problem. You can configure all the dependents components in requestScope and inject them in the parent view. It will be released in version 3. But the code is already commited in the repository and you can try it directly through the git source, if you want to test it right now

cosmoKenney commented 4 years ago

Thanks for such a great library! We can close this as I just decided to cache the data instead.