Open jorgeas80 opened 1 year ago
Oh, I think this another issue is related. Going to check it
class FactoryFactory:
def create_factory(self, t:int) -> Factory:
return Factory(t)
and then doing
inject.get_injector().get_instance(FactoryFactory).create_factory(10)
but i think it would be nice to have the ability to write something like:
inject.get_injector().get_instance(Type[Factory])(10)
and then have the injector provide the bound class instead of it's instance.
In the specific case in this issue the problem can also be solved by moving the t:int argument to the build() method, although i don't know if that is possible in the larger scope of that project. in that case the Factory method is already performing the functionality of the "FactoryFactory" i mentioned in the previous comment.
Just noticed that it is possible if Type[Factory]
is explicitly bound. would nice to have it be the default as well.
Hi! I'm sorry for the delay.
You got it all right. If you need to pass any extra arguments to build an object, you can create and inject an intermediate builder.
You see, internally the injector treats the whole application as a static immutable graph of objects. Something like this:
All objects are typed, it means that there exists exactly one instance for each type in the graph.
Positional arguments and values (int
, etc), cannot be represented in such a graph in Python. Even though you can bind a value to an int
, typically, this is not what one should do.
So, if you need to pass different arguments/values to construct different objects, you need to use a builder/factory, as you stated above.
Hello,
I have this use case in Django: Want to bind a class to a callable that requires one or more arguments.
Here is a simplified example of what I need:
So,
Factory
requires an argument. When instantiated (call to__init__
) or even whenbuild
method is called (can change that if needed)What would be the proper way of achieving this?
Many thanks in advance