Which dependency should locate in a method signature as a param and which should not? Since we have IoC container like spring, most of the dependencies could get injected through it.
For java.util.concurrent.Executor
:
public interface Executor {
void execute(Runnable command);
}
The interface could be
public interface Executor {
void execute();
}
Another case, in a traditional web application, a counter may be written like this
public interface CounterManager {
int query(User user);//user is a runtime information
}
Since spring offers request scope, User
could be injected either.
public interface Counter {
int query();//get user through Injected
}
Is there some principle or best practice to make the choice? Thanks!
Is there some principle or best practice to make the choice? Thanks!
You can use "Coding complexity rule" as a guidance. Everything that lowers code complexity in both short-term and a long-term way is good. Everything that raises it is bad.
If you start injecting everything with a use of DI container, you will end up with a DI settings file of Merriam-Webster Dictionary in size.
If something is so plain simple, that can be just passed as a parameter to a method, why to complicate things with a DI container?