stephanenicolas / toothpick

A scope tree based Dependency Injection (DI) library for Java / Kotlin / Android.
Apache License 2.0
1.12k stars 115 forks source link

RequireBinding? #427

Open davidsun opened 3 years ago

davidsun commented 3 years ago

https://google.github.io/guice/api-docs/4.2/javadoc/com/google/inject/AbstractModule.html#requireBinding-java.lang.Class- is a very nice feature in Guice. It allows the caller to be notified very early on that DI will fail. What's more helpful is if we can provide an error message such as "binding XXX isn't provided, did you forget to install XXXModule?".

davidsun commented 3 years ago

Thinking more about it: one difficulty is when we should do such checks. For Guice, there is an explicit createInjector() call. That's the moment when the bindings can be checked. However, such moment doesn't exist for Toothpick: a developer can continuously add more modules to a scope after some instances are already fetched the scope.

I'm considering performing such checks during the getInstance() call: we maintain a list of unchecked required bindings and fails the injection if a requested binding isn't satisfied.