feat: add scope() for hierarchical child scopes#39
Conversation
Adds ServiceContainer.scope(): a child container with its OWN singleton instance cache and a parent reference. Unknown services resolve from the parent (and cache there); scope-local services passed in scope(services:) are cached only in the child, so they never leak to the parent or siblings. has() and resolveByTag() also fall back to the parent. enhance() is untouched. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
@devtronic can you check this PR please |
Hi @jodinathan, |
|
@jodinathan Sorry, I haven't gotten around to it yet, and I'll be out of the office for the rest of the week. I'll take a look as soon as I get back. |
|
@jodinathan Thanks for your patience. I spent quite some time reviewing your changes, and I think the underlying issue is architectural: we're trying to solve the wrong problem.
What your changes are actually trying to achieve is support for scoped services, which is a feature that isn't implemented yet. Scoped services have been on my wishlist for this project for a long time, but they're more complex than they first appear. We need to ensure that a service never depends on another service with a shorter lifetime.
My suggestion would be:
|
Adds ServiceContainer.scope(): a child container with its OWN singleton instance cache and a parent reference. Unknown services resolve from the parent (and cache there); scope-local services passed in scope(services:) are cached only in the child, so they never leak to the parent or siblings. has() and resolveByTag() also fall back to the parent. enhance() is untouched.