博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cake php_如何(以及为什么)在Swinject中使用Cake Pattern
阅读量:2527 次
发布时间:2019-05-11

本文共 6064 字,大约阅读时间需要 20 分钟。

cake php

by Peter-John Welcome

由Peter-John Welcome

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

In my , 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.

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

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

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

You may be wondering: why we would need the cake pattern if we can just use ? 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.

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

入门 (Getting Started)

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

为了使我们在项目中使用 ,我们需要安装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提供了四个不同的内置范围。 请查看下面的链接,该链接对文档进行了详细说明。

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上找到它。

Thanks to , and for reviewing this post.

感谢和审阅了这篇文章。

翻译自:

cake php

转载地址:http://kdgwd.baihongyu.com/

你可能感兴趣的文章
图片放大器——wpf
查看>>
SCALA STEP BY STEP
查看>>
cocos2d-x学习笔记
查看>>
MySql中的变量定义
查看>>
Ruby数组的操作
查看>>
hdu1181暴搜
查看>>
解码字符串 Decode String
查看>>
json学习笔记
查看>>
工具:linux 性能监控工具-nmon
查看>>
fatal error C1853
查看>>
Ural 1001 - Reverse Root
查看>>
玩转webpack之webpack的entry output
查看>>
java 操作mongodb查询条件的常用设置
查看>>
黑马程序员_java基础笔记(02)...java语言基础组成
查看>>
关于缓存击穿
查看>>
对innodb 拷贝文件实现数据库的方式(转)
查看>>
python知识点 2014-07-09
查看>>
FloatingActionButton的一点学习感悟
查看>>
ABAP CDS ON HANA-(10)項目結合して一つ項目として表示
查看>>
网站地址信息
查看>>