python-dependency-injector icon indicating copy to clipboard operation
python-dependency-injector copied to clipboard

Provide injectee context to factory [aka InjectionPoint]

Open mxab opened this issue 3 years ago • 1 comments

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?

mxab avatar Jun 08 '21 12:06 mxab

Awesome!

Don't know yet how to implement it, but that does look cool. Thanks for bringing this up!

rmk135 avatar Jun 09 '21 02:06 rmk135