cake php_如何(以及为什么)在Swinject中使用Cake Pattern

cake php

by Peter-John Welcome

由Peter-John Welcome

如何(以及为什么)在Swinject中使用Cake Pattern (How (and why) to use the Cake Pattern with Swinject)

In my previous article, I showed how we can use the Cake Pattern to do dependency injection without any libraries. I got a lot of awesome feedback from many people suggesting alternative methods, which indicates that there is lots of interest in this topic.

在上一篇文章中 ,我展示了如何使用Cake Pattern在没有任何库的情况下进行依赖项注入。 我从很多人那里获得了很多令人敬畏的反馈,他们提出了替代方法,这表明对此主题有很多兴趣。

One of the questions I got asked, which is very important, was how do we swap out our implementation with a mock for testing.

我被问到的一个非常重要的问题是,如何用模拟替换掉我们的实现以进行测试。

In the comments, I made some suggestions. One of these was to use a dependency container.

在评论中,我提出了一些建议。 其中之一是使用依赖项容器。

Swinject, which is a framework, is one of the dependency injection frameworks out there that implements a dependency container pattern.

Swinject是一个框架,是实现依赖项容器模式的依赖项注入框架之一。

You may be wondering: why we would need the cake pattern if we can just use Swinject? Or why would we try to use them together? Well, this comes down to personal preference. But I’d like to show how we can use these two together.

您可能想知道:如果仅使用Swinject,为什么我们需要蛋糕模式? 还是我们为什么要尝试一起使用它们? 好吧,这取决于个人喜好。 但我想展示我们如何一起使用这两个。

入门 (Getting Started)

In order for us to use Swinject in our project, we will need to install the pod.

为了使我们在项目中使用Swinject ,我们需要安装pod。

pod 'Swinject'

Once we have our pod installed, we will start by creating two protocols. The first one will be a Registrable protocol that will have a register method that takes three parameters.

安装好pod后,我们将首先创建两个协议。 第一个将是可注册协议,该协议将具有采用三个参数的注册方法。

  1. Dependency — this will be the type we are registering on the container.

    依赖关系-这将是我们在容器上注册的类型。
  2. Implementation — The implementation for the dependency we want it to resolve to.

    实现-我们要解决的依赖项的实现。
  3. ObjectScope — The scope in which we want this dependency to live. (Optional)

    ObjectScope-我们希望此依赖项存在的范围。 (可选的)

Our second protocol will be the Resolvable protocol which will have two methods on it. The first one is a resolve method, which will take a dependency type and return a concrete implementation of that type. The second one is a reset method that will reset the Resolvable for us (useful for testing).

我们的第二个协议是可解析协议,上面有两种方法。 第一个是resolve方法,它将采用依赖类型并返回该类型的具体实现。 第二种是重置方法,它将为我们重置可解析的(可用于测试)。

We will now create a dependency container class that will conform to these protocols.

现在,我们将创建一个符合这些协议的依赖项容器类。

We will create a Swinject container and a static instance on our dependency container class.

我们将在依赖容器类上创建一个Swinject容器和一个静态实例。

Warning: This code is written in Swift 4, where private can be used in extensions (not like in Swift 3, were fileprivate was needed).

警告:这段代码是用Swift 4编写的,其中private可以在扩展中使用(与Swift 3不同,需要fileprivate)。

First, we will conform to the Registrable protocol and use the Swinject container we created and register our dependencies on it, with its respective implementations. We will also specify the objectScope to be graph by default.

首先,我们将遵循Registrable协议,并使用我们创建的Swinject容器并注册其依赖项及其相应的实现。 我们还将默认情况下将objectScope指定为图。

Swinject provides four different built-in scopes. Please see the link below to the documentation where it is excellently explained.

Swinject提供了四个不同的内置范围。 请查看下面的链接,该链接对文档进行了详细说明。

Swinject/SwinjectSwinject - Dependency injection framework for Swift with iOS/macOS/Linuxgithub.com

Swinject / Swinject Swinject-使用iOS / macOS / Linux的Swift依赖注入框架

Next, we conform to the Resolvable protocol and again use the same Swinject container to resolve the dependencies. We will reset the container in the reset method by removing all the registered dependencies on the container.

接下来,我们遵循可解析协议,并再次使用相同的Swinject容器来解析依赖关系。 我们将通过删除容器上所有已注册的依赖项,以reset方法重置容器。

We now have a dependency container — Yay!! But how do we use this container to resolve our dependencies?

现在,我们有了一个依赖容器-是的! 但是我们如何使用这个容器来解决我们的依赖关系呢?

We will create a Resolver factory that will handle this for us. It will first have a container property of type Resolvable, and this will be initialized with the dependency container class instance. We make this container of type Resolvable so that we can swap it out with any dependency container instance that conforms to that protocol.

我们将创建一个Resolver工厂来为我们处理。 它首先将具有Resolvable类型的容器属性,并将使用依赖项容器类实例进行初始化。 我们将此容器设置为Resolvable类型,以便我们可以将其与符合该协议的任何依赖关系容器实例交换出去。

We will now create two static methods that will be resolving and resetting our container when using our Resolvable container.

现在,我们将创建两个静态方法,这些方法将在使用可解析容器时解析和重置容器。

We have created this Resolver factory, and now it’s time to use it.

我们已经创建了这个Resolver工厂,现在是时候使用它了。

When creating our protocol extension (where we were resolving our implementation in the previous article), we can now use our Resolver factory.

创建协议扩展(在上一篇文章中解决实现的地方)时,我们现在可以使用Resolver工厂。

We also need to remember that we will now have to register our dependency on our container.

我们还需要记住,我们现在必须注册对容器的依赖。

There we go, we have the cake pattern with with Swinject as our dependency container.

到这里,我们有了以Swinject作为依赖容器的蛋糕模式。

好处 (Benefits)

The benefits of this approach are that we are decoupling the components of our application and providing a single source of resolving for these components. It also makes it much easier for us to swap out implementations with mocks for testing.

这种方法的好处是,我们可以将应用程序的组件分离开来,并为这些组件提供单一的解决方案。 这也使我们更容易将带有模拟的实现换出进行测试。

This gives us the option to share components anywhere in our application, as we will be able to resolve any dependency at any time with our injectable protocol extensions.

这使我们可以选择在应用程序中的任何位置共享组件,因为我们可以使用可注入协议扩展随时解决任何依赖性。

单元测试 (Unit Tests)

How would we test this? Well, all we need to do is call reset on the Resolver and then register the dependencies with mock implementations.

我们将如何测试呢? 好吧,我们需要做的就是在Resolver上调用reset,然后使用模拟实现注册依赖项。

We now have our mocks being injected. Looks like we’re done.

现在,我们的模拟被注入。 看起来我们完成了。

Go try it! Let me know what you guys think.

去试试吧! 让我知道你们的想法。

Swinject is very powerful, and this article just demonstrates its basic functionality. If you would like me to explore more of its features, let me know in the comments below.

Swinject非常强大,本文仅演示其基本功能。 如果您希望我探索其更多功能,请在下面的评论中告诉我。

Get in Touch!

保持联系!

For the full example, you can find it on my Github.

对于完整的示例,您可以在我的Github上找到它。

pjwelcome/CakePatternWithSwinjectCakePatternWithSwinject - Cake pattern with Swinject as a dependency containergithub.comPeter-John (@pjapplez) | TwitterThe latest Tweets from Peter-John (@pjapplez). Mobile App Developer, Technology Explorer, Photographer, Co-Founder…twitter.com

pjwelcome / CakePatternWithSwinject CakePatternWithSwinject-将Swinject作为依赖项容器的蛋糕模式 github.com Peter-John(@pjapplez)| Twitter 来自Peter-John(@pjapplez)的最新推文。 移动应用程序开发人员,技术资源管理器,摄影师,联合创始人… twitter.com

Peter John Welcome — Google+

彼得·约翰(Peter John)欢迎-Google+

Thanks to Ashton Welcome, and Keegan Rush for reviewing this post.

感谢Ashton Welcome和Keegan Rush审阅了这篇文章。

翻译自: https://www.freecodecamp.org/news/the-cake-pattern-with-swinject-4357c4d2bd0b/

cake php

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/394344.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

运用Appium 实现添加微信好友自动化

本文为原创文章,如需转载请注明出处. 任务:实现批量添加微信好友自动化。 任务分析:1.首先要实现添加单个好友步骤自动化。 2.实现脚本读取Excel里的值。 3.参数化好友电话号码或者昵称。 PS:代码采用POM(Page Object Model)便于后续维护 数…

pdf.js浏览中文pdf乱码的问题解决

由于项目中需要支持移动设备在线浏览pdf,苹果还好,天生支持,但是安卓中就不行了,需要第三方组件的支持。 这里就找到了pdf.js,由于pdf数据太多,开始的时候没法一一测试,所以随便测试打开了几篇没…

python导入sas数据集_运用import过程进行SAS数据导入完全实用教程

运用import过程进行SAS数据导入完全实用教程1 单个规范格式文件导入。对单个文件进行导入是我们遇到最多的情况,主要有以下几种:1.1 对指定分隔符(’|’,’’,’!’,’ab’等)数据的导入,这里以’!’为例de…

【效率专精系列】善用API统一描述语言提升RestAPI开发效率

团队内部RestAPI开发采用设计驱动开发的模式,即使用API设计文档解耦前端和后端的开发过程,双方只在联调与测试时耦合。在实际开发和与前端合作的过程中,受限于众多因素的影响,开发效率还有进一步提高的空间。本文的目的是优化工具…

leetcode剑指 Offer 14- I. 剪绳子(动态规划)

给你一根长度为 n 的绳子,请把绳子剪成整数长度的 m 段(m、n都是整数,n>1并且m>1),每段绳子的长度记为 k[0],k[1]…k[m-1] 。请问 k[0]k[1]…*k[m-1] 可能的最大乘积是多少?例如,当绳子的…

数据包提取文件_航测怎样高效提取无人机POS航点数据

无限创新工作室研发的POS数据记录仪是一款采集飞控POS 数据并管理的设备,它将飞控 POS 点数据进行记录,形成单独的POS 数据记录TXT 文本,并独立存储于内存卡,可通过USB、U 盘或内存卡形式对数据进行读取。通过对相机进行拍照控制和…

点击删除表格中的行并提交到数据库

html中&#xff1a; <el-table-column prop"operation" label"操作" width"170"> <template slot-scope"scope"> <el-button size"small" type"danger" click"deleteRow(scope.$index,s…

BZOJ 1878: [SDOI2009]HH的项链

1878: [SDOI2009]HH的项链 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 3548 Solved: 1757[Submit][Status][Discuss]Description HH有一串由各种漂亮的贝壳组成的项链。HH相信不同的贝壳会带来好运&#xff0c;所以每次散步 完后&#xff0c;他都会随意取出一段贝壳&…

分布式 知乎 github_如何使用GitHub本机功能来帮助管理中型分布式团队

分布式 知乎 githubby Alex Ewerlf由AlexEwerlf 如何使用GitHub本机功能来帮助管理中型分布式团队 (How to use GitHub native features to help manage a mid-size distributed team) My team created a wiki page in our private Github repo about how we work on a common…

开始时间小于 结束时间 js_DNF分享红包开始及结束时间 红包有什么奖励相关介绍...

[闽南网]DNF分享红包分享快乐时间从2019年的1月3日开始到1月21日前结束&#xff0c;活动期间玩家每天登录游戏可以得到一个新年红包&#xff0c;使用后可以为同一个频道的玩家送去祝福&#xff0c;根据送出红包的数量得到不同的奖励。(dnf幸运饺子铺活动)(DNF95版新副本攻略)本…

文件的相关操作

将输出的内容直接输出到文件中去 &#xff1a;freopen( “1.txt” , "w" , stdout &#xff09;转载于:https://www.cnblogs.com/ccut-ry/p/7456190.html

leetcode1504. 统计全 1 子矩形(动态规划)

给你一个只包含 0 和 1 的 rows * columns 矩阵 mat &#xff0c;请你返回有多少个 子矩形 的元素全部都是 1 。 示例 1&#xff1a; 输入&#xff1a;mat [[1,0,1], [1,1,0], [1,1,0]] 输出&#xff1a;13 解释&#xff1a; 有 6 个 1x1 的矩形。 有 2 个 1x2 的矩形。 有 3…

学plc好还是python好_PLC是学西门子的好还是学三菱的?

有人回复的很经典&#xff1a;“小孩子才会选择&#xff0c;大人肯定是都要。”如果你是学生&#xff0c;或者正准备踏入这个行业&#xff0c;建议你先学西门子的博途&#xff0c;毕竟这个在国内用的人多些。但是&#xff0c;你要时刻记得&#xff0c;你的目标是星辰大海~~~不要…

wps如何自己制作流程图_怎么制作流程图,wps自动生成流程图方法

在职场中我们要会熟练使用各种办公软件&#xff0c;才能提高我们的工作效率&#xff0c;下面我为大家分享三种制作流程图的方法&#xff0c;非常简单哦&#xff01;一&#xff0c;在Word中制作流程图1&#xff0c;首先点击“插入”再点击“形状”,点击新建绘图画布&#xff0c;…

doom 源码_Cartpole和Doom的策略梯度简介

doom 源码by Thomas Simonini通过托马斯西蒙尼(Thomas Simonini) Cartpole和Doom的策略梯度简介 (An introduction to Policy Gradients with Cartpole and Doom) This article is part of Deep Reinforcement Learning Course with Tensorflow ?️. Check the syllabus here…

SQL 邮件配置篇

在我们运维工作中&#xff0c;经常要对备份&#xff0c;ETL等作业进行监控&#xff0c;这时我们需要用到SQL SERVER自带的邮件服务器&#xff0c;其原理&#xff0c;我在这么里不多说&#xff0c;直接来实战&#xff0c;下面是我对服务器配置源码&#xff0c;分享给大家&#x…

选定用户与用户组启动流程(学习笔记)

public class RepostoryServiceTest {private static final Logger LOGGER LoggerFactory.getLogger(RepostoryServiceTest.class);Rulepublic ActivitiRule activitiRule new ActivitiRule();Testpublic void testRepository(){//repositoryService最重要的功能就是对流程定…

python关于包的题怎么做_Python自定义包引入

python中的Module是比较重要的概念。常见的情况是&#xff0c;事先写好一个.py文 件&#xff0c;在另一个文件中需要import时&#xff0c;将事先写好的.py文件拷贝 到当前目录&#xff0c;或者是在中增加事先写好的.py文件所在的目录&#xff0c;然后import。这样的做法&#x…

汽车之家的安全框架,是如何从0到1搭建的?

“别人家的安全”是安全威胁情报&#xff08;微信ID&#xff1a;Threatbook&#xff09;近期推出的一档专栏。 合规、管理、构建、应急……安全问题千千万&#xff0c;层出不穷。我们没办法给出这些问题的标准答案&#xff0c;但我们可以用Case Study的形式&#xff0c;让你看看…

leetcode264. 丑数 II

编写一个程序&#xff0c;找出第 n 个丑数。 丑数就是质因数只包含 2, 3, 5 的正整数。 示例: 输入: n 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。 说明: 1 是丑数。 n 不超过1690。 解题思路 直接用treeset去重和排序 代码 class Solution …