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.pswp.cn/news/394344.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/394344.shtml
英文地址,請注明出處:http://en.pswp.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 …