Configure inject in a library

8 views
Skip to first unread message

Juan Pablo Crossley

unread,
Jul 27, 2017, 6:47:07 AM7/27/17
to python-inject
Hello,

I've some sub projects that handle different things in a bigger project, everything works with your inject library (which is I love, so thank you for your work), but now I have a question.

Let's say that I have a library "libraryA", and a "main", the libraryA uses inject to inject all it's dependencies, now main has to call this, but also uses inject, how can the libraryA configure its dependencies whilst main configure its dependencies?

currently I'm doing this:

libraryA
+- classLA injects classLB

main
+ classMainA injects classMainB

so the configure in main is something like

inject.clear_and_configure(lambda binder: binder \
           .bind(ClassMainB, oClassMainB),
           .bind(ClassLB, oClassLB)
          )

but clearly this is wrong as main will need to know the internals of libraryA, I would want that libraryA configure its dependencies and main only its. I see that the examples contain this:
# Create an optional configuration.
def my_config(binder):
    binder.install(my_config2)  # Add bindings from another config.
    binder.bind(Cache, RedisCache('localhost:1234'))

# Configure a shared injector.
inject.configure(my_config)

but this looks more to modules than libraries, as this solution will still need to know the internals of the other packages,

Thanks for your help,


Ivan Korobkov

unread,
Jul 27, 2017, 7:00:10 AM7/27/17
to python...@googlegroups.com, Juan Pablo Crossley
Hi!

I think “Configuration” is a wrong term, instead I should have used “Module”. 
Each library, service, etc can export their own dependency configurations as public
modules and you can merge them into one in you main class.

For example, Angular 4 and other DI frameworks in other languages use this approach.

> but this looks more to modules than libraries, as this solution will still need to know the internals of the other packages,
Well, you still use a library, i.e. your code already knows about it. Just make its DI Module a part of its public API.


--
You received this message because you are subscribed to the Google Groups "python-inject" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-injec...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Juan Pablo Crossley

unread,
Jul 27, 2017, 7:06:34 AM7/27/17
to Ivan Korobkov, python...@googlegroups.com

Thanks for your prompt reply,

Sounds like a good approach, thanks I'll expose a configuration element that can be used in its clients,

Cheers

Reply all
Reply to author
Forward
0 new messages