What is Inversion of Control?

What is Inversion of Control?

Inversion of Control (IoC) and Dependency Injection (DI) are two related practices in software development which are known to lead to higher testability and maintainability of software products. these patterns are said to be based on a principle, which states: "don't call us, we'll call you".

If you follow these simple two steps, you have done inversion of control:
  1. Separate what-to-do part from when-to-do part.
  2. Ensure that when part knows as little as possible about what part; and vice versa.
There are several techniques possible for each of these steps based on the technology/language you are using for your implementation.The inversion part of the Inversion of Control (IoC) is the confusing thing; because inversion is the relative term. The best way to understand IoC is to forget about that word!

Inversion of control is a practical way to reduce code duplication, and if you find yourself copying an entire method and only changing a small piece of the code, you can consider tackling it with inversion of control. Inversion of control is made easy in many languages through the concept of delegates, interfaces, or even raw function pointers.

Examples
  • Event Handling. Event Handlers (what-to-do part) while Raising Events (when-to-do part)
  • Interfaces. Component client - an interface (when-to-do part) while Component Interface implementation (what-to-do part)
  • Template method design pattern. template method when-to-do part -- primitive subclass implementation what-to-do part

Comments

Popular posts from this blog

Data Bound Controls in ASP.Net - Part 4 (FormView and DetailsView controls)

ASP.net: HttpHandlers

The Clickjacking attack and X-Frame-Options