【转】Understanding Inversion of Control, Dependency Injection and Service Locator Print
-----------------------------------------------------------------------------------------
Understanding Inversion of Control, Dependency Injection and Service Locator
So many developers are confused about the the term Dependency Injection (DI). The confusion is about terminology, purpose, and mechanics. Should it be called Dependency Injection, Inversion of Control, or Service Locator? Over the Internet, there are a lot of articles, presentations and discussion but, unfortunately, many of them use conflicting terminology. In this article I would like to explain, all these three OOPS concepts.
Dependency Inversion Principle (DIP)
The Dependency Inversion Principle states that:
High level modules should not depend upon low level modules. Both should depend upon abstractions.
Abstractions should not depend upon details. Details should depend upon 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. The Inversion of Control pattern is an implementation of this principle
Inversion of Control (IoC)
The term Inversion of Control (IoC) refers to a programming style where a framework or runtime, controls the program flow. Inversion of control means we are changing the control from normal way. It works on Dependency Inversion Principle. The most software developed on the .NET Framework uses IoC.
For example
When you write an ASP.NET application, you lie into the ASP.NET page life cycle, but you aren’t in control while ASP.NET is.
When you write a WCF service, you implement interfaces decorated with attributes. You may be writing the service code, but ultimately, you aren’t in control while WCF is.

More over IoC is a generic term and it is not limited to DI. Actually, DI and Service Locator patterns are specialized versions of the IoC pattern or you can say DI and Service Locator are the ways of implementing IoC.
For example, Suppose your Client class needs to use a Service class component, then the best you can do is to make your Client class aware of an IService interface rather than a Service class. In this way, you can change the implementation of the Service class at any time (and for how many times you want) without breaking the host code.
IoC and DIP
DIP says High level module should not depend on low level module and both should depend on abstraction. IoC is a way that provide abstraction. A way to change the control. IoC gives some ways to implement DIP. 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 not controlling interface and creation of the object. Finally IoC gives some way to invert the control.

IoC and DI
The terms Dependency Injection (DI) & Inversion of Control (IoC) are generally used interchangeably to describe the same design pattern. The pattern was originally called IoC, but Martin Fowler proposed the shift to DI because all frameworks invert control in some way and he wanted to be more specific about which aspect of control was being inverted.
Dependency Injection (DI)
DI is a software design pattern that allow us to develop loosely coupled code. DI is a great way to reduce tight coupling between software components. DI also enables us to better manage future changes and other complexity in our software. The purpose of DI is to make code maintainable.
The Dependency Injection pattern 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 also allow us to develop loosely coupled code. It implements the DIP principle and easier to use with an existing codebase as it makes the overall design looser without forcing changes to the public interface.
The Service Locator pattern introduces a locator object that objects is used to resolve dependencies means it allows you to "resolve" a dependency within a class.
Dependency Injection and Service Locator with Example
Let's consider the simple dependency between two classes as shown in the fig and it is a simple approach, that you know.

Now have a look at the folllowing code:
- public class Service
- {
- public void Serve()
- {
- Console.WriteLine("Service Called");
- //To Do: Some Stuff
- }
- }
- public class Client
- {
- private Service _service;
- public Client()
- {
- this._service = new Service();
- }
- public void Start()
- {
- Console.WriteLine("Service Started");
- this._service.Serve();
- //To Do: Some Stuff
- }
- }
Clearly, 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, you have the choice of two main patterns: Service Locator and Dependency Injection. 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.
Using Service Locator

The above code can be re-written as by using Service Locator as follows.
- public interface IService
- {
- void Serve();
- }
- public class Service : IService
- {
- public void Serve()
- {
- Console.WriteLine("Service Called");
- //To Do: Some Stuff
- }
- }
- public static class LocateService
- {
- public static IService _Service { get; set; }
- public static IService GetService()
- {
- if (_Service == null)
- _Service = new Service();
- return _Service;
- }
- }
- public class Client
- {
- private IService _service;
- public Client()
- {
- this._service = LocateService.GetService();
- }
- public void Start()
- {
- Console.WriteLine("Service Started");
- this._service.Serve();
- //To Do: Some Stuff
- }
- }
Sample ServiceLocator-Implementation:
- class Program
- {
- static void Main(string[] args)
- {
- var client = new Client();
- client.Start();
- Console.ReadKey();
- }
- }
The Inversion happens in the constructor, by locating the Service that implements the IService-Interface. The dependencies are assembled by a "Locator".
Using Dependency Injection

The above code can also be re-written as by using Dependency Injection as follows.
- public interface IService
- {
- void Serve();
- }
- public class Service : IService
- {
- public void Serve()
- {
- Console.WriteLine("Service Called");
- //To Do: Some Stuff
- }
- }
- public class Client
- {
- private IService _service;
- public Client(IService service)
- {
- this._service = service;
- }
- public void Start()
- {
- Console.WriteLine("Service Started");
- this._service.Serve();
- //To Do: Some Stuff
- }
- }
Sample Builder-Implementation:
- class Program
- {
- static void Main(string[] args)
- {
- client = new Client(new Service());
- client.Start();
- Console.ReadKey();
- }
- }
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, feed the abstract IService to the Client
What's wrong with the above two approaches?
Above two approaches can be resemble to DI FACTORY. There are some issues with the above two approaches:
It is hardcoded and can't be reused across applications, due to hardcoded factories. This makes the code stringent to particular implementations.
It is Interface dependent since interfaces are used for decoupling the implementation and the object creation procedure.
It's Instantiating instantiations are very much custom to a particular implementation.
Everything is compile time since all the dependent types for the objects in the instantiation process (factory) have to be known at compile time.
What is the Solution ?
IoC containers is the solution to resolve above two approaches issues. Hence, when we compose applications without a DI CONTAINER, it is like a POOR MAN’S DI. Moreover, DI CONTAINER is a useful, but optional tool.
You can also improve above two approaches by doing some more work. You can also combine above two approaches to make code more independent
Dependency Injection VS Service Locator
When you use a service locator, every class will have a dependency on your service locator. This is not the case with dependency injection. The dependency injector will typically be called only once at startup, to inject dependencies into the main class.
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.
References
http://www.objectmentor.com/resources/articles/dip.pdf
http://satoricode.net/2011/10/01/UnderstandingTheBenefitsOfADependencyInjectionContainerIOC.aspx
What do you think?
I hope you will enjoy the DIP, IoC, DI and SL patterns. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.
【转】Understanding Inversion of Control, Dependency Injection and Service Locator Print的更多相关文章
- Inversion of Control Containers and the Dependency Injection pattern(转)
In the Java community there's been a rush of lightweight containers that help to assemble components ...
- Inversion of Control Containers and the Dependency Injection pattern--Martin Fowler
原文地址:https://martinfowler.com/articles/injection.html n the Java community there's been a rush of li ...
- [转载][翻译] IoC 容器和 Dependency Injection 模式
原文地址:Inversion of Control Containers and the Dependency Injection pattern 中文翻译版本是网上的PDF文档,发布在这里仅为方便查 ...
- Dependency Injection
Inversion of Control - Dependency Injection - Dependency Lookup loose coupling/maintainability/ late ...
- 控制反转Inversion of Control (IoC) 与 依赖注入Dependency Injection (DI)
控制反转和依赖注入 控制反转和依赖注入是两个密不可分的方法用来分离你应用程序中的依赖性.控制反转Inversion of Control (IoC) 意味着一个对象不会新创建一个对象并依赖着它来完成工 ...
- Inversion of Control Containers and the Dependency Injection pattern
https://martinfowler.com/articles/injection.html One of the entertaining things about the enterprise ...
- 控制反转(Inversion of Control,英文缩写为IoC),另外一个名字叫做依赖注入(Dependency Injection,简称DI)
控制反转(Inversion of Control,英文缩写为IoC),另外一个名字叫做依赖注入(Dependency Injection,简称DI),是一个重要的面向对象编程的法则来削减计算机程序的 ...
- AngularJs学习笔记--Dependency Injection(DI,依赖注入)
原版地址:http://code.angularjs.org/1.0.2/docs/guide/di 一.Dependency Injection(依赖注入) 依赖注入(DI)是一个软件设计模式,处理 ...
- 控制反转(Inversion of Control)之我的理解
关于控制反转(Inversion of Control),在具体实现上也有许多其它的叫法,如依赖倒置(Dependency Inversion Principles, DIP).依赖注入(Depend ...
随机推荐
- Educational Codeforces Round 22 E. Army Creation
Educational Codeforces Round 22 E. Army Creation 题意:求区间[L,R]内数字次数不超过k次的这些数字的数量的和 思路:和求区间内不同数字的数量类似,由 ...
- BZOJ1483 [HNOI2009]梦幻布丁 【链表 + 启发式合并】
题目 N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色. 例如颜色分别为1,2,2,1的四个布丁一共有3段颜色. 输入格式 第一行给出N,M表示 ...
- 【CZY选讲·棋盘迷宫】
题目描述 一个N*M的棋盘,’.’表示可以通过,’#’表示不能通过,给出Q个询问,给定起点和终点,判断两点是否联通,如联通输出“Yes”,否则输出“No”. 数据范围 N,M <=500,Q ...
- shell脚本——项目2
案例名称:发送告警邮件 背景: 外部邮箱的服务器(163等) 安装mailx(yum) 配置邮箱信息 vim /etc/mail.rc #配置自己的邮箱信息 set from=18906534060@ ...
- 插件安装:包管理器——Package Control
首先,按CTRL+`,打开控制台 粘贴下面的代码,之后回车 如果是sublime3 ? 1 import urllib.request,os,hashlib; h = '7183a2d3e96f1 ...
- Data_Structure01-绪论作业!
一.作业题目 仿照三元组或复数的抽象数据类型写出有理数抽象数据类型的描述 (有理数是其分子.分母均为整数且分母不为零的分数). 有理数基本运算: 构造有理数T,元素e1,e2分别被赋以分子.分母值 销 ...
- java基础练习 12
public class Twelfth { /*海滩上有一堆桃子,五只猴子来分.第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份.第二只猴子把剩下的桃子又平均分成五 ...
- 自定义topo遇到的坑
错误:TypeError: __init__() got an unexpected keyword argument 'delay' 解决办法:在创建topo的地方加一个link=TCLink即可, ...
- hdu 5142(数学-进制转换)
NPY and FFT Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- springBoot Ribbon 负载均衡
1.依赖引用 <!-- 引入关于 eureka-server的依赖 --> <dependency> <groupId>org.springframework.cl ...