Buzzword Buster — IoC

Definition:

Inver­sion of Control (IoC) refers to the inver­sion of the flow of con­trol (the order in which indi­vid­ual state­ments, func­tion calls, etc. are exe­cut­ed) in a soft­ware. You’ll often hear the term Hol­ly­wood prin­ci­ple being men­tioned in the same breath as IoC, it sim­ply states “Don’t call us, we’ll call you” which more or less sums up the prin­ci­ples of IoC.

Purpose:

In tra­di­tion­al soft­ware design, the flow of con­trol is gov­erned by a cen­tral piece of code which often have to address mul­ti­ple con­cerns (log­ging, val­i­da­tion, etc.) and need to be aware of the imple­men­ta­tion details of its depen­den­cies. This cre­ates a very tight­ly cou­pled appli­ca­tion where changes in one com­po­nent have a rip­ple effect through­out the rest of the appli­ca­tion.

Fol­low­ing the prin­ci­ples of IoC can help you achieve:

  • decou­pling of exe­cu­tion of a task from imple­men­ta­tion (through the use of inter­faces)
  • greater sep­a­ra­tion of con­cerns (each com­po­nent only focus­es on what it’s designed to do)
  • more flex­i­bil­i­ty (imple­men­ta­tion can be eas­i­ly changed with­out any side effects on oth­er com­po­nents)
  • more testable code (enables the use of stubs/mocks in place of con­crete class­es intend­ed for pro­duc­tion)

Advantages:

  • Sim­pli­fies the build­ing of spe­cif­ic tasks.

Disadvantages:

  • Has the poten­tial to make the flow of con­trol in an appli­ca­tion more com­plex, and there­fore mak­ing it hard­er to fol­low.

Parting thoughts..

  • Mis­us­ing or abus­ing IoC can result in Mac­a­roni code.
  • IoC is not a sil­ver bul­let for all your sys­tem engi­neer­ing prob­lems, and remem­ber, “Don’t fix what’s not bro­ken”
  • When adopt­ing IoC, there is addi­tion­al train­ing needs for new join­ers to the team.
  • Design sys­tems for flex­i­bil­i­ty, which allows quick adap­ta­tion to chang­ing environment/requirementsimage
  • Avoid com­pli­cat­ing sys­tem design by try­ing to be future-proof upfront, you can’t pre­dict the future! image

Further readings:

.NetRocks show 362 — James Kovac Inverts our Con­trol!

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

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