python-dependency-injector
python-dependency-injector copied to clipboard
Provide injectee context to factory [aka InjectionPoint]
Hi,
Would be cool if there would be a provider method that would be provided with the target information the provided element is injected to (...sorry for that funny sentence.. )
For example something like this:
@attr.s(auto_attribs=True)
class MyService:
log: Logger
def get_logger(target) -> Logger: # target could be the class for example
return logging.getLogger(target.__name__)
class Container(DC):
logger_factory = providers.SomeSpecialInjecteeAwareFactory(get_logger)
service = providers.Singleton(MyService, log = logger_factory)
The idea relates to Spring's InjectionPoint https://fatihdirlikli.wordpress.com/2016/11/22/logger-injection-with-injectionpoint/
Does this make sense?
Awesome!
Don't know yet how to implement it, but that does look cool. Thanks for bringing this up!