Buzzword Buster – Dependency Inversion Principle

Definition:

Dependency Inversion Principle refers to a specific form of decoupling aimed at rending high-level modules independent of the low-level modules’ implementation details. Its principle states:

  • High-level modules should not depend on low-level modules, both should depend on abstractions.
  • Abstractions should not depend upon details. Details should depend upon abstractions.

Dependency Inversion Principle is often talked about in connection with Inversion of Control or Dependency Injection.

Purpose:

Even in N-tiered applications you can often find tight coupling between the different layers, usually from upper to lower layers but not vice versa. For example, whilst your business layer might be intimately familiar with and dependent on your data access layer, the reverse is not true. This however, still represents a coupling problem and it:

  • makes changes to the data access layer more difficult as it might require changes to the business layer (ripple effect)
  • makes it hard to unit test the different layers in isolation

Dependency inversion (and decoupling in general) allows software architects to design their systems with greater flexibility by loosening up the dependencies between the different layers of their system.

Parting thoughts…

  • Coupling is like radiation, there are harmless background coupling everywhere (say, the core .Net libraries!), but exposure to tight coupling across the service boundaries/between interconnected modules in your application can be hazardous! With that said, without any coupling your system is as good as useless.
  • Tight coupling restricts a system’s ability to change in an industry where the only constant is change!

Further reading:

Loosen Up – Tame Your Software Dependencies For More Flexible Apps (MSDN article by James Kovac)

Robert C. Martin’s article on Dependency Inversion Principle

Design Pattern – Inversion of Control and Dependency Injection (by Shivprasad Koirala)