.netC#.net

Inversion of Control, Dependency Injection and Service Locator in C#

In this article, we will learn:

 

  • What is Dependency Inversion Principle (DIP)

  • What is Inversion of Control (IOC)

  • What is  Service Locator?

  • What is Dependency Injection?

  • Difference between DIP and IOC (DIP vs IoC)

  • Difference between Dependency Injection and Service Locator (DI vs SL)

 

Many developers get confused with the term Dependency Injection. mainly on: What is Dependency Injection ? Why is it required? what is the mechanism behind Dependency Injection ?
In this article we will discuss all these terms in simple words.

 

What is Dependency Inversion Principle (DIP)

The Dependency Injection principle refers to a specific form of decoupling software modules.It states:

  • High-level modules should not depend on low-level modules. Both should depend on abstractions.
  • Abstractions should not depend on details. Details should depend on abstractions

The Dependency Inversion principle (DIP) helps us to develop loosely couple code by ensuring that high-level modules depend on abstractions rather than concrete implementations of lower-level modules

 

What is Inversion Of Control (IOC)

Inversion of Control is a software design architecture where the framework controls the program flow. It means we are changing the control from normal way. IOC is used to increase modularity of the program and make it extensible.

 

To understand it better, lets look at an example.

When we are writing an ASP.net application, we are in ASP.net page life cycle but not in control where ASP.net is.

 

Dependency Injection

 

So as per the above diagram Dependency Injection (DI) and Service Locator (SL) are the ways of Implementing Inversion Of Control (IOC)

 

 

Difference between DIP and IOC (DIP VS IOC):

As stated earlier, DIP says high level module should not depend on low level module for abstraction and IOC provides abstraction.So if you want to make independent higher level module from the lower level module then you have to invert the control so that low level module will not control the interface and creation of the object.

 

 

Dependency Injection (DI):

  • Dependency Injection is a design pattern allows us to write loose coupled code
  • Dependency Injections helps us to manage future changes in code i.e. code maintainability
  • Dependency Injection uses a builder object to initialize objects and provide the required dependencies to the object means it allows you to ‘inject’ a dependency from outside the class

Service Locator (SL):

  • Service Locator is a software design pattern that allows us to develop loosely coupled code.
  • It implements the DIP principle and easier to use with an existing codebase
  • It introduces a locator object that  is used to resolve dependencies within a class.

Difference between DI and SL (DI VS SL):

The Service Locator allows you to “resolve” a dependency within a class and the Dependency Injection allows you to “inject” a dependency from outside the class.

  1. When you use a service locator, every class will have a dependency on your service locator but in dependency injection,the dependency injector will typically be called only once at startup, to inject dependencies into the main class.
  2. The Service Locator pattern is easier to use in an existing codebase as it makes the overall design looser without forcing changes to the public interface. Code that is based on the Service Locator pattern is less readable than the equivalent code that is based on Dependency Injection.

 

Let’s look at an example:

Dependency Injection

 

The client class has a dependency on the Service class.

If you want to make it loosely coupled, you have to use IoC to make the more flexible and reusable it.

To implement the IoC, we have  two main patterns: Service Locator and Dependency Injection.

Service Locator Implementation:

 

Dependency Injection

 

Sample Service Locator Implementation:
The Inversion happens in the constructor, by locating the Service that implements the IService-Interface.

The dependencies are assembled by a “Locator”.

Dependency Injection Implementation:

 

Dependency Injection

 

Sample Builder Implementation:
The Injection happens in the constructor, by passing the Service that implements the IService-Interface.

The dependencies are assembled by a “Builder” and Builder responsibilities are as follows:

  • knowing the types of each IService
  • according to the request, send the abstract IService to the Client

 

© 2015, www.techkatak.com. All rights reserved.

4 thoughts on “Inversion of Control, Dependency Injection and Service Locator in C#

    1. No.

      For instance inversion of control containers would not work without service location. It’s how the resolve the services internally.

      But a much better example is ASP.NET MVC and WebApi. What do you think makes the dependency injection possible in the controllers? It is: Service location.

      Regards,
      Techkatak Admin

Comments are closed.