injection of a dynamic class
See original GitHub issueGiven a class known at runtime (dynamic) with dependencies in the c’tor, can we use the container to initialize such class?
E.g:
from dependencies import Injector
class Person:
def __init__(self, name: str):
pass
def get_cls():
return Person
class Container(Injector): # (1)
...
# (2)
# (3)
person = ... # (4)
- I tried to inherit from more classes, but got an exception:
_dependencies.exceptions.DependencyError: Multiple inheritance is allowed for Injector subclasses only
- I tried to use
__new__
but with no luck - I tried to register a class variable after the class definition, but got an exception:
_dependencies.exceptions.DependencyError: 'Injector' modification is not allowed
Is there away to create such dynamic behavior? Or is it inevitable becuase of the nature of the simplified and non-boilerplate API?
@proofit404 Looking forward to hear your opinion 😃
Issue Analytics
- State:
- Created a year ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Injection of a dynamic class · Issue #586 - GitHub
Given a class known at runtime (dynamic) with dependencies in the c'tor, can we use the container to initialize such class?
Read more >Dynamic dependency injection - Stack Overflow
Let plugins rely on Constructor Injection, which has the advantage that constructors statically announce each class' dependencies. Then use ...
Read more >Dynamic dependency injection by pwm - GitHub Pages
In this short article I would like to demonstrate a way to inject dependencies that are not known until runtime. There are many...
Read more >Dynamic request class injection in laravel - Laracasts
I have a custom request class which is getting longer and longer with project development. An API endpoint is expected to have multiple...
Read more >Dependency injection in dynamically create instances
I have a class that is dynamically instantiated using reflection and which need to use a service. The service depends on a repository...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Feel free to reopen this issue if you have further questions.
Have a good day 🌴 🍸
Best regards, Artem.
We could try something like this.
It still requires you to know names of possible dependencies for all classes which could be returned from
get_plugin_classes
.What do you think?