6. Dependency Inversion Principle (DIP) Flashcards

1
Q

What does the DIP state?

A

The Dependency Inversion Principle states that high-level modules should not depend on low-level modules—both should depend on abstractions. It also goes on to say that abstractions should not depend on details, but rather details should depend on abstractions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the two kinds of dependencies in C#?

A
  1. Compile-time dependencies — references required to compile.
  2. Run-time dependencies — references required to run.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What kind of dependencies does the DIP mostly have to do with?

A

Compile-time dependencies.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What direction should your project references point if the DIP is followed?

A

Away from your low-level infrastructure code and towards high-level business abstractions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is “high-level” in the context of the DIP?

A

Constructs in our code that

  1. tend to be more abstract,
  2. relate more to the problem domain and our business rules,
  3. are generally more process-oriented than detail-oriented,
  4. and are further away from input/output (eg. forms, buttons, files, databases).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is “low-level” in the context of the DIP?

A

Constructs in our code that

  1. are closer to I/O (where is the input coming from?),
  2. are commonly referred to as “plumbing code,”
  3. interact with specific external systems and hardware.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does seperation of concerns relate to the DIP?

A

The Dependency Inversion Principle is all about applying separation of concerns — high-level concerns like your domain model or business rules should be kept separate from your low-level concerns like plumbing code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are abstractions in C#?

A

Abstractions in C# refer primarily to interfaces and abstract base classes. Essentially, the types you cannot instantiate.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What construct is generally prefered for abstractions in C# and why?

A

Interfaces over abstract base classes because interfaces don’t require object inheritance and are a bit more flexible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What do abstractions define?

A

A contract — a way of working with the type without actually specifying how that work is going to get done.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are “details” in the context of the DIP?

A

Concretions, essentially.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the DIP state?

A

The Dependency Inversion Principle states that high-level modules should not depend on low-level modules—both should depend on abstractions. It also goes on to say that abstractions should not depend on details, but rather details should depend on abstractions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the DIP state?

A

The Dependency Inversion Principle states that high-level modules should not depend on low-level modules—both should depend on abstractions. It also goes on to say that abstractions should not depend on details, but rather details should depend on abstractions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What can be said about abstractions vs details in the context of the DIP?

A

Abstractions shouldn’t be coupled to details, meaning that they shouldn’t know about the specifics of how they are implemented.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between abstractions and details?

A

Abstractions describe the “what” (eg. send a message, store a Customer record), whereas details specify the “how” (eg. send an SMTP email over port 25, serialize Customer to JSON and store it in a text file).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are some examples of low-level dependencies in an application?

A
  1. Database
  2. File System
  3. Email
  4. Web APIs
  5. Configuration details
  6. System clock
17
Q

What are hidden direct dependencies, in what form are they usually manifested, what do they cause and why?

A

The direct use of low-level concerns from the high-level concerns. Usually, these will take the form of static calls or the new keyword being used to directly create instances of low-level types. These direct dependencies cause pain because they introduce tight-coupling, are more difficult to isolate and unit-test, and promotes duplication.

18
Q

What is new?

A

new is glue. :) Using new to create dependencies glues your code to that dependency — it will tightly couple one class to another.

new isn’t bad, but just bear in mind the coupling it creates — do you need to specify the implementation or could you work with an abstraction instead?

19
Q

What does the Explicit Dependencies Principle state?

A

Classes should explicitly require their dependencies through their constructor and shouldn’t surprise clients with dependencies. Think of the dependencies as ingredients in a cooking recipe — always list them upfront.

20
Q

How does Dependency Injection relate to the DIP?

A

They go hand in hand. Following the DIP will mean that the client code injects the class’s dependencies.

21
Q

How can dependencies be injected for a class?

A

Ideally, as constructor arguments. However, they can also be injected through properties or method arguments.

22
Q

What are the advantages of using a constructor for injecting dependencies?

A
  1. Follows the Explicit Dependencies Principle
  2. Classes are never in an uninitialised state
  3. Can leverage an Inversion of Control container to construct types and their dependencies
23
Q

What is the DIP key to?

A

The Dependency Inversion Principle is the key to reducing coupling — especially coupling to specific implementation details.

24
Q

What is the first step in implementing DIP?

A

Identifying an interface using ISP and modify the code to depend on the interface rather than the concrete implementation.

25
Q

What are the key takeaways of this module?

A
  1. Most classes should depend on abstractions, not implementation details.
  2. Abstractions shouldn’t leak details.
  3. Classes should be upfront and explicit about their dependencies.
  4. Clients should inject dependencies when they create other classes.
  5. Structure your solutions to leverage dependency inversion.