Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toothpick bind modules that depends on each other

I am trying out the toothpick DI library and I seem to miss something crucial.

I created the following test project on github where I tried to make a smallest possible use case for my understanding issue.

There you will find the ApplicationModule where I try to inject everything I need in my "root" module. As you can see there, I have twice scope.installModules(module) because my PlainPojo depends on the Application instance. If I do not do the first installModule call, I have no application instance in the scope. I could, as mentioned in the comment, pass the application instance as a parameter but I thought that I can remove them when I use DI?

The second class that causes troubles is the SimpleTest class. If I do not call the inject() method in the constructor, the PlainPojo member will not be injected. I also tried to use a third time the installModules() in the ApplicationModule after binding the PlainPojo but that does not help.

Am I so wrong to assume that an @Inject on a member is enough to automatically inject it when it is available in the scope and that the order of binding is enough to make previous bindings available without installing in between (like done in ApplicationModule)?

If I can provide anything more to make my issue(s) understandable please leave a comment.

Thanks in advance!

like image 501
WarrenFaith Avatar asked Oct 28 '22 20:10

WarrenFaith


1 Answers

Toothpick can only inject dependencies automatically when it creates the instance of the depended object itself (e.g it is annotated with @Singleton or there is a direct binding for the class).

Otherwise you have to manually call Toothpick.inject, just like you do in SimpleTest. There is no way for TP to know when you call the constructor of PlainPojo.

You could just use

module.bind(PlainPojo.class);
module.bind(SimpleTest.class);

and maybe scope them as singletons if needed.

like image 109
MatF Avatar answered Nov 02 '22 14:11

MatF