Another use case for method injections

Method injections may be the least used way to inject dependencies. The most known reason for method injection is passing this reference to another object.

Because injected methods are called right after constructor is called, you would have a fully constructed this reference to pass by.

There is another case that method injections can be useful. Let’s say you want to create Interactor classes which basically call service methods. That means all of your interactor classes will depend on your service object.

Creating a base class for interactors to get service object won’t help. Because you will still need to inject service object for all of your extended classes.

Here method injections can help for common dependencies. I mean using method injection in your base class. Our new base and products interactor classes would be like:

So our new base interactor class getting service object by method injection. Therefore we won’t need to call super method in constructors with service object!

The negative side of this approach is you can not reach base dependencies in constructor.

Like what you read? Give Ferhat Parmak a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.