Injection Scope
akutz opened this issue · 2 comments
Hi All,
This is more of a discussion than an issue, and if there's a better forum for such conversations I apologize for not using it. I did not see on mentioned in the article.
What I'd really like to do with inject
is to fork and enhance it to have injection scopes, much like Spring. Whether it's request based, thread based, or a custom scope, it all comes down to enabling some form of consistent way of sharing instances of objects with one another in some isolated boundary while there may be more of those isolated boundaries with different instances of the same types.
Ultimately I think this is made difficult to impossible? by golang's lack of threads. Since goroutines are multiplexed onto multiple OS threads, we can never truly know where code is executing, so there's no real good way to use something like a ThreadLocal to maintain any consistent ID.
The best solution of which I'm able to conceive is having multiple object graphs with a single graph at the root that provides true singletons to the other, scoped graphs. Instances of the scoped graphs would have to be passed around to any and all type instances involved in that scope in case they needed to contribute or request some type of object from the graph. At the very least there would have to be a scope entry point where a new, scoped graph is initialized using a combination of objects provided from the shared graph and new ones created from the scoped graph.
I'd love to hear your thoughts on this. The use case is where I'm building a server that can load multiple module instances. These instances can be different module types or the same module type, all configured as the user sees fit per the initialization routine. Because the modules obviously share some common dependencies (especially the instances of the same type), I'd like to make it as painless as possible to create scopes per module instance, scopes that include the modules' dependencies, be they scoped as singletons or to the module instance.
Thank you for your time!
You can achieve what you're suggesting using multiple inject.Graph
s, and using the Graph.Objects
method to seed the "child scope" with objects from the "parent scope". If it isn't terribly clear what I mean I can try to write up an example for you later this week.