0

I'm just learning about dependency injection. Unfortunately, I don't have the luxury and time to learn a DI framework right now so I am attempting to do DI through some simple means.

Say I have an object A, that requires a class B, and class B requires class C. When I go to create Object A, is it best practice to create the object like

A instanceOfA = new A(new B(new C ()))

My question is Is there a better way to do this?

8
  • 1
    End result will be same, just do this in more readable and comprehensible manner for yourself and teammates.
    – Fabio
    Jun 26, 2019 at 21:41
  • 4
    What does "better" mean for you in this specific case?
    – Laiv
    Jun 27, 2019 at 6:31
  • 2
    The better way is a DI framework.
    – Omegastick
    Jun 27, 2019 at 6:57
  • 3
    @Omegastick DI frameworks are in no way better than pure DI. They just make developments more productive and some make the code cleaner at expenses of comprehension (it's not obvious how and when the dependency graph is solved). Moreover, some DI frameworks are not declarative, what basically force the developer to solve the dependency graph by instantiating and invoking the framework's API, what doesn't make the solution better than pure DI in any way,
    – Laiv
    Jun 27, 2019 at 7:09
  • 3
    @Omegastick, from my experience, until someone understands why DI frameworks are not better than pure DI, they should only use pure DI. Once they have a good grasp of pure DI, they are then in a position to weigh up the pros and cons that Laiv mentions and decide whether to switch to a framework. I personally prefer comprehension over simplicity and so generally still with pure DI, but others reach the alternative, equally valid conclusion and adopt a framework.
    – David Arno
    Jun 27, 2019 at 7:23

1 Answer 1

4

The better pattern is to remember that C also needs to be followed by a pair of parenthesis.

But seriously, yes this is called pure DI. You don't need a framework. It's just good old fashioned reference passing.

My basic pattern is to get inside a method that is called once (usually main), build an object graph, hold on to one object in the graph, and call it's start method to start the whole thing running.

In your case that would look like instanceOfA.start();. That gets you out of the static main and into some actual OOP code. Keep construction code and behavior code as separate as you can.

You can do it all again for more transitory objects like timestamps and such that need constructing later but this first bit should get your long lived objects up and going.

2
  • Cool thanks. What do you mean though to say "hold one to one object in the graph and call it's start method?"
    – alex
    Jun 26, 2019 at 23:22
  • 1
    In your case that would look like instanceOfA.start();. That gets you out of the static main and into some actual OOP code. Keep construction code and behavior code as separate as you can. Jun 26, 2019 at 23:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.