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
  • 206k
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
  • 26.8k
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.4k
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
12 votes
Accepted

When should interface be owned by client?

First, lets clarify what we mean when we say "interface is owned by client". It means that interface is defined by what client of the interface needs and not by what implementer provides. If we were ...
Euphoric's user avatar
  • 37.3k
10 votes

How is Inversion of Control related to Dependency Inversion

There is a great article on Martin Fowler's site that has a chapter specifically about the difference between DIP, DI and IoC. The gist of it (as copied from that site) is DI is about how one ...
JDT's user avatar
  • 6,360
9 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
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,844
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,106
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
  • 206k
7 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,442
7 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
  • 134k
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
  • 206k
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.2k
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
  • 74.6k
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,787
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
  • 11.7k
5 votes
Accepted

Protecting Invariants of Classes

You are definitely in the right here. This is pretty basic defensive programming and there are no real arguments against it. Now for the arguments : The coupling to IoC and fail-fast issue is just ...
Euphoric's user avatar
  • 37.3k
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

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
  • 206k
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

Why support cyclic dependencies at all? Are there valid use cases?

Cyclic module dependencies frequently crop up when you have a module that serves as an interface between user code and (several) implementation modules. Typically, such an interface module defines ...
cmaster - reinstate monica'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.4k
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
  • 33.7k
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.2k
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 Irene Perez's user avatar

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