- Mastering Ninject for Dependency Injection
- Daniel Baharestani
- 656字
- 2021-08-04 10:12:02
What is Dependency Injection?
Dependency Injection is one of the techniques in software engineering which improves the maintainability of a software application by managing the dependent components. In order to have a better understanding of this pattern, let's start this section with an example to clarify what is meant by a dependency, and what other elements are involved in this process.
Cameron is a skilled carpenter who spends most of his time creating wooden stuffs. Today, he is going to make a chair. He needs a saw, a hammer, and other tools. During the process of creating the chair, he needs to figure out what tool he needs and find it in his toolbox. Although what he needs to focus on is how to make a chair, without thinking of what tools he needs and how to find them, it is not possible to finish the construction of the chair.
The following code is the C# representation of Cameron, as a carpenter:
class Carpenter { Saw saw = new Saw(); void MakeChair() { saw.Cut(); // ... } }
Sarah is a heart surgeon. She works for a hospital and spends her days in the operation room, and today she is going to perform an open-heart surgery. It is a sophisticated procedure, and she needs to focus on the operation itself, rather than finding the tools during the operation. That is why she has an assistant to provide her with the tools she requires. This way, she ensures that the exact tool that she needs will be in her hand by her assistant. She doesn't need to know where the tool is and how to find it. These are her assistant's responsibilities.
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
This is the C# implementation of Sarah, the surgeon:
class Surgeon { private Forceps forceps; // The forceps object will be injected into the constructor // method by a third party while the class is being created. public Surgeon(Forceps forceps) { this.forceps = forceps; } public void Operate() { forceps.Grab(); //... } }
As we can see, she doesn't need to worry about how to get the forceps; they are provided to her by someone else.
In the previous examples, Cameron and Sarah are samples of dependent components that have a responsibility, and tools that they need are their dependencies. Dependency Injection is all about how they get to the tools they need. In the first example, the dependent component (Cameron) itself had to locate the dependency, while in the second one, a third party (the assistant) locates and provides it. This third party is called an Injector, which injects the dependencies.
DI or Inversion of Control (IoC)
Martin Fowler defines Inversion of Control (IoC) as a style of programming in which the framework takes the control of the flow instead of your code. Comparing handling an event to calling a function is a good example to understand IoC. When you call the functions of a framework, you are controlling the flow, because you decide in what sequence to call the functions. But in case of handling events, you are defining the functions and the framework is calling them, so the control is inverted to the framework instead of you. This example showed you how control can be inverted. DI is a specific type of IoC, because instead of your components concern about their dependencies, they are provided with the dependencies by the framework. Indeed, as Mark Seemann states in his book, Dependency Injection in .NET, IoC is a broader term which includes, but is not limited to, DI, even though they are often being used interchangeably. IoC is also known as the Hollywood Principle: "Don't call us, we'll call you".
- PyTorch深度學習實戰(zhàn):從新手小白到數(shù)據(jù)科學家
- ETL數(shù)據(jù)整合與處理(Kettle)
- Hands-On Machine Learning with Microsoft Excel 2019
- Java Data Science Cookbook
- R數(shù)據(jù)科學實戰(zhàn):工具詳解與案例分析(鮮讀版)
- The Game Jam Survival Guide
- Lego Mindstorms EV3 Essentials
- TextMate How-to
- 跨領(lǐng)域信息交換方法與技術(shù)(第二版)
- 機器學習:實用案例解析
- Deep Learning with R for Beginners
- MySQL技術(shù)內(nèi)幕:InnoDB存儲引擎
- 改進的群智能算法及其應(yīng)用
- 云工作時代:科技進化必將帶來的新工作方式
- 大數(shù)據(jù)測試技術(shù):數(shù)據(jù)采集、分析與測試實踐(在線實驗+在線自測)