injector
injector copied to clipboard
Implementation of simple InstanceScope
Am aware this is more a question, rather than a bug, but perhaps an indication that an example scope implementation (other that an example of seemingly re-implementing NoScope would be helpful for those new to injector.
Anyway, I am trying to implement a simple custom scope which provides the same instance of a given class to any sub-objects in that class that might require a reference to it.
e,g
class A:
@inject
def __init__(self, b: B, c: C):
self.b = b
self.c = c
class B:
@inject
def __init__(self, c: C):
self.c =c
class C:
pass
I essentially need a scope that will ensure that the instance of C that is provided at injection of both A and B is the same one for any given instance of A.
It feels like I should be able to do this by creating a custom scope that is applied on A, but the scope documentation is great for everything except for an idea of how you might go about implementing the scoping itself, I feel like I'm missing something obvious somewhere!
Currently I am handling this by using AssistedBuilder instances in a factory class to provide those C instance, but that feels like the wrong approach...
Yeah, the documentation can be improved in this regard. About your case – can you provide some pseudo-code of what you're trying to achieve?
Sorry, hit enter too early - edited the OP to include an example.
I don't think there's a way to do this at the moment. Possibly the scope system could be improved to handle this, but it'd require some redesign I believe.