1

I have done proper dependency injection with constructors, but that makes my main method look like this:

MyModel myModel = MyModelFileReader.getModel(Config.getDefaultFile());
MyApp myApp = new MyApp(myModel, new View(new SubView(myModel), new SubView(myModel, new CustomChart(Config.getChartOptionOne(), Config.getChartOptionTwo)));
myApp.start();

All the constructing gets piled here, and it looks bad. What the proper way to do this?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • 1
    if you are using the new keyword ... what injection are you using? – Stultuske Jun 19 '20 at 11:26
  • 2
    The main method now become the composition root. If doing pure DI *(Pure DI is Dependency Injection without a DI Container.)* then one would expect to see `new` when manually initializing the object graph. Restructure the code. There is no problem here – Nkosi Jun 19 '20 at 11:38
  • @Nkosi you mean restructure my code by making the new keywords on different lines? –  Jun 19 '20 at 19:34

2 Answers2

1

To expand on the comment from @Nkosi, new has to called somewhere. Objects are instantiated by invoking their constructors, and dependency injection doesn't change that.

If you apply a DI framework, that framework will take on the responsibility of instantiating objects, so you won't necessarily see the new keywords; but they are just hidden rather than eliminated.

If you apply your own DI, then you implement a composition root yourself (which will be filled with new keywords). Your main method is exactly the right place to implement a composition root.

Note the advantage of the composition root pattern is that it consolidates object instantiation (and thereby new keywords) in a single location. If you think of creating objects as a "single responsibility" then consolidating that work into main makes your codebase more cohesive.

No pattern will eliminate the need to new up objects. By injecting dependencies through constructors, you're doing DI the right way.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
  • So doing DI the right way without a framework is basically instantiating them in the composition root? I have somehow thought that it would bad practise for one single class to manage all the dependencies of an application... –  Jun 19 '20 at 19:32
  • 1
    Managing all dependencies of an application (i.e. its object graph) in one location is exactly what the composition root pattern describes. The linked thread contains great answers about the pattern and links to even more exhaustive explanations. I highly encourage anyone who is interested in Dependency Injection to read through them. – jaco0646 Jun 19 '20 at 20:53
0

Here are some of the things you can do:

  1. Instantiate every object on the separate line. It will improve readability of your code.
  2. Use "Builder" creational design pattern to extract object construction from main method to a "Builder" class where you can manage it better. https://refactoring.guru/design-patterns/builder
  3. Use some of the IoC container/Dependency injection frameworks. Core module from Spring framework contains a good solution. Also you have "guice" framework from Google and a lot more.
lazap
  • 11
  • 3