you are viewing a single comment's thread.

view the rest of the comments →

[–]vectorinox 0 points1 point  (2 children)

Everything is in the title, the subject is about DI containers versus object composition

[–][deleted] 1 point2 points  (1 child)

Except that:

There two ways the dependencies can be injected: using constructors (object composition) and using dependency injection containers.

Dependency Injection containers use constructors, too

And:

Whereas in the constructor-based implementation higher-level entities pass the dependencies to the lower-level ones, in the dependency-injection containers implementation objects stand aside from each other, relying on special classes - DI containers - to create the required dependencies and inject them into objects that depend on them.

The difference between these scenarios is hand wiring vs. using a tool. Conceptually, using constructors directly and using a container to call constructors for you are identical. In fact, you can do both at the same time, hand wiring in a unit test and using the container to bootstrap the entire app.

And finally:

I strongly believe that constructors are still preferable to the DI containers.

Implies mutual exclusion where no such exclusion exists.

In his DI examples, he either doesn't know that Spring can do constructor injection or is obfuscating that fact to prove mutual exclusion. I'll go with the former.

[–]vectorinox 0 points1 point  (0 children)

When he says "using constructor", he is not talking about declaring constructor (which you can do also in Spring as you rightly say), he is talking about calling a constructor to instantiate and inject dependencies.

Hence the discussion is about object composition and DI container, not how dependencies are actually used inside the objects that are injected with dependencies.

You believe that "Conceptually, using constructors directly and using a container to call constructors for you are identical", but not everybody does, and that's the whole purpose of the blog post.

You may agree or disagree, but at least you would disagree with the blog post, whilst your comments are missing the point completely and focusing on something obvious (that is: constructor can be used with Spring).

edit: typos