Skip to main content
33 votes
Accepted

Is dependency inversion principle necessary?

(Disclaimer: I understand this question as "applying the Dependency Inversion Principle by injecting objects through interfaces into other object's methods", a.k.a "Dependency Injection&...
Doc Brown's user avatar
  • 214k
17 votes

Coding to Interfaces vs Abstract Inheritance

Am I misunderstanding the concept of "code to an interface and do not use inheritance"? Yes you are misunderstanding that, because you seem to be conflating two different principles. On the one hand,...
Bart van Ingen Schenau's user avatar
15 votes
Accepted

Is there really such a thing as the onion architecture?

It seems to me that most 'new' things in software architecture are old ideas refurbished and combined with new technologies or other ideas. Often these are incremental. In this case, the change that ...
JimmyJames's user avatar
  • 28.9k
14 votes

I'm struggling to see how dependency inversion doesn't lead to tighter coupling in lower level modules and less reuseability

I fail to see how dependency inversion is useful at all. That's because you've failed to use it for anything yet. You're bemoaning the fact that DIP is work, adds complexity, and does nothing for you....
candied_orange's user avatar
14 votes

Is dependency inversion principle necessary?

At its core DI means streamlining your dependencies. Rather than making A, B and C aware of each other (to know each other's type) you introduce another thing D. You then make A. B and C know D but ...
Martin Maat's user avatar
  • 18.5k
13 votes
Accepted

How to avoid bidirectional class and module dependencies

There are two main ways to deal with circular dependencies: Hide it with interfaces Add an intermediary object In this particular case I would recommend the second option. Your module would have ...
Berin Loritsch's user avatar
10 votes

Is it okay to use Dependency injection only because of unit testing?

It is absolutely okay to use a programming method only to support proper testing. Test suites are just as important as business code - the fact that you don't ship them to the customer is not ...
Kilian Foth's user avatar
9 votes

Depend on DDD Entities or Interfaces?

Well, well, let's get something that is bothering me out of the way first... if (Company == 1) { //This is how code should NEVER look like } OK, now that I took it out of me it, let's get to the ...
Vector Zita's user avatar
  • 2,482
8 votes

How to avoid bidirectional class and module dependencies

Thinking of an untyped code, if the Response gets the Request object injected, but doesn't create one on it's own, it doesn't depend on the Request class. It depends just on it's interface (which may ...
Frax's user avatar
  • 1,864
8 votes
Accepted

How do I implement Dependency Inversion in C?

Applying SOLID is not always appropriate. Dependency inversion implies some indirection, and that typically means overhead. This kind of overhead is unlikely to be appropriate in memory-constrained ...
amon's user avatar
  • 135k
7 votes

Why do I need dependency injection and the depencency inversion principal in my case?

Main goal of Dependency Inversion Principle is change direction of dependency. In your example UI layer depend on Presentation layer and Presentation layer depend on Business logic layer. So if you ...
Fabio's user avatar
  • 3,156
7 votes

Help with dependency inversion

DI is not an end in itself, it is a means to an end. I have my doubts about the usefulness of applying DI to this binary tree with something else than the "canonical" binary node, or even a necessity ...
Doc Brown's user avatar
  • 214k
7 votes

Dependency Inversion principle and high-level module reuse

Low level modules are "low level" because they have no dependencies, or no relevant dependencies. Very often, they can be easily reused in different contexts without introducing any separate,...
Doc Brown's user avatar
  • 214k
6 votes
Accepted

Is Implementation class exposure fine in Dependency Inversion

This is an area where the type system of a language doesn't give you many safeguards. Without making the class a package level case in the case of Java, or an internal class in the case of C#, you can'...
Greg Burghardt's user avatar
6 votes

Is it good design to have one constructor that supplies a "default" concrete class to another that takes an abstraction?

Is there a name for this pattern? Let's start with this question as the its name gives a succinct answer to your more general question. This is known as the Bastard Injection (Anti) Pattern. ...
David Arno's user avatar
  • 39.5k
6 votes

If it is a bad practice to use an interface if only one class will implement it, what is the purpose of IoC container?

IoC container advertise themselves with code snippets like: container.Register<IEmail,Gmail>() which suggest you always need an interface and that its a 1 to 1 relationship. But this is not ...
Ewan's user avatar
  • 79.9k
5 votes

How Dependency inversion is an extension of OCP?

The DIP requires your classes to depend on abstractions. Because you can always provide different implementations of these abstract dependencies, they create natural extension points that allow you to ...
doubleYou's user avatar
  • 2,847
5 votes

How to avoid bidirectional class and module dependencies

The solution below allows compiling the Python code while avoiding the creation of a third module which reduces readability IMO. Python type hinting request.IRequest enabled code completion on IDEs (...
Elrond's user avatar
  • 301
5 votes

Clarification on the Dependency Inversion Principle

Who is going to determine what classes (that implement SwitchableDevice) need to be called? Who tells Button what devices he need to turn on/off? How do you tell an object that uses something ...
Ben Cottrell's user avatar
  • 12.1k
5 votes

Do we really need interface classes for the dependency inversion principle?

The DIP is not an end in itself, it is a means to an end. Let us, for example, assume you use the DIP for unit testing. If you want to be able to test Gripper in isolation, without the "real" ...
Doc Brown's user avatar
  • 214k
5 votes
Accepted

Wrapping a legacy project in a test framework

You've got the basic pattern. Define interfaces for the classes that communicate with out-of-process resources, like databases and file systems. Then have the "real" class implement the interface. ...
Greg Burghardt's user avatar
5 votes
Accepted

Onion architecture: Dependency Inversion Principle within the Service layer

You apply DIP when crossing a significant boundary. What's on either side doesn't matter. What matters is having a good reason to keep what's on one side from having a source code dependency on the ...
candied_orange's user avatar
4 votes
Accepted

Testable design for a class that can only be instantiated through a static method

If you are using c#, you can use this "friend" approach: Add another (possibly parameterless) constructor to the class solely for your test project. "Hide" the constructor from other developers by ...
John Wu's user avatar
  • 26.9k
4 votes

Coding to Interfaces vs Abstract Inheritance

To add to the other good answers: We should not automatically create class hierarchy, especially in cases when a single class will do. Many things are better modeled thru attributes in the general ...
Erik Eidt's user avatar
  • 34.4k
4 votes

Dependency Injection and DI Container

If you inject a "ServiceFactory", which contains all the dependencies for all parts of your code, into all parts of your code, then you are using dependency injection, but not dependency inversion. ...
David Arno's user avatar
  • 39.5k
4 votes
Accepted

Help with dependency inversion

Let's lay out two things. The first is that it's good to hide implementation details from the user of your code, but it's not necessary to abstract them away entirely. In other words, it's fine to use ...
Alecto's user avatar
  • 533
4 votes

Should a function implementer or function caller handle immutability

if you return an object of MyType, I (personally) would expect it to be a new object I guess it's because I would expect to modify the parameter by using a ref call. Also it would look weird: var ...
HankTheTank's user avatar
4 votes
Accepted

3 layer architecture In DI based software

The entry point of the application (the UI project) must setup the DI container, providing implementations of all the dependencies. That's why it must have direct connection (reference) to all the ...
Rumen Georgiev's user avatar
4 votes

Clean Architecture and Persistence Annotations

You basically answer your own question. Strictly following the "clean architecture" dogma leads to "many new classes and boilerplate code (...) this option may do more harm than good." If ...
JacquesB's user avatar
  • 61k

Only top scored, non community-wiki answers of a minimum length are eligible