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

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

  1. [Cake] 1. CI中的Cake

    在上一篇C#Make自动化构建-简介中,简单的介绍了下Cake的脚本如何编写以及通过Powershell或者Bash在本地运行Cake脚本.本篇在此基础上,介绍下如何在CI环境中使用Cake. 1. ...

  2. java正则表达式类_java中正则表达式之Pattern类与Matcher类

    java中正则表达式之Pattern类与Matcher类 ======================================================================= ...

  3. 如何在ex表格导入php_怎么使用php把表格中的数据导入到excel中,php如何快速导入excel表格数据...

    php怎么导入大量数据的excel php导出数据的Excel: PHP从数据库分多次读取100万行记录,和分将100万入文本文件都没问题 Excel可以支100万行记录,Excel 2003最大支持 ...

  4. linux命令行安装 php_如何在 Linux 命令行中使用和执行 PHP 代码

    PHP是一个开源服务器端脚本语言,最初这三个字母代表的是"Personal Home Page",而现在则代表的是"PHP:Hypertext Preprocessor& ...

  5. 什么工具可以分析php源代码,PHP_一个可以找出源代码中所有中文的工具,一个可以找出源代码中所有中 - phpStudy...

    一个可以找出源代码中所有中文的工具 一个可以找出源代码中所有中文的工具 填写需要查找的路径$sf即可. 功能 1 找出所有中文 2 忽略注释语句中的中文 3 可添加需要忽略的文件和文件夹 4 生成日志 ...

  6. java中的pattern_Java Pattern的用法是什么?

    展开全部 java 中pattern为正则表达式的编译表示形式.指定为字符串的正则表达式必须首先被编译为此类的实例.然后,62616964757a686964616fe58685e5aeb931333 ...

  7. java中正则表达式以及Pattern和Matcher

    正则匹配 // 反斜杠 /t 间隔 ('/u0009') /n 换行 ('/u000A') /r 回车 ('/u000D') /d 数字 等价于[0-9] /D 非数字 等价于[^0-9] /s 空白 ...

  8. TALIB 中文文档 Pattern Recognition Functions 形态识别

    文章目录 Pattern Recognition Functions 形态识别 CDL2CROWS - Two Crows CDL3BLACKCROWS - Three Black Crows CDL ...

  9. HTML5中的Article和Section元素

    日期:2013-3-20  来源:GBin1.com HTML5带出了一系列新元素,并且将在未来被广泛应用.然而,有一些元素在使用时易被混淆,包括以下两个新元素:<article>和< ...

最新文章

  1. 算法er的尽头会是To B吗?
  2. Java Web 前端高性能优化(二)
  3. python 客户端应用程序_创建python Web服务和C#客户端应用程序的最佳方法 - c#
  4. json 数据 生成 图表_Python数据分析:手把手教你用Pandas生成可视化图表
  5. 当你所有的尝试告一段落
  6. 源码时代php中级项目,PHP学科项目评比圆满结束
  7. boost库 数组智能指针scoped_array
  8. SpringBoot_Redis配置
  9. oracle使用连接池,使用Oracle的数据连接池
  10. 用unity3d切割图片
  11. 什么是数字证书?它有什么作用?
  12. .net 版农业银行接口
  13. matlab uigetfile
  14. DM6437烧写总结
  15. LANP环境搭建(yum安装)
  16. html加密文件怎么解密,如何取消文件的加密保护,如何解密?
  17. 十秒钟刷完云班课的一节视频
  18. 凌晨3点不回家-现实版
  19. Java菜鸟逆袭之入门篇(附讲解)
  20. mas机 mysql_移动MAS机开发相关

热门文章

  1. 「一本通 6.4 例 4」曹冲养猪(CRT)
  2. /etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 文件的作用
  3. (hdu 简单题 128道)平方和与立方和(求一个区间的立方和和平方和)
  4. iOS 多参数 ...NS_REQUIRES_NIL_TERMINATION 的写法
  5. 在linux上实现DllMain + 共享库创建方法
  6. 关于Oracle实时数据库的优化思路
  7. Unity3D面试ABC
  8. Free SQLSever 2008的书
  9. OSCON上最受欢迎的Docker演讲
  10. spring集成struts2