ivankorobkov / python-inject

Python dependency injection
Apache License 2.0
694 stars 79 forks source link

Configure with modules #10

Closed ivankorobkov closed 5 years ago

ivankorobkov commented 10 years ago

Something like:

inject.configure_with_module(module1, module2, module3, ...)
Enforcer commented 6 years ago

I was thinking about implementing this feature. Do you think that requiring module1, module2, module3 to be imported module objects with configure_inject(binder) function inside is a good idea? In case there is no such a function in any of modules an exception would be raised.

# module1
def configure_inject(binder) -> None:
    pass
# module2
# nothing here yet - passing module2 to `configure_inject` would raise an exception about missing configure_inject

This convention is somehow inspired by Pyramid's includeme functions

ivankorobkov commented 6 years ago

Hi!

Well, I like the way I did it in Go DI: https://github.com/ivankorobkov/go-di The main idea is a module imports other modules and defines what types it provides. For example:

def db_module(m):
  m.import(config_module)
  m.add(DB)

def cache_module(m):
  m.import(config_module)
  m.add(Cache)

def hello_module(m):
  m.import(db_module)
  m.import(cache_module)
  m.add(HelloService)

def __main__():
  inject.configure_with_modules(config_module, hello_module)

So, we get a manageable graph of modules with imports, dependencies and providers (and can check it). It works pretty well in Go. Also, it is somewhat similar to Angular DI. Though, it is a bit complex.

ivankorobkov commented 5 years ago

Obsolete