{"id":17834,"date":"2017-04-27T14:25:09","date_gmt":"2017-04-27T18:25:09","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=17834"},"modified":"2023-12-22T13:21:36","modified_gmt":"2023-12-22T18:21:36","slug":"using-swift-protocol-compositon-for-dependency-injection","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/04\/27\/using-swift-protocol-compositon-for-dependency-injection\/","title":{"rendered":"Using Swift Protocol Composition for Dependency Injection"},"content":{"rendered":"<p><a href=\"http:\/\/merowing.info\/2017\/04\/using-protocol-compositon-for-dependency-injection\/\">Krzysztof Zab&#x142;ocki<\/a>:<\/p>\n<blockquote cite=\"http:\/\/merowing.info\/2017\/04\/using-protocol-compositon-for-dependency-injection\/\"><p>Swift allows us to compose protocol requirements by using <code>&amp;<\/code> operator, that means that our entities can now contain just a single dependency storage:<\/p><pre>class FooViewModel {\n    typealias Dependencies = HasImageProvider &amp; HasArticleProvider\n    let dependencies: Dependencies init(..., dependencies: Dependencies)\n}<\/pre>\n<p>In your app controller or <a href=\"http:\/\/merowing.info\/2016\/01\/improve-your-ios-architecture-with-flowcontrollers\/\">flow coordinator<\/a> (Whatever is used to create new entities) you can store all dependencies under a single container struct:<\/p>\n<pre>struct AppDependency: HasImageProvider, HasArticleProvider, HasPersistanceProvider { \n    let imageProvider: ImageProvider\n    let articleProvider: ArticlesProvider\n    let persistanceProvider: PersistenceProvider\n}<\/pre>\n<p>Now all app dependencies are stored in a simple data container, one that doesn&rsquo;t have any logic, its not magical or anything, its just a plain struct.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Krzysztof Zab&#x142;ocki: Swift allows us to compose protocol requirements by using &amp; operator, that means that our entities can now contain just a single dependency storage:class FooViewModel { typealias Dependencies = HasImageProvider &amp; HasArticleProvider let dependencies: Dependencies init(..., dependencies: Dependencies) } In your app controller or flow coordinator (Whatever is used to create new entities) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2023-12-22T18:21:39Z","apple_news_api_id":"387dd88e-4818-42c1-99a6-4af973b2a616","apple_news_api_modified_at":"2023-12-22T18:21:40Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AOH3YjkgYQsGZpkr5c7KmFg","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[27,71,901,268],"class_list":["post-17834","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-craft","tag-programming","tag-swift-programming-language","tag-testing"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=17834"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17834\/revisions"}],"predecessor-version":[{"id":41553,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17834\/revisions\/41553"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=17834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=17834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=17834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}