Skip to main content
1 of 3
Andy
  • 10.4k
  • 4
  • 28
  • 51

Creating classes and assigning them dependencies is part of the modeling process, the fun parts, it's the reason why most of programmers enjoy programming.

Deciding, which implementation will be used and how classes are constructed is a thing which will have to be implemented somehow and is part of application configuration.

Manually writing factories is a tedious work, DI frameworks make it easier, by automatically resolving dependencies which may be resolved and requiring configuration for those which may not.

Using modern IDEs allowing you to navigate through methods and their usages, having manually written factories is quite good, because you can simply highlight a class' constructor, show its usages and it will show you all the places where and how the specific class is being constructed.

But even with DI framework, you can have a central place, such as object graph construction configuration (OGCC from now on), and if a class depends on ambiguous dependencies, you can simply look into OGCC and see how a specific class is being constructed right there.

You could object, that you personally think being able to see the usages of a specific constructor is a great plus. I would say what comes out better to you is not only subjective, but comes mostly from personal experience.

If you had always worked on projects which relied on DI frameworks, you would really know only that and when you wanted to see a configuration of a class, you would always look into OGCC and wouldn't even try to see how the constructors are used, because you would know the dependencies are all wired up automatically anyway.

Naturally, DI frameworks cannot be used for everything and every now and then you will have to write a factory method or two. A very common case is constructing a dependency during iteration over a collection, where each iteration supplies a different flag which should produce a different implementation of an otherwise common interface.

In the end it will most likely all come down to what your preferences are and what you are willing to sacrifice (either time writing factories or transparency).


###Personal experience (warning: subjective)

Speaking as a PHP developer, although I like the idea behind DI frameworks I have only ever used them once, that was when a team I was working with was supposed to deploy an application very quickly and we couldn't lose time writing factories. So we simply picked up a DI framework, configured it and it worked, somehow.

For most projects I like to have the factories manually written, because I don't like the black magic happening within DI frameworks. I was the one responsible for modeling a class and its dependencies, I was the one deciding, why the design looks the way it does, so I'll be the one who properly constructs the class (even if the class takes hard dependencies, such as concrete classes instead of interfaces).

Andy
  • 10.4k
  • 4
  • 28
  • 51