Buzzword Buster — Dependency Inversion Principle

Definition:

Depen­den­cy Inver­sion Prin­ci­ple refers to a spe­cif­ic form of decou­pling aimed at rend­ing high-lev­el mod­ules inde­pen­dent of the low-lev­el mod­ules’ imple­men­ta­tion details. Its prin­ci­ple states:

  • High-lev­el mod­ules should not depend on low-lev­el mod­ules, both should depend on abstrac­tions.
  • Abstrac­tions should not depend upon details. Details should depend upon abstrac­tions.

Depen­den­cy Inver­sion Prin­ci­ple is often talked about in con­nec­tion with Inver­sion of Con­trol or Depen­den­cy Injec­tion.

Purpose:

Even in N-tiered appli­ca­tions you can often find tight cou­pling between the dif­fer­ent lay­ers, usu­al­ly from upper to low­er lay­ers but not vice ver­sa. For exam­ple, whilst your busi­ness lay­er might be inti­mate­ly famil­iar with and depen­dent on your data access lay­er, the reverse is not true. This how­ev­er, still rep­re­sents a cou­pling prob­lem and it:

  • makes changes to the data access lay­er more dif­fi­cult as it might require changes to the busi­ness lay­er (rip­ple effect)
  • makes it hard to unit test the dif­fer­ent lay­ers in iso­la­tion

Depen­den­cy inver­sion (and decou­pling in gen­er­al) allows soft­ware archi­tects to design their sys­tems with greater flex­i­bil­i­ty by loos­en­ing up the depen­den­cies between the dif­fer­ent lay­ers of their sys­tem.

Parting thoughts…

  • Cou­pling is like radi­a­tion, there are harm­less back­ground cou­pling every­where (say, the core .Net libraries!), but expo­sure to tight cou­pling across the ser­vice boundaries/between inter­con­nect­ed mod­ules in your appli­ca­tion can be haz­ardous! With that said, with­out any cou­pling your sys­tem is as good as use­less.
  • Tight cou­pling restricts a system’s abil­i­ty to change in an indus­try where the only con­stant is change!

Further reading:

Loosen Up – Tame Your Soft­ware Depen­den­cies For More Flex­i­ble Apps (MSDN arti­cle by James Kovac)

Robert C. Martin’s arti­cle on Depen­den­cy Inver­sion Prin­ci­ple

Design Pat­tern – Inver­sion of Con­trol and Depen­den­cy Injec­tion (by Shiv­prasad Koirala)