神秘的数组初始化
As a new developer myself, when I started to learn how to use dependency injection I found it oddly confusing, and during my research, I discovered that many new developers also found the concept difficult to grasp. Yet, the concept isn’t very difficult! I believe once new developers understand why using dependency injection is a good practice the concept will become more clear. In this article I hope to demystify the dependency injection for beginners, dependency injection is an incredibly powerful pattern and should be implemented in your codebase.
作为我自己的新开发人员,当我开始学习如何使用依赖注入时,我感到奇怪,在研究过程中,我发现许多新开发人员也发现了难以理解的概念。 但是,这个概念并不是很难! 我相信,一旦新开发人员了解了为什么使用依赖注入是一种好习惯,那么这个概念将变得更加清晰。 在本文中,我希望为初学者揭开依赖注入的神秘面纱,依赖注入是一种非常强大的模式,应该在您的代码库中实现。
Let’s have a look at the definition of dependency injection:
让我们看一下依赖注入的定义:
In software engineering, dependency injection is a technique in which an object receives other objects that it depends on. These other objects are called dependencies. In the typical “using” relationship the receiving object is called a client and the passed object is called a service. — Wikipedia
在软件工程中,依赖项注入是一种对象接收其依赖的其他对象的技术。 这些其他对象称为依赖项。 在典型的“使用”关系中,接收对象称为客户端,而传递的对象称为服务。 —维基百科
Now that seems like a very vague definition, but don’t worry we are going to break this down. A simplified explanation of how a dependency injection works would be something along these lines, rather than a class instantiating the objects it encapsulates itself, it gets passed instances of these objects to use. These objects are then its dependencies and the act of passing them to the class is called injection.
现在,这似乎是一个非常模糊的定义,但是请放心,我们将对此进行细分。 对依赖注入的工作方式的简化解释将遵循这些思路,而不是通过类实例化其自身封装的对象,而是通过传递这些对象的实例来使用。 这些对象就是它们的依赖关系,将它们传递给类的行为称为注入。
依赖项注入的类型 (Types of Dependency Injections)
In this article we will be covering the most common types of dependency injections namely:
在本文中,我们将介绍最常见的依赖项注入类型,即:
- Constructor Injection 构造函数注入
- Property Injection 资产注入
- Method Injection 方法注入
- Bastard/Poor man’s Dependency Injection 混蛋/穷人的依赖注射
The best way forward would be to start at the beginning, the first example we will be looking at is a class which has dependencies but does not make use of dependency injection:
最好的方法是从头开始,我们将要看的第一个示例是一个具有依赖关系但不使用依赖关系注入的类:
没有依赖注入: (No Dependency Injection:)
Say we have a class called Band which has two dependencies called drummer and guitarist. To have a functional Band class without using dependency injection we would have to do something like this:
假设我们有一个名为Band的类,它有两个依赖项,分别是鼓手和吉他手。 要拥有功能性的Band类而不使用依赖项注入,我们必须执行以下操作:
构造函数注入: (Constructor Injection:)
Now we can improve the above code and implement constructor injection, here we will pass the dependencies through the constructor of Band.
现在我们可以改进上面的代码并实现构造函数注入,这里我们将依赖项传递给Band的构造函数。
Benefits when using constructor injection include the ability to have the dependencies remain immutable, as we are able to declare the properties as constants.
使用构造函数注入的好处包括使依赖项保持不变的能力,因为我们能够将属性声明为常量。
属性注入: (Property Injection:)
Here we are going to be implementing a property injection, we declare public optional variables and set them after we instantiate the band object, effectively injecting the Band class with the dependencies it requires.
在这里,我们将实现属性注入,我们声明公共可选变量,并在实例化band对象之后对其进行设置,从而有效地将Band类及其所需的依赖项注入。
Property injection is also a great way to use dependency injection however, in this case, we need to declare the properties as mutable which is not ideal as it can be modified accidentally. Another shortcoming compared to constructor injection would be that if you are working in a large codebase or a codebase which you are not familiar with, when creating an object of Band you will not necessarily know that it has dependencies without going to investigate the Band class and this could possibly cause a runtime error.
属性注入也是使用依赖注入的一种好方法,但是,在这种情况下,我们需要将属性声明为可变的,这是不理想的,因为它可能被意外修改。 与构造函数注入相比,另一个缺点是,如果您使用的是大型代码库或您不熟悉的代码库,则在创建Band对象时,不必调查Band类和这可能会导致运行时错误。
方法注入: (Method Injection:)
Method injections are quite flexible and offer the developer a lot of freedom, as you are able to instantiate an object and even make use of the functions that do not require dependencies, and only inject the dependencies where they are needed.
方法注入非常灵活,为开发人员提供了很大的自由度,因为您可以实例化对象,甚至可以使用不需要依赖项的功能,而只需将依赖项注入到需要的地方。
One of the limiting factors that Method Injection introduces is that the Band class does not have full access to the dependencies as a whole.
方法注入引入的限制因素之一是Band类不能完全访问整个依赖项。
混蛋/穷人的DI: (Bastard/Poor Man’s DI:)
Now, this may seem similar to the constructor injection, however, we are providing default parameters, which are, essentially, default dependencies. This will improve the ability to test however this is still considered bad practice as if you were to use the default constructor it is tightly coupled to the implementation of the dependencies.
现在,这似乎类似于构造函数注入,但是,我们提供了默认参数,本质上是默认依赖项。 这将提高测试的能力,但是仍然认为这是一种不好的做法,就像您要使用默认构造函数一样,它与依赖项的实现紧密耦合。
So, something worth noting is that you should avoid implementing Bastard Injection.
因此,值得注意的是您应该避免实施Bastard Injection。
为什么要使用依赖注入: (Why you should be using Dependency Injection:)
There are multiple benefits when using dependency injection, here are the main reasons you should be making use of dependency injection:
使用依赖项注入有很多好处,这是您应该使用依赖项注入的主要原因:
可读性: (Readability:)
Once you have adopted the dependency injection pattern the structure of the classes and project as a whole becomes a lot more clear and easier to read. Once you get comfortable with the pattern you will start to know where to look for certain things and what certain classes will require.
一旦采用了依赖注入模式,整个类和项目的结构就变得更加清晰易读。 一旦熟悉了模式,您将开始知道在哪里寻找某些东西以及某些类将需要什么。
测试: (Testing:)
Unit testing is quite difficult to do without using dependency injection, once you follow the pattern it becomes significantly easier to unit test as you are able to mock out dependencies easily.
如果不使用依赖注入,就很难进行单元测试,一旦遵循这种模式,单元测试就变得非常容易,因为您可以轻松地模拟出依赖。
关注点分离: (Separation of concerns:)
Using dependency injection also prevents your code from being tightly coupled together as well as promotes the separation of concerns design principle.
使用依赖注入还可以防止代码紧密耦合在一起,并促进关注点设计原则的分离。
结论: (In conclusion:)
There are many other methods you can use to implement the dependency injection pattern into your projects, these are simply the most common ways. lastly, I would like to encourage new developers to embrace topics that they do not understand and discover why they are used.
您可以使用许多其他方法将依赖关系注入模式实现到项目中,这些只是最常见的方法。 最后,我想鼓励新开发人员加入他们不了解的主题,并发现使用它们的原因。
翻译自: https://medium.com/dvt-engineering/dependency-injection-demystified-in-swift-fbce9e94a5d
神秘的数组初始化