Dependency Injection & Inversion of Controls (IOC) in C# – Part 1

Recently i was going through the articles related to the Design patterns. So I came across the term Inversion of controls and the dependency injection. Though these are not considered to be design patterns, like factory, abstract factory etc., but they also allow us to achieve the goals of loosely coupled system and re-usable code, like the design patterns. The main idea behind the concept of using this concept is to develop a loosely coupled system.

What is Loosely coupled system then ?

I know you guys will be aware of this term but still to give an idea about it, a loosely coupled system is the one in which the components are designed in such a manner so that adding or removing any component does not affect the functioning or the overall architecture of the existing system. So this requires the development in a very planned manner using the design patterns or concepts like these.

So moving on to our system, let me give you an example of a tightly coupled system. Look at the image below :


As you can see here, we have two classes ClassA & ClassB. ClassA is calling the ClassB using its instance. This is known as the dependency or a tightly coupled system where one system/component is dependent on the another.

Here, ClassB is the dependency and ClassA is the dependent. Our job will be to remove this task of creating the instance of ClassB to an intermediate so that both the components are loosely coupled. This is done using the abstractions so that both the components interact though the abstraction concept rather then the concrete implementation. This is known as the concept of Inversion of Controls, where we free the dependent from the responsibility to managing the dependency inside itself. It is like we are outsourcing the responsibility of managing the dependency to some external component, so that both the dependents and dependency are loosely coupled.

The process we will be using to implement the inversion of control, is known as Dependency injection, which implies, we are injecting the dependency to the dependent using another component.

So to summarize, Inversion of controls(IOC) is a concept & Dependency injection is just another way to implement it. To implement dependency injection, we have 3 different ways. These are :

So let’s start with our first approach – Constructor dependency injection.

About Jasminder

.Net developer and blogger by profession, keen to learn new technologies, love nature, music and cricket.
This entry was posted in Design and Architecture and tagged . Bookmark the permalink.

I have a suggestion..

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s