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后,我們將首先創建兩個協議。 第一個將是可注冊協議,該協議將具有采用三個參數的注冊方法。
- Dependency — this will be the type we are registering on the container. 依賴關系-這將是我們在容器上注冊的類型。
- Implementation — The implementation for the dependency we want it to resolve to. 實現-我們要解決的依賴項的實現。
- 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