3

Well, I was developing a certain project named Library Management System and I developed it using dependency injection, however my colleagues laughed at me saying that dependency injection is not a design pattern and I have to redo my project, while I spent plenty of time and effort to create my project. Is there any evidence that I can call dependency injection a design pattern?

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • 5
    _"and I have to redo my project"_ - Why? – ProgrammingLlama Apr 02 '21 at 07:41
  • 1
    If you google 'Dependency Injection', the first thing that pops up is '_Dependency Injection (DI) is a design pattern used to implement IoC_'. – Lolop Apr 02 '21 at 07:41
  • Also take a look at the information given by Stack Overflow about the [dependency-injection](https://stackoverflow.com/tags/dependency-injection/info) tag. It contains a wealth of resources at your disposal. – Steven Apr 02 '21 at 08:22
  • 1
    Why would "it's not a design pattern" be a reason to reject your implementation? The rejection might be valid if you didn't do it "the correct way™" – Hans Kesting Apr 02 '21 at 08:37
  • @Llama because senior dev is not fully satisfied about my knowledge, that was the case. – Davron Yusupov Apr 02 '21 at 10:11
  • Whether or not DI is a design pattern or not, is irrelevant. The only thing that counts is whether it is beneficial to the project at hand. Considering the many advantages it has, you should compare it with other practices and see whether it can be replaced with practices or techniques that yield the same benefits. This is an analysis that is worth to invest in together as a team. No opinions, no "I don't like this code". Just, what is the benefit, do we need this, can we achieve the same with something simpler? It does help, however, when the team uses the same resources (e.g. DIPP&P). – Steven Apr 03 '21 at 08:44

2 Answers2

13

The way that Steven van Deursen and I present it in our book Dependency Injection Principles, Practices, and Patterns, Dependency Injection is a set of patterns and principles; i.e. not a single pattern.

The most important patterns are Constructor Injection and Composition Root, while some of the important principles are the Dependency Inversion Principle and the Liskov Substitution Principle.

Some people consider Dependency Injection (DI) and Inversion of Control (IoC) the same, but based on Martin Fowler's definitions, I don't think that's the case.

Other people confuse DI with the use of DI Containers. DI Containers are optional; I personally recommend Pure DI.

Steven
  • 166,672
  • 24
  • 332
  • 435
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • Thanks for your answer! But still I have to clarify one point if it is okay. If the requirement is to use software design pattern, could I use DI principles as Constructor injection and composite root patterns? – Davron Yusupov Apr 02 '21 at 10:43
  • @DavronYusupov I would assert that those are bona-fide patterns. If someone asks in which book they are described, you know which book it is, too. I fail to see how 'use software design patterns' can ever be a *requirement*, unless this is really some kind of homework. – Mark Seemann Apr 02 '21 at 11:52
  • @DavronYusupov, Erich Gamma has mentioned in an [interview](https://www.informit.com/articles/article.aspx?p=1404056) that if the GoF book had a second edition it would include Dependency Injection as a Creational pattern. I think that answer is as authoritative as one could hope to find. – jaco0646 Apr 02 '21 at 14:00
0

Dependency injection is one of the methods you can implement the Dependency Inversion principal, its not the only way but it is a very common way of achieving it. Redoing your project won't really change the fact that you should use DI in your application. You can ask your colleagues what they think about Dependency Inversion and how they would achieve it

aaarianme
  • 282
  • 4
  • 17
  • Dependency Injection is one of the design patterns used to implement the [Dependency Inversion Principle](https://stackoverflow.com/tags/dependency-inversion/info). – jaco0646 Apr 02 '21 at 15:43