Skip to main content

Dependency Injection (DI) vs. Inversion of Control (IOC)

The main goal of Inversion of control and Dependency Injection is to remove dependencies of an application. This makes the system more decoupled and maintainable.
First let’s try to understand IOC (Inversion of control). If you go back to old computer programming days, program flow used to run in its own control. For instance let’s consider a simple chat application flow as shown in the below flow diagram.
  1. End user sends chat message.
  2. Application waits for the message from the other end.
  3. If no message is found it goes to Step 2 or else moves to Step 4.
  4. Displays the message.
  5. User continues with his work ahead.
Now if you analyze the program flow closely, it’s sequential. The program is in control of himself. Inversion of control means the program delegates control to someone else who will drive the flow. For instance if we make the chat application event based then the flow of the program will go something as below:-
  1. End user sends chat message.
  2. User continues with his work ahead.
  3. Application listens to events. If a message arrives event is activated and message is received and displayed.
If you see the program flow it’s not sequential, its event based. So now the control is inverted. So rather than the internal program controlling the flow, events drive the program flow. Event flow approach is more flexible as their no direct invocation which leads to more flexibility.
A word of caution here, do not conclude that IOC are implemented by only events. You can delegate the control flow by callback delegates, observer pattern, events, DI (Dependency injection) and lot of other ways.
IOC (Inversion of control) is a general parent term while DI (Dependency injection) is a subset of IOC. IOC is a concept where the flow of application is inverted. So for example rather than the caller calling the method.

SomeObject.Call();

Will get replaced with an event based approach as shown below.

SomeObject.WhenEvent+=Call();
In the above code the caller is exposing an event and when that event occurs he is taking action. It’s based on the Hollywood principle “Don’t call us we will call you”. In Hollywood when artists used to give auditions the judges would say them “Don’t call us we will call you”.
The above approach makes code more flexible as the caller is not aware of the object methods and the object is not aware of caller program flow.
DI provides objects that an object needs. So rather than the dependencies construct themselves they are injected by some external means. For instance let’s say we have the following below class “Customer” who uses a “Logger” class to log errors. So rather than creating the “Logger” from within the class, you can inject the same via a constructor as shown in the below code snippet.
The biggest benefit achieved by the above approach is “Decoupling”. You can now invoke the customer object and pass any kind of “Logger” object as shown in the below code.
Customer Obj= new Customer(new EmailLogger());
Customer Obj1=new Customer(new EventViewerLogger());

Inversion of control :- It’s a generic term and implemented in several ways (events, delegates etc).
Dependency injection :- DI is a subtype of IOC and is implemented by constructor injection, setter injection or method injection.

Comments

Popular posts from this blog

Python Multi Threading

Speeding up Python code using multithreading A lot of times we end up writing code in Python which does remote requests or reads multiple files or does processing on some data. And in a lot of those cases I have seen programmers using a simple  for loop  which takes forever to finish executing. For example: import requests from time import time  url_list = [     "https://via.placeholder.com/400",     "https://via.placeholder.com/410",     "https://via.placeholder.com/420",     "https://via.placeholder.com/430",     "https://via.placeholder.com/440",     "https://via.placeholder.com/450",     "https://via.placeholder.com/460",     "https://via.placeholder.com/470",     "https://via.placeholder.com/480",     "https://via.placeholder.com/490",     "https://via.placeholder.com/500",     "https://via.placeholder.com/510...

How to encrypt connection string in web.config . In order to not expose connection string to outside world.

Protecting Connection Strings and Other Configuration Information  (C#) Introduction Configuration information for ASP.NET applications is commonly stored in an XML file named  Web.config . Over the course of these tutorials we have updated the  Web.config  a handful of times. When creating the  Northwind  Typed DataSet in the  first tutorial , for example, connection string information was automatically added to  Web.config  in the  <connectionStrings>  section. Later, in the  Master Pages and Site Navigation  tutorial, we manually updated Web.config , adding a  <pages>  element indicating that all of the ASP.NET pages in our project should use the DataWebControls  Theme. Since  Web.config  may contain sensitive data such as connection strings, it is important that the contents of Web.config  be kept safe and hidden from unauthorized viewers. By default, any HTTP re...

Books To Refer

1. C#   in Depth ,  Third   Edition : Foreword   by Eric   Lippert        By:  Jon Skeet    Publication Date:  16-SEP-2013 2. Learning jQuery - Fourth Edition     B y:  Jonathan Chaffer; Karl Swedberg     Pub. Date:  June 25, 2013      3.Beginning JSON                    By:  Ben Smith     Publication Date:  04-MAR-2014 4. Responsive Web Design with jQuery     By:  Gilberto Crespo      Pub. Date:  November 25, 2013 5. Developing Responsive Web Applications with AJAX and jQuery       By:  Sandeep Kumar Patel  P ub. Date:  July 25, 2014 6. Dependency Injection in .NET     by  Mark Seemann