Java here. I have always used Spring DI (for Spring projects) or Guice (for non-Spring projects) for dependency injection, and have always loved them.
I recently took a job where they do 100% "DIY DI". That is, every project's main/driver class has an init()
method that creates all of their objects/factories for them:
public class SomeApp {
private DatabaseService databaseService;
public static void main(String[] args) {
SomeApp someApp = new SomeApp();
someApp.run();
}
private void run() {
init();
// Now do some stuff (whatever SomeApp does).
}
private void init() {
SomeAppConfig someAppConfig = readFromFileSystemSomehow();
databaseService = new DatabaseServiceImpl(someAppConfig.getDatabaseInfo())
// ...etc.
}
}
I feel like there is nothing necessarily wrong with this DIY DI, however I feel like Guice and Spring DI exist for a reason, implement DI best practices, and handle a variety of situations better than what any homegrown solution could handle.
I would like to try and propose using Guice/Spring DI to this team, but before I do I want to either:
- Make sure that I have solid/concrete reasons for why these open source projects exist and do DI better than a DIY solution; or
- Perhaps amend my outlook on DIY DI if it is in fact perfectly fine and these other libs (Guice/Spring DI/Weld/etc.) aren't really necessary for proper DI practices
So I ask: What is lost by forgoing on Guice/Spring DI/etc. and using a DIY DI solution? What is the opportunity cost?