Compare commits

...

302 Commits

Author SHA1 Message Date
Ilya Puchka 7ec008a5c0 Merge pull request #237 from michalsrutek/develop
Fix couple of typos
2020-05-29 14:42:25 +01:00
Ilya Puchka 772bc13daa Merge pull request #238 from michalsrutek/use-anyobject-keyword
Use preferred AnyObject keyword
2020-05-29 14:42:03 +01:00
Michal Srutek fa4325399a Use preferred AnyObject keyword 2020-04-23 10:22:20 +02:00
Michal Srutek f7fbbda748 Fix couple of typos 2020-04-23 09:59:03 +02:00
Ilya Puchka 56b0768d60 bump version to 7.1.1 2019-12-27 11:14:55 +00:00
Ilya Puchka 8cf87efb95 Update podspec author 2019-12-27 11:07:21 +00:00
Ilya Puchka 3aa9b334f1 Merge pull request #233 from AliSoftware/spm-fix
Enabled StoryboardInstantiatable for SPM builds
2019-11-15 11:53:25 +00:00
Ilya Puchka b2a0cda242 Update StoryboardInstantiatable.swift 2019-11-13 13:16:52 +00:00
Ilya Puchka 82e3d02497 Merge branch 'master' into develop 2019-11-11 14:22:24 +00:00
Ilya Puchka 3b2f5a272f bumpe version to 7.1.0 2019-11-10 16:11:17 +00:00
Ilya Puchka eab2d424e7 allow to disable thread safety on container level 2019-11-10 16:01:20 +00:00
Ilya Puchka 0f2cda0a52 update changelog 2019-11-10 15:53:45 +00:00
Ilya Puchka 05c02c9645 Merge pull request #225 from AliSoftware/property-delegates
Property wrappers
2019-11-10 15:52:49 +00:00
Ilya Puchka fa086dad8f add xcode 11 and swift 5.1 to travis build 2019-11-10 15:35:53 +00:00
Ilya Puchka cdc09f5a03 removed unneeded initial nil values 2019-11-10 15:22:38 +00:00
Ilya Puchka b94ed3bea2 fix warning 2019-11-10 15:22:16 +00:00
Ilya Puchka 0fd70e65dd Merge branch 'develop' into property-delegates 2019-11-10 15:13:55 +00:00
Ilya Puchka 97e7f4782f Merge branch 'pr/221' into develop 2019-11-10 15:12:41 +00:00
Ilya Puchka f1fdbbc988 code review 2019-11-10 15:02:03 +00:00
Ilya Puchka e368bb3051 Merge branch 'develop' into nested_types 2019-11-10 14:50:07 +00:00
Ilya Puchka 4ce4832960 Merge pull request #229 from dchohfi/develop
Bump watchos deployment target to 3.0
2019-10-28 08:46:22 +00:00
Diego Chohfi d3bd0c27e3 Bump watchos deployment target to 3.0 2019-10-21 10:40:46 -07:00
Ilya Puchka be0c39ab6b update for beta 3 2019-07-07 23:54:58 +01:00
Ilya Puchka 0de45e7d53 fixed checking for new injected value 2019-07-07 23:53:45 +01:00
Ilya Puchka 3b421d0cff refactor property wrappers to struct 2019-07-07 16:16:50 +01:00
Ilya Puchka f1234bf2b9 introduce swift 5.1 property delegates 2019-06-30 19:12:42 +01:00
Ilya Puchka b0d7153bfe ignore swiftpm files 2019-06-30 18:27:59 +01:00
Ilya Puchka 0193aa6bf6 Merge pull request #224 from AliSoftware/swift5
Swift 5
2019-06-30 17:17:49 +01:00
Ilya Puchka de7ad3ac48 removed linux thread safety tests, regenerate test manifest 2019-06-30 16:59:10 +01:00
Ilya Puchka 40ad490c48 removed unneeded code 2019-06-30 15:53:23 +01:00
Ilya Puchka 95cc5df26f fixed Linux tests 2019-06-30 15:40:40 +01:00
Ilya Puchka 09a7115aff fix tests 2019-06-30 14:29:22 +01:00
Ilya Puchka 15c0002ea3 remove compatibility file 2019-06-12 01:18:14 +01:00
Ilya Puchka 0644d85a9c update swift version in all targets 2019-06-12 01:18:05 +01:00
Ilya Puchka e1522a0bf3 Merge branch 'develop' into swift5 2019-06-12 01:15:06 +01:00
Ilya Puchka a659415496 generate LinuxMain 2019-06-11 23:23:39 +01:00
Ilya Puchka efe25d575b update package description 2019-06-11 23:10:22 +01:00
Ilya Puchka 22c67fa30c disable spec validation 2019-06-11 21:30:55 +01:00
Ilya Puchka 7a091da44d migrate to swift 5 2019-06-11 21:16:03 +01:00
s 9580976dd8 Print nested types in errors, hash keys using full typenames 2019-04-01 17:13:06 +02:00
Ilya Puchka a4f0256313 fixed typos 2018-12-20 01:32:21 +00:00
Ilya Puchka 815e9ccd66 shorthand method for resolving properties via keypaths 2018-12-20 01:30:35 +00:00
Ilya Puchka b9bf9c4d75 Merge pull request #216 from AliSoftware/develop
Release 7.0.1
2018-12-19 22:54:14 +00:00
Ilya Puchka 530a20999a use Cocoapods 1.4.0 as latest versions fail validation 2018-12-19 15:56:32 +00:00
Ilya Puchka 4f3bc1e498 Merge branch 'master' into release/7.0.1 2018-11-19 20:35:40 +00:00
Ilya Puchka 8dcb136ff0 fixed typos 2018-11-19 20:34:56 +00:00
Ilya Puchka 88d02af706 bump version to 7.0.1 2018-11-19 20:32:40 +00:00
Ilya Puchka a8777c067e Allow to disabled/enable auto injection for container or single definition. fixes #214, resolves #212 2018-11-19 00:03:15 +00:00
Ilya Puchka db40e8580c Added test for regression SR-8878 2018-11-19 00:03:15 +00:00
Ilya Puchka 334fb384a5 Update README.md 2018-11-18 23:54:49 +00:00
Ilya Puchka 54e1db2232 Merge pull request #207 from AliSoftware/develop
Release 7.0.0
2018-09-22 23:31:41 +03:00
Ilya Puchka c5c6bd2821 Merge branch 'master' into develop 2018-09-22 23:20:46 +03:00
Ilya Puchka 593129c63e update cocoapods on travis 2018-09-22 20:47:45 +01:00
Ilya Puchka 9a8d919a13 bump xcode and swift version on travis 2018-09-22 20:16:49 +01:00
Ilya Puchka 17a2d64361 bumped version to 7.0.0 2018-09-22 20:16:49 +01:00
Ilya Puchka 27037c2dd7 fix indentation in some docs 2018-09-22 20:01:16 +01:00
Ilya Puchka bd253f4b0a bump xcode and swift version on travis 2018-09-22 19:48:10 +01:00
Ilya Puchka 882b2a2031 bumped version to 7.0 2018-09-22 19:25:18 +01:00
Ilya Puchka 6a14c1f96d updated CHANGELOG 2018-09-22 19:24:00 +01:00
Ilya Puchka f60e322445 bumped swift version, removed unneded IUO tests 2018-09-22 19:19:45 +01:00
Ilya Puchka 77f6fde2fa Merge branch 'swift42' into develop 2018-09-22 19:16:27 +01:00
Ilya Puchka a8f7a2972a silence warning 2018-09-22 19:15:43 +01:00
Ilya Puchka ce9088afc7 fix reusing optionals 2018-09-22 18:47:34 +01:00
Ilya Puchka ef563c65ba fix build error on mac with SPM 2018-09-19 16:46:31 +03:00
Ilya Puchka 90ff39c720 fix build error on mac with SPM 2018-09-19 15:13:22 +03:00
Ilya Puchka e45dbd634f Merge with DipUI (#206)
* merge with DipUI
2018-09-19 15:02:26 +03:00
Bruno Virlet f81c267c61 Disable gathering code coverage (#198)
This causes issues whenn linking as a static library using Carthage
2018-07-25 18:10:51 +01:00
Michele Gruppioni d5b07b1916 Removed extension of ImplicitlyUnwrappedOptional (#191)
* Removed extension of ImplicitlyUnwrappedOptional

It was deprecated and has been removed in swift 4.2

* Added Swift Version Check
2018-07-13 11:43:40 +01:00
Ilya Puchka 33993df68a disable test coverage 2018-06-06 21:08:44 +01:00
Ilya Puchka dadb68862a fixed getting resolved instances 2018-06-06 01:15:08 +01:00
Ilya Puchka 2e53aa8ae2 Merge pull request #190 from AliSoftware/develop
Release 6.1
2018-04-28 20:24:35 +01:00
Ilya Puchka 142a348b20 removed unneded builds from travis 2018-04-28 20:05:53 +01:00
Ilya Puchka 88f101639d updated travis config 2018-04-28 19:48:03 +01:00
Ilya Puchka 5e70633ece bumped swift version to 4.1 2018-04-28 19:43:49 +01:00
Ilya Puchka af57aa26ed bumped version to 6.1 2018-04-28 19:41:45 +01:00
Ilya Puchka 6d6d77d603 fixed swift 4.1 warnings 2018-04-28 19:39:46 +01:00
Ilya Puchka 8d2f6ab8fb Revert #171 (#189)
* Revert "fix merge conflict"

This reverts commit aff01c541b.

* Revert "Merge branch 'develop' into develop"

This reverts commit a8dd47cee5, reversing
changes made to 9e7bd51bcd.
2018-04-28 19:21:05 +01:00
Ilya Puchka 2cc5310d4b Merge pull request #182 from tapsandswipes/develop
Fix crash with colaborator containers and weak singletons
2018-02-26 13:25:17 +00:00
Antonio Cabezuelo Vivo 5cb03a11bb Fix crash with colaborator containers and weak singletons
Signed-off-by: Antonio Cabezuelo Vivo <antonio@tapsandswipes.com>
2018-02-12 14:17:31 +01:00
Ilya Puchka aff01c541b fix merge conflict 2017-12-12 16:56:23 +01:00
Ilya Puchka a8dd47cee5 Merge branch 'develop' into develop 2017-12-12 16:51:32 +01:00
Ilya Puchka 12738a665f Merge pull request #171 from creditkarma/parent_child_containers
Parent child container support for Dip
2017-12-12 16:33:32 +01:00
Oleksa 'trimm' Korin 9e7bd51bcd Restructured tests, fixed crash 2017-10-30 23:49:44 +02:00
Oleksa 'trimm' Korin ce394e7d2b Added reproduction test 2017-10-30 23:26:36 +02:00
Oleksa 'trimm' Korin cd2c66f4a8 Initial try for collaboration reproduction crash 2017-10-30 20:45:51 +02:00
Ilya Puchka 5464f00bad Merge pull request #177 from AliSoftware/release/6.0
Release 6.0
2017-10-09 18:12:39 +02:00
Ilya Puchka 5b9cc50190 Merge branch 'master' into release/6.0 2017-09-27 00:35:49 +02:00
Ilya Puchka a7c8616300 updated changelog 2017-09-27 00:34:57 +02:00
Ilya Puchka 8349fbd8d8 bump version to 6.0 2017-09-26 23:51:08 +02:00
Ilya Puchka 4b1ecbcb8e removed previously deprecated method 2017-09-26 23:50:56 +02:00
Ilya Puchka 2f0e1fdb10 Merge pull request #176 from AliSoftware/swift4
Swift 4 migration
2017-09-26 23:45:25 +02:00
Ilya Puchka 4a8a1daeca swift version settings update 2017-09-21 22:26:19 +02:00
Ilya Puchka 56554147cb travis update 2017-09-16 19:36:07 +02:00
Ilya Puchka a554454afe fixed failing tests 2017-09-16 19:24:15 +02:00
Ilya Puchka dde2e98953 project settings updated and warnings fixed 2017-09-16 18:52:58 +02:00
Ilya Puchka a2c04ed61e migrate tests 2017-09-16 18:39:12 +02:00
Ilya Puchka c64cf720b8 swift 4 migration 2017-09-16 18:04:36 +02:00
John Twigg 3429c6ae0b Parent child container support for Dip 2017-08-02 10:41:03 -07:00
Ilya Puchka 451fb03bc8 updated changelog 2017-07-30 12:31:06 +02:00
Ilya Puchka b1ad2a65b2 Merge pull request #169 from AliSoftware/fix-collaboration-sharing-singletons
Fix: containers with common collaborator share resolved instances for their own definitions
2017-07-30 12:19:34 +02:00
Ilya Puchka 3f8b83b87c addet test for peforming autowiring before collaboration 2017-07-29 18:16:54 +02:00
Ilya Puchka b7bf4904e9 fixed sharing singletons during collaboration 2017-07-29 18:04:28 +02:00
Ilya Puchka 199b1aec8f Merge pull request #156 from kandelvijaya/improvement/equatableIntoItsExtension
Added Equatable conformance on the site of declaration
2017-04-14 23:06:30 +02:00
Ilya Puchka 22d19697f1 Merge pull request #155 from kandelvijaya/patch-1
Playgrounds typo fixes
2017-04-14 23:05:22 +02:00
vkandel d809177273 moved equatable conformance into Hashable declaration site for DefinationKey 2017-04-09 21:41:10 +02:00
vkandel 420c866726 Added Equatable conformance on the site of declaration 2017-04-09 21:32:31 +02:00
Vijaya Prakash Kandel 6e6cc0b1df Merge pull request #1 from kandelvijaya/patch-2
Update Contents.swift
2017-04-09 21:04:19 +02:00
Vijaya Prakash Kandel b71f50cef5 Update Contents.swift 2017-04-09 21:03:56 +02:00
Vijaya Prakash Kandel 462d528474 Update Contents.swift 2017-04-09 21:02:00 +02:00
Ilya Puchka 01c318bd5c Merge branch 'release/5.1' into develop 2017-04-09 13:20:40 +02:00
Ilya Puchka a0c890c931 Merge pull request #152 from AliSoftware/release/5.1
Release 5.1
2017-04-09 13:18:56 +02:00
Ilya Puchka 9b2a7918d8 Merge pull request #154 from AliSoftware/auto-wiring-tag-fix
Fixed auto-wiring with tagged definitions
2017-04-09 13:08:04 +02:00
Ilya Puchka 218b5a4e9a disable running tests on watchos on Travis
https://github.com/travis-ci/travis-ci/issues/7580
2017-04-09 12:56:22 +02:00
Ilya Puchka 0b221f3368 fixed auto-wiring with tagged definitions 2017-04-09 12:08:48 +02:00
Ilya Puchka 3b943c10ef Merge branch 'master' into release/5.1 2017-04-06 22:51:21 +02:00
Ilya Puchka a1da60bd14 updated CHANGELOG, bumped version to 5.1 2017-04-06 22:49:05 +02:00
Ilya Puchka 1825cd7d4c Merge pull request #151 from AliSoftware/recursive-collaboration
Fixed collaboration shared references
2017-04-06 22:40:08 +02:00
Ilya Puchka e7d8fb41e1 fixed collaboration references, now collaboration is bidirectional 2017-04-06 21:46:08 +02:00
Ilya Puchka 1c398defeb Merge pull request #150 from AliSoftware/drop-swift2.3
Drop Swift 2.3
2017-04-06 10:49:51 +02:00
Ilya Puchka ab561546f0 allow spec warnings 2017-04-06 00:43:48 +02:00
Ilya Puchka c0fb925ab8 updated travis config to build for 3.0 and 3.1 on Linux 2017-04-06 00:00:22 +02:00
Ilya Puchka 80ee4865ce drop swift 2.3 support 2017-04-01 00:44:10 +02:00
Ilya Puchka d95df1343e minor changes for loging function 2017-03-30 19:43:11 +02:00
Ilya Puchka a83d866cbd Merge pull request #146 from Pr0Ger/develop
Enable use of custom or no logging function
2017-03-30 19:42:25 +02:00
Ilya Puchka cc1dcba4b9 Merge pull request #145 from DenHeadless/patch-1
Fix swift 3.1 warning
2017-03-30 19:15:19 +02:00
Sergey Petrov 0ceb6a3503 Enable use of custom or no logging function 2017-03-30 17:02:11 +03:00
Denys Telezhkin 4e641a6465 fix swift 3.1 warning 2017-03-30 15:14:27 +03:00
Ilya Puchka 35b6da8556 fixed method name inconsistency 2017-03-10 00:01:37 +01:00
Ilya Puchka 47bc1913e3 Merge pull request #141 from AliSoftware/swift-3.0.2
Swift 3.0.2
2017-01-23 23:24:02 +01:00
Ilya Puchka 24d341503e swift 3.0.2 2017-01-21 23:04:50 +01:00
Ilya Puchka 3c1331089b Merge branch 'hotfix/swift2.3-api-diff' into develop 2016-11-01 17:24:21 +03:00
Ilya Puchka 7af8957a01 Merge pull request #135 from AliSoftware/hotfix/swift2.3-api-diff
Hotfix - swift2.3 api diff
2016-11-01 18:23:04 +04:00
Ilya Puchka 6b68bea55d bumped version to 5.0.4 2016-11-01 14:52:37 +03:00
Ilya Puchka 5b2168ecef fixed broken swift 2.3 api 2016-11-01 14:47:33 +03:00
Ilya Puchka 5eb0eece56 Merge branch 'release/5.0.3' into develop 2016-10-23 23:54:58 +03:00
Ilya Puchka bdf4477774 Merge pull request #131 from AliSoftware/release/5.0.3
Release 5.0.3
2016-10-24 00:53:32 +04:00
Ilya Puchka 84573d967b fixed typo 2016-10-23 22:19:19 +03:00
Ilya Puchka 83511d601b Merge branch 'master' into release/5.0.3 2016-10-21 22:01:23 +04:00
Ilya Puchka b24734e1c9 fixed access levels 2016-10-13 00:04:31 +02:00
Ilya Puchka ab9abbe7ab bumped version to 5.0.3 2016-10-12 23:54:30 +02:00
Ilya Puchka 298cf83be3 updated README and CHANGELOG 2016-10-12 23:54:30 +02:00
Ilya Puchka 455456e817 Merge pull request #127 from AliSoftware/feature/swift-compatibility
Swift 2.3 compatibility
2016-10-12 22:59:02 +02:00
Ilya Puchka 905cbdadb0 swift 2.3 compatibility 2016-10-12 20:47:33 +02:00
Ilya Puchka 945caa451a sligtly improved logging 2016-10-11 13:40:10 +02:00
Ilya Puchka bfacca6fd0 Merge pull request #129 from AliSoftware/features/fixed-reusing-released-weak-singletons
Fixed reusing instances for weak singletons
2016-10-10 15:08:03 +02:00
Ilya Puchka 21673d1f4d added some tests 2016-10-10 15:07:22 +02:00
Ilya Puchka 65175fa372 fixed reusing instances for weak singletons when underlying instance was already released 2016-10-10 14:02:11 +02:00
Ilya Puchka 99fb4ea081 Merge branch 'release/5.0.2' into develop 2016-10-09 11:34:29 +02:00
Ilya Puchka a1ece4b0ab Merge pull request #126 from AliSoftware/release/5.0.2
Release 5.0.2
2016-10-09 11:33:26 +02:00
Ilya Puchka 0b00e13e00 added hasPrefix method for Linux 2016-10-09 00:34:11 +02:00
Ilya Puchka 4aec626a1f bumped version to 5.0.2 2016-10-08 00:28:05 +02:00
Ilya Puchka d477392deb building against 3.0-RELEASE on Travis on Linux 2016-10-08 00:27:51 +02:00
Ilya Puchka 3b23dc1a6f updated README and CHANGELOG 2016-10-08 00:27:08 +02:00
Ilya Puchka 1d7d0052bf fixed logging 2016-10-07 22:47:07 +02:00
Ilya Puchka b1fee5c1db Merge pull request #125 from AliSoftware/features/swift-3-fixes
Fixed Swift 3 issues
2016-10-07 22:13:41 +02:00
Ilya Puchka e19b65d9c2 fixed issues with reflection of objectes with IUO properties 2016-10-07 19:19:05 +02:00
Ilya Puchka 86bb28da14 Merge branch 'hotfix/pod-fix' into develop 2016-09-16 12:13:30 +02:00
Ilya Puchka e4545e3ed3 Merge pull request #124 from AliSoftware/hotfix/pod-fix
Pod hotfix
2016-09-16 12:13:03 +02:00
Ilya Puchka 52455e663c bumped version to 5.0.1 2016-09-15 01:37:25 +02:00
Ilya Puchka 7a10d1e46e fixed linting podspec 2016-09-15 01:00:34 +02:00
Ilya Puchka e95df85503 Update README.md 2016-09-11 21:18:03 +02:00
Ilya Puchka a028c05271 Merge branch 'release/5.0.0' into develop 2016-09-11 20:48:56 +02:00
Ilya Puchka e18a4b131f Merge pull request #123 from AliSoftware/release/5.0.0
Release 5.0.0
2016-09-11 20:46:31 +02:00
Ilya Puchka e2799279ab Updated README 2016-09-11 20:23:40 +02:00
Ilya Puchka ffb61be63c Merge branch 'master' into release/5.0.0 2016-09-11 19:29:20 +02:00
Ilya Puchka 0070522341 bumped version to 5.0.0 2016-09-11 19:12:14 +02:00
Ilya Puchka 6842a016bf updated CHANGELOG 2016-09-11 19:12:14 +02:00
Ilya Puchka 6a6c4a358e lowercased DipError enum cases 2016-09-11 11:29:44 +02:00
Ilya Puchka 734e67f124 playground fix 2016-09-11 11:15:28 +02:00
Ilya Puchka aa46ef79b9 removed deprecated APIs 2016-09-11 11:12:14 +02:00
Ilya Puchka 2d177b7aea lowercased ComponentScope enum cases 2016-09-11 11:07:01 +02:00
Ilya Puchka 3ada18d756 Swift 3 (#120)
Swift 3 migration
2016-09-11 11:01:02 +02:00
Ilya Puchka 2441f096d6 Merge branch 'feature/single_target' into develop 2016-09-11 02:28:52 +02:00
Ilya Puchka f9f2777474 single target 2016-09-11 02:25:27 +02:00
Ilya Puchka 1dca889730 Merge pull request #121 from AliSoftware/feature/public-methods-renamed
Some public methods renamed
2016-09-11 02:05:15 +02:00
Ilya Puchka 86b23d7260 some public methods renamed 2016-09-11 01:48:57 +02:00
Ilya Puchka 85897e562d minor playground docs fixes 2016-09-11 00:38:15 +02:00
Ilya Puchka cdb5f1b901 Merge pull request #118 from AliSoftware/feature/invalid-type-error
Added error when resolved instance has mismatched type
2016-09-10 00:10:26 +02:00
Ilya Puchka 03903931f2 Merge pull request #117 from AliSoftware/feature/simplified-auto-wiring
Simplified auto-wiring
2016-09-06 23:23:23 +02:00
Ilya Puchka 5016ce860c added error for resolving instance with mismatched type 2016-09-06 01:25:16 +02:00
Ilya Puchka 8da6be8cad simplified auto-wiring 2016-09-06 00:40:59 +02:00
Ilya Puchka 873347aed0 fixed constructing context when resolving with containers collaboration 2016-08-29 23:25:02 +02:00
Ilya Puchka 637ff1f916 made Resolvable extension public 2016-08-29 00:41:14 +02:00
Ilya Puchka c8ddfad188 removed unneeded print 2016-08-27 22:35:19 +02:00
Ilya Puchka 22d73210d0 Merge pull request #116 from AliSoftware/feature/inheritance
Properly handling inheritance
2016-08-27 22:31:16 +02:00
Ilya Puchka feeb7b424a added resolveDependencies(_:DependencyContainer) method in Resolvable 2016-08-27 22:05:21 +02:00
Ilya Puchka a6d2a1b1ab fixed resolving inherited auto-injected dependencies 2016-08-27 21:47:10 +02:00
Ilya Puchka fa400b9a50 added missing precondition in implements method 2016-08-27 19:36:27 +02:00
Ilya Puchka 771c993d78 Merge pull request #114 from AliSoftware/feature/type-forwarding-improvements
Improved type forwarding
2016-08-27 00:25:59 +02:00
Ilya Puchka 10d2560b57 Merge pull request #115 from AliSoftware/feature/registering-explicit-type
Optional type parameter in register methods
2016-08-26 22:53:17 +02:00
Ilya Puchka 2887b78944 added optional type parameter to register methods 2016-08-26 21:40:33 +02:00
Ilya Puchka ed6d45e372 Update README.md 2016-08-26 01:39:25 +02:00
Ilya Puchka 7654737a34 Update README.md 2016-08-26 01:35:47 +02:00
Ilya Puchka f1d35a6a96 improved type forwarding
- removed deprecation annotation for register(_:type:tag:)
- added removing previously registered definition when overriding (for proper cleanup)
- fixed calling resolvingProperties set after registering type-forwarding
- improved tests and code comments
2016-08-26 00:38:01 +02:00
Ilya Puchka 0b355e0fbe some internal methods renaming 2016-08-26 00:34:09 +02:00
Ilya Puchka 9a8e189ce7 fixed playground 2016-08-21 17:44:44 +02:00
Ilya Puchka 53ff00f00d Merge pull request #113 from AliSoftware/feature/definition-refactoring
Definition refactoring
2016-08-21 00:47:05 +02:00
Ilya Puchka 1c18eab371 type forwarding with implements method 2016-08-21 00:11:10 +02:00
Ilya Puchka cd47d888aa renamed DefinitionOf to Definition and changed generic parameter type 2016-08-21 00:11:10 +02:00
Ilya Puchka 499b0c1e38 Merge pull request #112 from ilyapuchka/feature/shared-scope-as-default
Made shared scope a default argument value for register methods
2016-08-21 00:09:28 +02:00
Ilya Puchka 6536a2182d Made shared scope a default argument value for register methods 2016-08-20 23:41:13 +02:00
Ilya Puchka e2ae164d3e fixed crashing in DipUI (SR-680) 2016-08-20 02:12:31 +02:00
Ilya Puchka 092afd8a8b Update README.md 2016-08-20 00:58:06 +02:00
Ilya Puchka a7985f949a Merge branch 'release/4.6.1' into develop 2016-08-14 22:32:17 +02:00
Ilya Puchka 967e1a43cb Merge pull request #111 from AliSoftware/release/4.6.1
Release 4.6.1
2016-08-14 22:31:48 +02:00
Ilya Puchka 73225ddd10 bumped version to 4.6.1, updated README and CHANGELOG 2016-08-14 21:41:45 +02:00
Ilya Puchka 2d238265ca Merge pull request #105 from AliSoftware/feature/grand-renaming
Grand renaming
2016-08-14 20:56:26 +02:00
Ilya Puchka cdfbf3dbfe Update README.md 2016-08-07 00:26:51 +02:00
Ilya Puchka 21a3b43d9b renamed resolveDependencies to resolingProperities 2016-07-28 23:58:24 +02:00
Ilya Puchka 8f0f49e790 renamed withArguments to arguments 2016-07-28 23:35:26 +02:00
Ilya Puchka 7c5966ab61 switched tag and scope parameters in register method 2016-07-28 23:35:26 +02:00
Ilya Puchka c41696cd23 some internal methods renaming 2016-07-28 23:35:26 +02:00
Ilya Puchka 6e7a168fb7 renamed properties of DefinitionKey 2016-07-28 23:35:26 +02:00
Ilya Puchka a2efe0abff renamed Prototype and ObjectGraph scopes 2016-07-28 23:35:26 +02:00
Ilya Puchka d876389c19 Update README.md 2016-07-27 23:05:43 +02:00
Ilya Puchka 5c54e07219 Merge pull request #104 from AliSoftware/feature/config-retain-cycle
Some notes about avoiding retain cycle when configuring container
2016-07-27 22:29:48 +02:00
Ilya Puchka 0e32a79d8c added some notes about avoiding retain cycle when configuring container 2016-07-27 22:02:52 +02:00
Ilya Puchka 37c8b3d03d Merge pull request #103 from AliSoftware/feature/shared-singletons
Sharing singletons between collaborating containers
2016-07-27 21:14:54 +02:00
Ilya Puchka 21ea965607 fixed releasing weak singletons when resetting definition 2016-07-27 20:41:37 +02:00
Ilya Puchka 80d8b1ba99 fixed sharing singletons between collaborators 2016-07-27 20:41:37 +02:00
Ilya Puchka 975041933e Update README.md 2016-07-27 02:18:36 +02:00
Ilya Puchka 404b65b633 Update README.md 2016-07-27 02:16:07 +02:00
Ilya Puchka 73a78ed5a0 Merge branch 'release/4.6.0' into develop 2016-07-17 21:59:32 +02:00
Ilya Puchka 2371338e9b Merge pull request #100 from AliSoftware/release/4.6.0
Release 4.6.0
2016-07-17 21:58:33 +02:00
Ilya Puchka 65f4b0a4ab bumped version to 4.6.0 2016-07-17 21:48:13 +02:00
Ilya Puchka 2c7444fef8 updated CHANGELOG and README 2016-07-17 21:47:32 +02:00
Ilya Puchka daad001e17 fixed building on linux 2016-07-17 15:38:07 +02:00
Ilya Puchka 11ed3c975a Merge pull request #96 from AliSoftware/feature/weak-singletons
WeakSingleton scope
2016-07-17 15:36:38 +02:00
Ilya Puchka 02e0877fcb weak singleton scope 2016-07-17 15:24:05 +02:00
Ilya Puchka 54844f8459 improved comment explaining need of manual unboxing of optionals 2016-07-17 15:23:21 +02:00
Ilya Puchka fe006c4f13 Merge pull request #99 from AliSoftware/feature/improved-logging
Improved logging
2016-07-17 15:22:54 +02:00
Ilya Puchka 8e0e165aa4 Merge pull request #98 from AliSoftware/feature/fix-updating-context
Fix updating context
2016-07-17 15:07:57 +02:00
Ilya Puchka 0319bcff97 improved logging 2016-07-17 15:00:36 +02:00
Ilya Puchka ffdfdb0826 fix build error 2016-07-17 14:53:33 +02:00
Ilya Puchka c0884deddd simplified resolving with collaborator 2016-07-17 14:11:56 +02:00
Ilya Puchka 23318bbb7b Merge pull request #97 from AliSoftware/feature/auto-injecting-properties-first
Auto-injecting properties before calling resolveDependencies block
2016-07-17 13:04:50 +02:00
Ilya Puchka 2800c84553 fixed updating context when auto-injecting and resolving with collaborator 2016-07-17 12:51:13 +02:00
Ilya Puchka 18087d67c9 auto-injecting properties before calling resolve dependencies block 2016-07-17 12:27:20 +02:00
Ilya Puchka ab235c23e9 accessing previously resolved instances with subscript 2016-07-17 12:26:54 +02:00
Ilya Puchka c82dfb3dbe Merge pull request #95 from AliSoftware/feature/collaborating-containers
Containers collaboration
2016-07-12 00:07:09 +02:00
Ilya Puchka 2865a07e5a removed underscore prefix from internal methods names 2016-07-11 01:54:13 +02:00
Ilya Puchka ac5999011d filter matching definitions by arguments type 2016-07-11 01:54:13 +02:00
Ilya Puchka cd92deeccd added playground page for containers collaboration 2016-07-11 01:54:13 +02:00
Ilya Puchka 4f1dbd1e48 added context property to disable logging 2016-07-11 01:54:13 +02:00
Ilya Puchka 51e95fe73c improved collaborating containers implementation 2016-07-11 01:54:13 +02:00
Ilya Puchka aaab404d45 containers collaboration 2016-07-04 15:13:10 +02:00
Ilya Puchka 55da6f3707 Merge pull request #94 from AliSoftware/feature/logging
Logging all errors
2016-07-04 10:45:49 +02:00
Ilya Puchka e24d4cb577 logging all errors 2016-07-04 10:30:54 +02:00
Ilya Puchka b733c74e34 Fixed wiki links 2016-07-04 02:19:58 +02:00
Ilya Puchka 5927ab9471 Added link to VIPER example using Dip in README 2016-07-02 20:38:45 +02:00
Ilya Puchka 86a7289d2c Merge pull request #92 from mwoollard/develop
Enable the 'allow app extension api only' option
2016-06-13 00:15:44 +04:00
Mark Woollard 2c85a01f0d Enable the 'allow app extension api only' option
Dip doesn't appear to use any API calls that are unavailable to app extensions / watch kit. Enabling this flag removes warnings in extension / watch kit projects that link against dip.
2016-06-10 08:07:50 +01:00
Ilya Puchka bb85e27db8 Merge branch 'release/4.5.0' into develop 2016-06-08 23:58:37 +03:00
Ilya Puchka eadd87a5bb Merge pull request #90 from AliSoftware/release/4.5.0
Release 4.5.0
2016-06-08 22:57:58 +02:00
Ilya Puchka 4c4b9ef57a bumped version to 4.5.0 2016-06-08 23:45:01 +03:00
Ilya Puchka ee99cf4fdc updated CHANGELOG and README 2016-06-08 23:44:50 +03:00
Ilya Puchka 40dfd6419f improved tests playground page 2016-06-08 23:44:25 +03:00
Ilya Puchka 88fa94ed30 Merge pull request #89 from AliSoftware/feature/type-forwarding
Type forwarding
2016-06-08 21:21:12 +02:00
Ilya Puchka 58d1813681 sources layout fixed 2016-06-05 21:39:46 +02:00
Ilya Puchka 7277b940ca type forwarding playground page 2016-06-05 21:39:46 +02:00
Ilya Puchka 581c241fd1 improved type-forwarding implementation 2016-06-05 17:23:45 +02:00
Ilya Puchka 3c9d2a6db8 type forwarding 2016-05-22 17:17:57 +02:00
Ilya Puchka 10309c1d5d incremented swift snapshot version 2016-05-22 17:17:36 +02:00
Ilya Puchka 2a27152fa1 skip loading SPM 2016-05-22 17:16:57 +02:00
Ilya Puchka e04a741479 fixed travis script 2016-05-22 16:23:15 +02:00
Ilya Puchka dbaba50c88 fixed travis script indentation 2016-05-22 16:18:03 +02:00
Ilya Puchka 032db2d2b1 fixed travis scrip 2016-05-22 16:16:56 +02:00
Ilya Puchka b2456e0430 fixed cloning spm in travis script 2016-05-22 16:08:31 +02:00
Ilya Puchka fd17075a8a some minor docs changes 2016-05-22 15:55:31 +02:00
Ilya Puchka 2d9be9acc0 Merge pull request #87 from AliSoftware/feature/validate-container
Validating container
2016-05-13 16:16:30 +02:00
Ilya Puchka efd279b26b validate method 2016-05-12 22:28:29 +02:00
Ilya Puchka b5cb796f5f Merge pull request #88 from AliSoftware/linux-test-update
Linux test update
2016-05-06 00:20:41 +02:00
Ilya Puchka e1fd904842 added linux step on travis 2016-05-05 21:26:39 +02:00
Ilya Puchka c65ae6cedd adopted xctest api changes 2016-05-05 17:27:15 +02:00
Ilya Puchka 21aedcb655 minor fix 2016-05-05 16:43:12 +02:00
Ilya Puchka 37d0e4750f Merge pull request #86 from phatblat/phatblat-patch-1
Fix typo on readme
2016-05-01 18:51:25 +02:00
Ben Chatelain 95157a99f6 Fix typo on readme 2016-05-01 09:47:20 -06:00
Ilya Puchka 1ad3aa2c18 Merge pull request #85 from AliSoftware/feature/auto-wiring-error
Auto wiring error
2016-05-01 15:10:27 +02:00
Ilya Puchka 1c916cbafc Added AutoWiringFailed error 2016-05-01 14:31:27 +02:00
Ilya Puchka 86b6afe5d9 Merge pull request #84 from AliSoftware/feature/resolving-optionals
Resolving optionals
2016-05-01 14:22:56 +02:00
Ilya Puchka a1e7a1f958 resolving optionals 2016-05-01 12:39:02 +02:00
Ilya Puchka c414882c6a Merge pull request #83 from AliSoftware/feature/container-context
Container context
2016-04-29 10:22:00 +02:00
Ilya Puchka f8cd1c4dc3 container context implementation 2016-04-27 14:59:23 +02:00
Ilya Puchka 3e68acd37b settable auto-injected property without calling didInject observer 2016-04-25 22:51:34 +02:00
Ilya Puchka 9b047e4145 Merge pull request #81 from AliSoftware/feature/settable-auto-injected-properties
Settable auto-injected properties
2016-04-25 22:43:28 +02:00
Ilya Puchka f31236688d settable auto-injected properties 2016-04-25 14:10:25 +02:00
Ilya Puchka af61fea49e Merge pull request #79 from AliSoftware/feature/weakly-typed-resolve
Weakly typed resolve
2016-04-21 22:42:14 +02:00
Ilya Puchka 8c9172aac9 Merge pull request #74 from AliSoftware/docs
Cleaned up README
2016-04-21 22:41:59 +02:00
Ilya Puchka 5495bd2b97 removed description that encouraged service locator anti-pattern from podspec 2016-04-21 22:20:43 +02:00
Ilya Puchka b3e6c3c61a Cleaned up README 2016-04-21 22:19:54 +02:00
Ilya Puchka be86b30da3 weakly typed resolve 2016-04-20 01:54:25 +02:00
Ilya Puchka 26ba11dcbf Merge branch 'release/4.4.0' into develop 2016-03-31 22:57:29 +02:00
Ilya Puchka 1356a8056f Merge pull request #70 from ilyapuchka/release/4.4.0
Release 4.4.0
2016-03-31 22:55:38 +02:00
Ilya Puchka 0ddb37bea3 Bumped version to 4.4.0 2016-03-31 22:39:00 +02:00
Ilya Puchka 537cad5923 updated docs 2016-03-31 22:37:53 +02:00
Ilya Puchka 0c4ce2213b bootstrap method can throw 2016-03-31 22:19:29 +02:00
Ilya Puchka a91dacb29c Merge pull request #65 from ilyapuchka/feature/eager-singleton-scope
EagerSingleton scope and bootstrap method
2016-03-31 10:03:59 +02:00
Ilya Puchka 895a6f2583 Merge pull request #67 from ilyapuchka/feature/resolvable-calls-order
Reversed order of Resolvable callback calls
2016-03-31 10:03:52 +02:00
Ilya Puchka 73f71a99b2 Reversed order of Resolvable callback calls 2016-03-30 21:56:45 +02:00
Ilya Puchka 41664914f4 EagerSingleton scope and bootstrap method 2016-03-25 10:24:49 +01:00
Ilya Puchka 53bc97ba63 Update README.md
Added some fancy badges
2016-03-24 14:34:38 +01:00
Ilya Puchka c321189b66 Merge branch 'release/4.3.1' into develop 2016-03-24 12:24:05 +01:00
103 changed files with 7106 additions and 3771 deletions
+1
View File
@@ -36,3 +36,4 @@ Carthage
# SPM
.build/
Packages
.swiftpm
+56 -18
View File
@@ -1,19 +1,57 @@
language: objective-c
osx_image: xcode7.3
matrix:
allow_failures:
- os: linux
include:
- script:
- set -o pipefail && xcodebuild test -workspace Dip.xcworkspace -scheme Dip -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6,OS=latest' ONLY_ACTIVE_ARCH=NO | xcpretty -c
- set -o pipefail && xcodebuild test -workspace Dip.xcworkspace -scheme Dip -sdk macosx -destination 'platform=macOS,arch=x86_64' ONLY_ACTIVE_ARCH=NO | xcpretty -c
# - pod spec lint --allow-warnings
- carthage build --no-skip-current
- swift package clean && swift build && swift test
os: osx
osx_image: xcode10.2
language: objective-c
before_install:
- gem install cocoapods --version 1.8.4 --no-document
- script:
- set -o pipefail && xcodebuild test -workspace Dip.xcworkspace -scheme Dip -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 11,OS=latest' ONLY_ACTIVE_ARCH=NO | xcpretty -c
- set -o pipefail && xcodebuild test -workspace Dip.xcworkspace -scheme Dip -sdk macosx -destination 'platform=macOS,arch=x86_64' ONLY_ACTIVE_ARCH=NO | xcpretty -c
# - pod spec lint --allow-warnings
- carthage build --no-skip-current
- swift package clean && swift build && swift test
os: osx
osx_image: xcode11.2
language: objective-c
before_install:
- gem install cocoapods --version 1.8.4 --no-document
- script:
- swift package clean && swift build && swift test
os: linux
dist: trusty
sudo: required
language: generic
before_install:
- wget -q -O - https://swift.org/keys/all-keys.asc | gpg --import -
- cd ..
- export SWIFT_VERSION=swift-5.1-RELEASE
- wget https://swift.org/builds/swift-5.1-release/ubuntu1404/$SWIFT_VERSION/$SWIFT_VERSION-ubuntu14.04.tar.gz
- tar xzf $SWIFT_VERSION-ubuntu14.04.tar.gz
- export PATH="${PWD}/${SWIFT_VERSION}-ubuntu14.04/usr/bin:${PATH}"
- cd Dip
- script:
- swift package clean && swift build && swift test
os: linux
dist: trusty
sudo: required
language: generic
before_install:
- wget -q -O - https://swift.org/keys/all-keys.asc | gpg --import -
- cd ..
- export SWIFT_VERSION=swift-5.0-RELEASE
- wget https://swift.org/builds/swift-5.0-release/ubuntu1404/$SWIFT_VERSION/$SWIFT_VERSION-ubuntu14.04.tar.gz
- tar xzf $SWIFT_VERSION-ubuntu14.04.tar.gz
- export PATH="${PWD}/${SWIFT_VERSION}-ubuntu14.04/usr/bin:${PATH}"
- cd Dip
# cache: cocoapods
# before_install:
# - gem install cocoapods # Since Travis is not always on latest version
install:
- gem install xcpretty --no-rdoc --no-ri --no-document --quiet
- curl -OlL "https://github.com/Carthage/Carthage/releases/download/0.11/Carthage.pkg" && sudo installer -pkg "Carthage.pkg" -target / && rm "Carthage.pkg"
script:
- set -o pipefail && xcodebuild test -workspace Dip.xcworkspace -scheme Dip-iOS -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6,OS=latest' ONLY_ACTIVE_ARCH=NO | xcpretty -c
- set -o pipefail && xcodebuild test -workspace Dip.xcworkspace -scheme Dip-OSX -sdk macosx -destination 'platform=OS X,arch=x86_64' ONLY_ACTIVE_ARCH=NO | xcpretty -c
- set -o pipefail && xcodebuild test -workspace Dip.xcworkspace -scheme Dip-tvOS -sdk appletvsimulator -destination 'platform=tvOS Simulator,name=Apple TV 1080p,OS=latest' ONLY_ACTIVE_ARCH=NO | xcpretty -c
- set -o pipefail && xcodebuild -workspace Dip.xcworkspace -scheme Dip-watchOS -sdk watchsimulator -destination 'platform=watchOS Simulator,name=Apple Watch - 38mm,OS=latest' ONLY_ACTIVE_ARCH=NO | xcpretty - c
- set -o pipefail && xcodebuild test -workspace Dip.xcworkspace -scheme DipSampleApp -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6,OS=latest' ONLY_ACTIVE_ARCH=NO | xcpretty -c
- pod lib lint --quick
- carthage build --no-skip-current
notifications:
email: false
+160 -2
View File
@@ -1,8 +1,166 @@
# CHANGELOG
## Develop
## 7.1.1
* Fixed using `StoryboardInstantiatable` with SPM ([#233](https://github.com/AliSoftware/Dip/pull/233)).
## 7.1.0
* You can now use a shorthand syntax for resolving a single property using a key path, i.e. `resolvingProperty(\.value)`.
* Swift 5.0 support ([#224](https://github.com/AliSoftware/Dip/pull/224)).
* Fixed resolving nested types with the same local names ([#221](https://github.com/AliSoftware/Dip/pull/221)).
* `@Injected` and `@IntectedWeak` property wrappers ([#225](https://github.com/AliSoftware/Dip/pull/225)).
* Thread safety can be disabled on container level.
## 7.0.1
* Added a workaround for Swift 4.2 regression related to retaining weak properties ([#214](https://github.com/AliSoftware/Dip/issues/214)).
For that auto-injection can be disabled or enabled for the whole container or individula registrations.
## 7.0.0
* Swift 4.2 support.
* Fixed some issues when reusing instances previously resolved as optionals.
* Dip-UI is now part of Dip.
## 6.1
* Swift 4.1 support.
* Fixed crashes resolving singletons using collaborating containers.
[#179](https://github.com/AliSoftware/Dip/pull/179), [@trimmurrti](https://github.com/trimmurrti)
[#182](https://github.com/AliSoftware/Dip/pull/182), [@tapsandswipes](https://github.com/tapsandswipes)
## 6.0
* Swift 4 support
* Fixed unneeded reuse of singletons in collaborating containers.
Containers now first attempt to autowire components and fallback to collaboration when it fails.
[#169](https://github.com/AliSoftware/Dip/issues/169), [@ilyapuchka](https://github.com/ilyapuchka)
## 5.1
* Dropped Swift 2.3 support.
[#150](https://github.com/AliSoftware/Dip/issues/150), [@ilyapuchka](https://github.com/ilyapuchka)
* Added custom logging function.
[#146](https://github.com/AliSoftware/Dip/issues/146), [@Pr0Ger](https://github.com/Pr0Ger)
#### Fixed
* Fixed Swift 3.1 warnings.
[#145](https://github.com/AliSoftware/Dip/issues/145), [@DenHeadless](https://github.com/DenHeadless)
* Fixed collaboration shared references.
[#151](https://github.com/AliSoftware/Dip/issues/151), [@ilyapuchka](https://github.com/ilyapuchka)
* Fixed autowiring when using tags.
[#154](https://github.com/AliSoftware/Dip/issues/154), [@ilyapuchka](https://github.com/ilyapuchka)
## 5.0.4
#### Fixed
* Fixed broken compatibility for Swift 2.3 API in `resolve(tag:arguments:)` method.
[#135](https://github.com/AliSoftware/Dip/issues/135), [@ilyapuchka](https://github.com/ilyapuchka)
## 5.0.3
* Added Swift 2.3 compatibility. `swift2.3` brunch is no longer maintained.
[#127](https://github.com/AliSoftware/Dip/issues/127), [@ilyapuchka](https://github.com/ilyapuchka)
#### Fixed
* Fixed reusing instances registered with `WeakSingleton` scope
[#129](https://github.com/AliSoftware/Dip/issues/129), [@ilyapuchka](https://github.com/ilyapuchka)
## 5.0.2
#### Fixed
* Fixed Swift 3 issues related to reflection and IUO
[#125](https://github.com/AliSoftware/Dip/issues/125), [@ilyapuchka](https://github.com/ilyapuchka)
## 5.0.1
This release is the same as 5.0.0 and only fixes CocoaPods speck pushed to trunk without macOS, tvOS and watchOS deployment targets. Please use this release instead of 5.0.0 if you integrate Dip via Cocoapods.
## 5.0.0
* Migrated to Swift 3.0
[#120](https://github.com/AliSoftware/Dip/issues/120), [@patrick-lind](https://github.com/patrick-lind), [@mark-urbanthings](https://github.com/mark-urbanthings), [@ilyapuchka](https://github.com/ilyapuchka)
* Renamed `DefinitionOf` to `Definition` and some other source-breaking refactoring.
[#113](https://github.com/AliSoftware/Dip/issues/113), [@ilyapuchka](https://github.com/ilyapuchka)
* Added `invalidType` error when resolved instance does not implement requested type.
[#118](https://github.com/AliSoftware/Dip/issues/118), [@ilyapuchka](https://github.com/ilyapuchka)
* Added optional `type` parameter in register methods to be able to specify type when registering using method literal instead of closure.
[#115](https://github.com/AliSoftware/Dip/issues/115), [@ilyapuchka](https://github.com/ilyapuchka)
* Added `implements` family of methods in to `Definition` to register type-forwarding definitions.
[#114](https://github.com/AliSoftware/Dip/issues/114), [@ilyapuchka](https://github.com/ilyapuchka)
* Shared scope is now the default scope.
[#112](https://github.com/AliSoftware/Dip/issues/112), [@ilyapuchka](https://github.com/ilyapuchka)
* Single target project setup.
[#121](https://github.com/AliSoftware/Dip/issues/121), [@ilyapuchka](https://github.com/ilyapuchka)
* Simplified implementation of auto-wiring.
[#117](https://github.com/AliSoftware/Dip/issues/117), [@ilyapuchka](https://github.com/ilyapuchka)
#### Fixed
* Auto-injected properties inherited from super class are now properly injected when resolving subclass.
Added `resolveDependencies(_:DependencyContainer)` method to `Resolvable` protocol to handle inheritance when resolving.
[#116](https://github.com/AliSoftware/Dip/issues/116), [@ilyapuchka](https://github.com/ilyapuchka)
## 4.6.1
#### Fixed
* Fixed sharing singletons between collaborating containers.
[#103](https://github.com/AliSoftware/Dip/issues/103), [@ilyapuchka](https://github.com/ilyapuchka)
* Renamed some public API's (see release notes for more info).
[#105](https://github.com/AliSoftware/Dip/issues/105), [@ilyapuchka](https://github.com/ilyapuchka)
## 4.6.0
* Containers collaboration. Break your definitions in modules and link them together.
[#95](https://github.com/AliSoftware/Dip/pull/95), [@ilyapuchka](https://github.com/ilyapuchka)
* Added WeakSingleton scope.
[#96](https://github.com/AliSoftware/Dip/pull/96), [@ilyapuchka](https://github.com/ilyapuchka)
* Properties Auto-injection now is performed before calling `resolveDependencies` block
[#97](https://github.com/AliSoftware/Dip/pull/97), [@ilyapuchka](https://github.com/ilyapuchka)
* Fixed updating container's context when resolving properties with auto-injection.
[#98](https://github.com/AliSoftware/Dip/pull/98), [@ilyapuchka](https://github.com/ilyapuchka)
* Improved logging.
[#94](https://github.com/AliSoftware/Dip/pull/94), [#99](https://github.com/AliSoftware/Dip/pull/99), [@ilyapuchka](https://github.com/ilyapuchka)
* Fixed warning about using only extensions api.
[#92](https://github.com/AliSoftware/Dip/pull/92), [@mwoollard](https://github.com/mwoollard)
## 4.5.0
* Added weakly-typed API to resolve components when exact type is unknown during compile time.
[#79](https://github.com/AliSoftware/Dip/pull/79), [@ilyapuchka](https://github.com/ilyapuchka)
* Added type forwarding feature. You can register the same factory to resolve different types.
[#89](https://github.com/AliSoftware/Dip/pull/89), [@ilyapuchka](https://github.com/ilyapuchka)
* Container now can resolve optional types :tada:
[#84](https://github.com/AliSoftware/Dip/pull/84), [@ilyapuchka](https://github.com/ilyapuchka)
* Added container context that provides contextual information during graph resolution process.
[#83](https://github.com/AliSoftware/Dip/pull/83), [@ilyapuchka](https://github.com/ilyapuchka)
* Added method to validate container configuration.
[#87](https://github.com/AliSoftware/Dip/pull/87), [@ilyapuchka](https://github.com/ilyapuchka)
* Added method to manually set value wrapped by auto-injection wrappers.
[#81](https://github.com/AliSoftware/Dip/pull/81), [@ilyapuchka](https://github.com/ilyapuchka)
* Added separate error type for failures during auto-wiring.
[#85](https://github.com/AliSoftware/Dip/pull/85), [@ilyapuchka](https://github.com/ilyapuchka)
## 4.4.0
* Added `.EagerSingleton` scope for objectes requiring early instantiation and `bootstrap()` method on `DepenencyContainer`.
[#65](https://github.com/AliSoftware/Dip/pull/65), [@ilyapuchka](https://github.com/ilyapuchka)
* Reverted order of `Resolvable` callbacks.
[#67](https://github.com/AliSoftware/Dip/pull/67), [@ilyapuchka](https://github.com/ilyapuchka)
## 4.3.1
* Fix Swift 2.2 compile errors in tests
* Fix Swift 2.2 compile errors in tests.
[#62](https://github.com/AliSoftware/Dip/pull/62), [@mwoollard](https://github.com/mwoollard)
## 4.3.0
@@ -106,7 +264,7 @@
This code:
```swift
container.register("some tag") { SomeClass() as SomeProtocol }
container.register(tag: "some tag") { SomeClass() as SomeProtocol }
container.resolve("some tag") as SomeProtocol
```
+9 -15
View File
@@ -1,25 +1,17 @@
Pod::Spec.new do |s|
s.name = "Dip"
s.version = "4.3.1"
s.summary = "A simple Dependency Resolver: Dependency Injection using Protocol resolution."
s.version = "7.1.1"
s.summary = "Dependency Injection for Swift made easy."
s.description = <<-DESC
Dip is a Swift framework to manage your Dependencies between your classes
in your app using Dependency Injection.
It's aimed to be very simple to use while improving testability
of your app by allowing you to get rid of those sharedInstances and instead
inject values based on protocol resolution.
Define your API using a protocol, then ask Dip to resolve this protocol into
an instance dynamically in your classes. Then your App and your Tests can be
configured to resolve the protocol using a different instance or class so this
improve testability by decoupling the API and the concrete class used to implement it.
DESC
Dip is a Swift Dependency Injection Container.
It provides reusable functionality for managing dependencies of your types
and will help you to wire up different parts of your app.
DESC
s.homepage = "https://github.com/AliSoftware/Dip"
s.license = 'MIT'
s.authors = { "Olivier Halligon" => "olivier@halligon.net", "Ilya Puchka" => "ilya@puchka.me" }
s.authors = { "Olivier Halligon" => "olivier@halligon.net", "Ilya Puchka" => "ilyapuchka@gmail.com" }
s.source = { :git => "https://github.com/AliSoftware/Dip.git", :tag => s.version.to_s }
s.social_media_url = 'https://twitter.com/aligatr'
@@ -31,4 +23,6 @@ Pod::Spec.new do |s|
s.requires_arc = true
s.source_files = 'Sources/**/*.swift'
s.swift_version = "5.0", "5.1"
end
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
File diff suppressed because it is too large Load Diff
@@ -1,100 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0710"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B3771C1615EC002241C1"
BuildableName = "Dip.framework"
BlueprintName = "Dip-iOS"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B3801C1615EC002241C1"
BuildableName = "Dip-iOSTests.xctest"
BlueprintName = "Dip-iOSTests"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B3771C1615EC002241C1"
BuildableName = "Dip.framework"
BlueprintName = "Dip-iOS"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B3771C1615EC002241C1"
BuildableName = "Dip.framework"
BlueprintName = "Dip-iOS"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B3771C1615EC002241C1"
BuildableName = "Dip.framework"
BlueprintName = "Dip-iOS"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
@@ -1,99 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0710"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B3A51C1618AF002241C1"
BuildableName = "Dip.framework"
BlueprintName = "Dip-tvOS"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B3AE1C1618AF002241C1"
BuildableName = "Dip-tvOSTests.xctest"
BlueprintName = "Dip-tvOSTests"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B3A51C1618AF002241C1"
BuildableName = "Dip.framework"
BlueprintName = "Dip-tvOS"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B3A51C1618AF002241C1"
BuildableName = "Dip.framework"
BlueprintName = "Dip-tvOS"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B3A51C1618AF002241C1"
BuildableName = "Dip.framework"
BlueprintName = "Dip-tvOS"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
@@ -1,80 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0710"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B4031C162862002241C1"
BuildableName = "Dip.framework"
BlueprintName = "Dip-watchOS"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B4031C162862002241C1"
BuildableName = "Dip.framework"
BlueprintName = "Dip-watchOS"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B4031C162862002241C1"
BuildableName = "Dip.framework"
BlueprintName = "Dip-watchOS"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0710"
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
@@ -16,7 +16,21 @@
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B3571C161543002241C1"
BuildableName = "Dip.framework"
BlueprintName = "Dip-OSX"
BlueprintName = "Dip"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "NO"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B3601C161543002241C1"
BuildableName = "DipTests.xctest"
BlueprintName = "DipTests"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</BuildActionEntry>
@@ -33,8 +47,8 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B3601C161543002241C1"
BuildableName = "Dip-OSXTests.xctest"
BlueprintName = "Dip-OSXTests"
BuildableName = "DipTests.xctest"
BlueprintName = "DipTests"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</TestableReference>
@@ -44,7 +58,7 @@
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B3571C161543002241C1"
BuildableName = "Dip.framework"
BlueprintName = "Dip-OSX"
BlueprintName = "Dip"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</MacroExpansion>
@@ -66,7 +80,7 @@
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B3571C161543002241C1"
BuildableName = "Dip.framework"
BlueprintName = "Dip-OSX"
BlueprintName = "Dip"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</MacroExpansion>
@@ -84,7 +98,7 @@
BuildableIdentifier = "primary"
BlueprintIdentifier = "0903B3571C161543002241C1"
BuildableName = "Dip.framework"
BlueprintName = "Dip-OSX"
BlueprintName = "Dip"
ReferencedContainer = "container:Dip.xcodeproj">
</BuildableReference>
</MacroExpansion>
+1 -1
View File
@@ -9,7 +9,7 @@
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<string>7.1.1</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
+1 -1
View File
@@ -9,7 +9,7 @@
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<string>7.1.1</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
+54
View File
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14313.18"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Dip View Controller-->
<scene sceneID="adI-oe-5KL">
<objects>
<viewController storyboardIdentifier="DipViewController" id="fzZ-tH-vfC" customClass="DipViewController" customModule="DipTests" sceneMemberID="viewController">
<view key="view" id="vso-jO-9Ex">
<rect key="frame" x="0.0" y="0.0" width="450" height="300"/>
<autoresizingMask key="autoresizingMask"/>
</view>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="string" keyPath="dipTag" value="vc"/>
</userDefinedRuntimeAttributes>
</viewController>
<customObject id="wjM-mL-nmG" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="311" y="339"/>
</scene>
<!--Nil Tag View Controller-->
<scene sceneID="OhX-tC-zpS">
<objects>
<viewController storyboardIdentifier="NilTagViewController" id="35S-Ec-qEA" customClass="NilTagViewController" customModule="DipTests" sceneMemberID="viewController">
<view key="view" id="y58-K4-cDZ">
<rect key="frame" x="0.0" y="0.0" width="450" height="300"/>
<autoresizingMask key="autoresizingMask"/>
</view>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="nil" keyPath="dipTag"/>
</userDefinedRuntimeAttributes>
</viewController>
<customObject id="pvf-jv-8Cj" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="874" y="339"/>
</scene>
<!--View Controller-->
<scene sceneID="Rwu-gt-fAa">
<objects>
<viewController storyboardIdentifier="ViewController" id="tne-ER-mvb" sceneMemberID="viewController">
<view key="view" id="Kwe-OO-w0D">
<rect key="frame" x="0.0" y="0.0" width="450" height="300"/>
<autoresizingMask key="autoresizingMask"/>
</view>
</viewController>
<customObject id="0u1-hv-ZtW" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="311" y="729"/>
</scene>
</scenes>
</document>
-9
View File
@@ -1,9 +0,0 @@
import PackageDescription
let package = Package(
name: "DipTests",
dependencies: [
.Package(url: "../../../Dip", majorVersion: 4, minor: 2),
]
)
@@ -1,286 +0,0 @@
//
// Dip
//
// Copyright (c) 2015 Olivier Halligon <olivier@halligon.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import XCTest
@testable import Dip
private protocol Server: class {
weak var client: Client? {get}
var anotherClient: Client? {get set}
var optionalProperty: AnyObject? {get}
}
private protocol Client: class {
var server: Server? {get}
var anotherServer: Server? {get set}
var optionalProperty: AnyObject? {get}
}
private class ServerImp: Server {
var _client = InjectedWeak<Client>() { _ in
AutoInjectionTests.clientDidInjectCalled = true
}
var client: Client? {
return _client.value
}
weak var anotherClient: Client?
weak var _optionalProperty = InjectedWeak<AnyObject>(required: false)
var optionalProperty: AnyObject? { return _optionalProperty?.value }
}
private class ClientImp: Client {
var _server = Injected<Server>() { _ in
AutoInjectionTests.serverDidInjectCalled = true
}
var server: Server? {
return _server.value
}
var anotherServer: Server?
var _optionalProperty = Injected<AnyObject>(required: false)
var optionalProperty: AnyObject? { return _optionalProperty.value }
var taggedServer = Injected<Server>(tag: "tagged")
}
private class Obj1 {
let obj2 = InjectedWeak<Obj2>()
let obj3 = Injected<Obj3>()
}
private class Obj2 {
let obj1 = Injected<Obj1>()
}
private class Obj3 {
weak var obj1: Obj1?
init(obj: Obj1) {
self.obj1 = obj
}
}
class AutoInjectionTests: XCTestCase {
static var serverDidInjectCalled: Bool = false
static var clientDidInjectCalled: Bool = false
let container = DependencyContainer()
#if os(Linux)
var allTests: [(String, () throws -> Void)] {
return [
("testThatItResolvesAutoInjectedDependencies", testThatItResolvesAutoInjectedDependencies),
("testThatItThrowsErrorIfFailsToAutoInjectDependency", testThatItThrowsErrorIfFailsToAutoInjectDependency),
("testThatItResolvesAutoInjectedSingletons", testThatItResolvesAutoInjectedSingletons),
("testThatItCallsResolveDependencyBlockWhenAutoInjecting", testThatItCallsResolveDependencyBlockWhenAutoInjecting),
("testThatItReusesResolvedAutoInjectedInstances", testThatItReusesResolvedAutoInjectedInstances),
("testThatItReusesAutoInjectedInstancesOnNextResolveOrAutoInjection", testThatItReusesAutoInjectedInstancesOnNextResolveOrAutoInjection),
("testThatThereIsNoRetainCycleBetweenAutoInjectedCircularDependencies", testThatThereIsNoRetainCycleBetweenAutoInjectedCircularDependencies),
("testThatItCallsDidInjectOnAutoInjectedProperty", testThatItCallsDidInjectOnAutoInjectedProperty),
("testThatNoErrorThrownWhenOptionalPropertiesAreNotAutoInjected", testThatNoErrorThrownWhenOptionalPropertiesAreNotAutoInjected),
("testThatItResolvesTaggedAutoInjectedProperties", testThatItResolvesTaggedAutoInjectedProperties)
]
}
func setUp() {
container.reset()
}
#else
override func setUp() {
container.reset()
}
#endif
func testThatItResolvesAutoInjectedDependencies() {
container.register(.ObjectGraph) { ServerImp() as Server }
container.register(.ObjectGraph) { ClientImp() as Client }
let client = try! container.resolve() as Client
let server = client.server
XCTAssertTrue(client === server?.client)
}
func testThatItThrowsErrorIfFailsToAutoInjectDependency() {
container.register(.ObjectGraph) { ClientImp() as Client }
AssertThrows(expression: try container.resolve() as Client)
}
func testThatItResolvesAutoInjectedSingletons() {
//given
container.register(.Singleton) { ServerImp() as Server }
container.register(.Singleton) { ClientImp() as Client }
//when
let sharedClient = try! container.resolve() as Client
let sharedServer = try! container.resolve() as Server
let client = try! container.resolve() as Client
let server = client.server
//then
XCTAssertTrue(client === sharedClient)
XCTAssertTrue(client === server?.client)
XCTAssertTrue(server === sharedServer)
}
func testThatItCallsResolveDependencyBlockWhenAutoInjecting() {
var serverBlockWasCalled = false
//given
container.register(.ObjectGraph) { ServerImp() as Server }
.resolveDependencies { (container, server) -> () in
serverBlockWasCalled = true
}
var clientBlockWasCalled = false
container.register(.ObjectGraph) { ClientImp() as Client }
.resolveDependencies { (container, client) -> () in
clientBlockWasCalled = true
}
//when
try! container.resolve() as Client
XCTAssertTrue(serverBlockWasCalled)
try! container.resolve() as Server
XCTAssertTrue(clientBlockWasCalled)
}
func testThatItReusesResolvedAutoInjectedInstances() {
//given
container.register(.ObjectGraph) { ServerImp() as Server }
.resolveDependencies { (container, server) -> () in
server.anotherClient = try! container.resolve() as Client
}
container.register(.ObjectGraph) { ClientImp() as Client }
.resolveDependencies { (container, client) -> () in
client.anotherServer = try! container.resolve() as Server
}
//when
let client = try! container.resolve() as Client
//then
let server = client.server
let anotherServer = client.anotherServer
XCTAssertTrue(server === anotherServer)
let oneClient = server!.client
let anotherClient = server!.anotherClient
XCTAssertTrue(oneClient === anotherClient)
XCTAssertTrue(client === anotherClient)
}
func testThatItReusesAutoInjectedInstancesOnNextResolveOrAutoInjection() {
//given
container.register(.ObjectGraph) { Obj1() }
container.register(.ObjectGraph) { Obj2() }
container.register(.ObjectGraph) { Obj3(obj: try self.container.resolve()) }
//when
let obj2 = try! container.resolve() as Obj2
//then
XCTAssertTrue(obj2 === obj2.obj1.value!.obj2.value!,
"Auto-injected instance should be reused on next auto-injection")
XCTAssertTrue(obj2.obj1.value! === obj2.obj1.value!.obj3.value!.obj1,
"Auto-injected instance should be reused on next resolve")
}
func testThatThereIsNoRetainCycleBetweenAutoInjectedCircularDependencies() {
//given
container.register(.ObjectGraph) { ServerImp() as Server }
container.register(.ObjectGraph) { ClientImp() as Client }
//when
var client: Client? = try! container.resolve() as Client
//then
weak var weakServer: Server? = client?.server
weak var weakClient = client
XCTAssertNotNil(weakClient)
XCTAssertNotNil(weakServer)
client = nil
XCTAssertNil(weakClient)
XCTAssertNil(weakServer)
}
func testThatItCallsDidInjectOnAutoInjectedProperty() {
AutoInjectionTests.clientDidInjectCalled = false
AutoInjectionTests.serverDidInjectCalled = false
//given
container.register(.ObjectGraph) { ServerImp() as Server }
container.register(.ObjectGraph) { ClientImp() as Client }
//when
try! container.resolve() as Client
//then
XCTAssertTrue(AutoInjectionTests.clientDidInjectCalled)
XCTAssertTrue(AutoInjectionTests.serverDidInjectCalled)
}
func testThatNoErrorThrownWhenOptionalPropertiesAreNotAutoInjected() {
//given
container.register(.ObjectGraph) { ServerImp() as Server }
container.register(.ObjectGraph) { ClientImp() as Client }
AssertNoThrow(expression: try container.resolve() as Client, "Container should not throw error if failed to resolve optional auto-injected properties.")
}
func testThatItResolvesTaggedAutoInjectedProperties() {
//given
container.register(.ObjectGraph) { ServerImp() as Server }
container.register(tag: "tagged", .ObjectGraph) { ServerImp() as Server }
container.register(.ObjectGraph) { ClientImp() as Client }
//when
let client = try! container.resolve() as Client
//then
let taggedServer = (client as! ClientImp).taggedServer.value!
let server = client.server!
//server and tagged server should be resolved as different instances
XCTAssertTrue(server !== taggedServer)
}
}
-299
View File
@@ -1,299 +0,0 @@
//
// Dip
//
// Copyright (c) 2015 Olivier Halligon <olivier@halligon.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import XCTest
@testable import Dip
private protocol Service: class { }
private class ServiceImp1: Service { }
private class ServiceImp2: Service { }
private protocol AutoWiredClient: class {
var service1: Service! { get set }
var service2: Service! { get set }
}
private class AutoWiredClientImp: AutoWiredClient {
var service1: Service!
var service2: Service!
init(service1: Service, service2: ServiceImp2) {
self.service1 = service1
self.service2 = service2
}
init() {}
}
class AutoWiringTests: XCTestCase {
let container = DependencyContainer()
#if os(Linux)
var allTests: [(String, () throws -> Void)] {
return [
("testThatItCanResolveWithAutoWiring", testThatItCanResolveWithAutoWiring),
("testThatItUsesAutoWireFactoryWithMostNumberOfArguments", testThatItUsesAutoWireFactoryWithMostNumberOfArguments),
("testThatItThrowsAmbiguityErrorWhenUsingAutoWire", testThatItThrowsAmbiguityErrorWhenUsingAutoWire),
("testThatItFirstTriesToUseTaggedFactoriesWhenUsingAutoWire", testThatItFirstTriesToUseTaggedFactoriesWhenUsingAutoWire),
("testThatItFallbackToNotTaggedFactoryWhenUsingAutoWire", testThatItFallbackToNotTaggedFactoryWhenUsingAutoWire),
("testThatItDoesNotTryToUseAutoWiringWhenCallingResolveWithArguments", testThatItDoesNotTryToUseAutoWiringWhenCallingResolveWithArguments),
("testThatItDoesNotUseAutoWiringWhenFailedToResolveLowLevelDependency", testThatItDoesNotUseAutoWiringWhenFailedToResolveLowLevelDependency),
("testThatItReusesInstancesResolvedWithAutoWiringWhenUsingAutoWiringAgain", testThatItReusesInstancesResolvedWithAutoWiringWhenUsingAutoWiringAgain),
("testThatItReusesInstancesResolvedWithAutoWiringWhenUsingAutoWiringAgainWithTheSameTagged", testThatItReusesInstancesResolvedWithAutoWiringWhenUsingAutoWiringAgainWithTheSameTagged),
("testThatItDoesNotReuseInstancesResolvedWithAutoWiringWhenUsingAutoWiringAgainWithNoTag", testThatItDoesNotReuseInstancesResolvedWithAutoWiringWhenUsingAutoWiringAgainWithNoTag)
]
}
func setUp() {
container.reset()
}
#else
override func setUp() {
container.reset()
}
#endif
func testThatItCanResolveWithAutoWiring() {
//given
container.register(.ObjectGraph) { ServiceImp1() as Service }
container.register(.ObjectGraph) { ServiceImp2() }
container.register(.ObjectGraph) { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
//when
let client = try! container.resolve() as AutoWiredClient
//then
let service1 = client.service1
XCTAssertTrue(service1 is ServiceImp1)
let service2 = client.service2
XCTAssertTrue(service2 is ServiceImp2)
}
func testThatItUsesAutoWireFactoryWithMostNumberOfArguments() {
//given
//1 arg
container.register(.ObjectGraph) { AutoWiredClientImp(service1: $0, service2: try self.container.resolve()) as AutoWiredClient }
//1 arg
container.register(.ObjectGraph) { AutoWiredClientImp(service1: try self.container.resolve(), service2: $0) as AutoWiredClient }
//2 args
var factoryWithMostNumberOfArgumentsCalled = false
container.register(.ObjectGraph) { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
.resolveDependencies { _ in
factoryWithMostNumberOfArgumentsCalled = true
}
container.register(.ObjectGraph) { ServiceImp1() as Service }
container.register(.ObjectGraph) { ServiceImp2() }
//when
let _ = try! container.resolve() as AutoWiredClient
//then
XCTAssertTrue(factoryWithMostNumberOfArgumentsCalled)
}
func testThatItThrowsAmbiguityErrorWhenUsingAutoWire() {
//given
//1 arg
container.register(.ObjectGraph) { AutoWiredClientImp(service1: $0, service2: try self.container.resolve()) as AutoWiredClient }
//1 arg
container.register(.ObjectGraph) { AutoWiredClientImp(service1: try self.container.resolve(), service2: $0) as AutoWiredClient }
container.register(.ObjectGraph) { ServiceImp1() as Service }
container.register(.ObjectGraph) { ServiceImp2() }
//when
AssertThrows(expression: try container.resolve() as AutoWiredClient) { error -> Bool in
switch error {
case DipError.AmbiguousDefinitions: return true
default: return false
}
}
}
func testThatItFirstTriesToUseTaggedFactoriesWhenUsingAutoWire() {
//given
//1 arg
container.register(.ObjectGraph) { AutoWiredClientImp(service1: $0, service2: try self.container.resolve()) as AutoWiredClient }
//1 arg
container.register(.ObjectGraph) { AutoWiredClientImp(service1: try self.container.resolve(), service2: $0) as AutoWiredClient }
//2 args
container.register(.ObjectGraph) { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
//1 arg tagged
var taggedFactoryWithMostNumberOfArgumentsCalled = false
container.register(tag: "tag", .ObjectGraph) { AutoWiredClientImp(service1: $0, service2: try self.container.resolve()) as AutoWiredClient }
//2 arg tagged
container.register(tag: "tag", .ObjectGraph) { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }.resolveDependencies { _ in
taggedFactoryWithMostNumberOfArgumentsCalled = true
}
container.register(.ObjectGraph) { ServiceImp1() as Service }
container.register(.ObjectGraph) { ServiceImp2() }
//when
let _ = try! container.resolve(tag: "tag") as AutoWiredClient
//then
XCTAssertTrue(taggedFactoryWithMostNumberOfArgumentsCalled)
}
func testThatItFallbackToNotTaggedFactoryWhenUsingAutoWire() {
//given
//1 arg
var notTaggedFactoryWithMostNumberOfArgumentsCalled = false
container.register(.ObjectGraph) { AutoWiredClientImp(service1: $0, service2: try self.container.resolve()) as AutoWiredClient }.resolveDependencies {_ in
notTaggedFactoryWithMostNumberOfArgumentsCalled = true
}
//1 arg tagged
container.register(tag: "tag", .ObjectGraph) { AutoWiredClientImp(service1: $0, service2: try self.container.resolve()) as AutoWiredClient }
container.register(.ObjectGraph) { ServiceImp1() as Service }
container.register(.ObjectGraph) { ServiceImp2() }
//when
let _ = try! container.resolve(tag: "other tag") as AutoWiredClient
//then
XCTAssertTrue(notTaggedFactoryWithMostNumberOfArgumentsCalled)
}
func testThatItDoesNotTryToUseAutoWiringWhenCallingResolveWithArguments() {
//given
container.register(.ObjectGraph) { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
container.register(.ObjectGraph) { ServiceImp1() as Service }
container.register(.ObjectGraph) { ServiceImp2() }
//when
let service = try! container.resolve() as Service
AssertThrows(expression: try container.resolve(withArguments: service) as AutoWiredClient,
"Container should not use auto-wiring when resolving with runtime arguments")
}
func testThatItDoesNotUseAutoWiringWhenFailedToResolveLowLevelDependency() {
//given
container.register(.ObjectGraph) { AutoWiredClientImp() as AutoWiredClient }
.resolveDependencies { container, resolved in
resolved.service1 = try container.resolve() as Service
resolved.service2 = try container.resolve() as ServiceImp2
//simulate that something goes wrong on the way
throw DipError.DefinitionNotFound(key: DefinitionKey(protocolType: ServiceImp1.self, factoryType: Any.self))
}
container.register(.ObjectGraph) { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
.resolveDependencies { container, resolved in
//auto-wiring should be performed only when definition for type to resolve is not found
//but not for any other type along the way in the graph
XCTFail("Auto-wiring should not be performed if instance was actually resolved.")
}
container.register(.ObjectGraph) { ServiceImp1() as Service }
container.register(.ObjectGraph) { ServiceImp2() }
//then
AssertThrows(expression: try container.resolve() as AutoWiredClient,
"Container should not use auto-wiring when definition for resolved type is registered.")
}
func testThatItReusesInstancesResolvedWithAutoWiringWhenUsingAutoWiringAgain() {
//given
container.register(.ObjectGraph) { ServiceImp1() as Service }
container.register(.ObjectGraph) { ServiceImp2() }
var anotherInstance: AutoWiredClient?
container.register(.ObjectGraph) { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
.resolveDependencies { container, _ in
if anotherInstance == nil {
anotherInstance = try! container.resolve() as AutoWiredClient
}
}
//when
let resolved = try! container.resolve() as AutoWiredClient
//then
//when doing another auto-wiring during resolve we should reuse instance
XCTAssertTrue((resolved as! AutoWiredClientImp) === (anotherInstance as! AutoWiredClientImp))
}
func testThatItReusesInstancesResolvedWithAutoWiringWhenUsingAutoWiringAgainWithTheSameTagged() {
//given
container.register(.ObjectGraph) { ServiceImp1() as Service }
container.register(.ObjectGraph) { ServiceImp2() }
var anotherInstance: AutoWiredClient?
container.register(tag: "tag", .ObjectGraph) { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
.resolveDependencies { container, _ in
if anotherInstance == nil {
anotherInstance = try! container.resolve(tag: "tag") as AutoWiredClient
}
}
//when
let resolved = try! container.resolve(tag: "tag") as AutoWiredClient
//then
//when doing another auto-wiring during resolve we should reuse instance
XCTAssertTrue((resolved as! AutoWiredClientImp) === (anotherInstance as! AutoWiredClientImp))
}
func testThatItDoesNotReuseInstancesResolvedWithAutoWiringWhenUsingAutoWiringAgainWithNoTag() {
//given
container.register(.ObjectGraph) { ServiceImp1() as Service }
container.register(.ObjectGraph) { ServiceImp2() }
var anotherInstance: AutoWiredClient?
container.register(.ObjectGraph) { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
.resolveDependencies { container, _ in
if anotherInstance == nil {
anotherInstance = try! container.resolve() as AutoWiredClient
}
}
//when
let resolved = try! container.resolve(tag: "tag") as AutoWiredClient
//then
//when doing another auto-wiring during resolve we should reuse instance
XCTAssertTrue((resolved as! AutoWiredClientImp) !== (anotherInstance as! AutoWiredClientImp))
}
}
@@ -1,222 +0,0 @@
//
// Dip
//
// Copyright (c) 2015 Olivier Halligon <olivier@halligon.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import XCTest
@testable import Dip
private protocol Service: class {}
private class ServiceImp1: Service {}
private class ServiceImp2: Service {}
private class Server {
weak var client: Client?
init() {}
}
private class Client {
var server: Server
init(server: Server) {
self.server = server
}
}
class ComponentScopeTests: XCTestCase {
let container = DependencyContainer()
#if os(Linux)
var allTests: [(String, () throws -> Void)] {
return [
("testThatPrototypeIsDefaultScope", testThatPrototypeIsDefaultScope),
("testThatScopeCanBeChanged", testThatScopeCanBeChanged),
("testThatItResolvesTypeAsNewInstanceForPrototypeScope", testThatItResolvesTypeAsNewInstanceForPrototypeScope),
("testThatItReusesInstanceForSingletonScope", testThatItReusesInstanceForSingletonScope),
("testThatSingletonIsNotReusedAcrossContainers", testThatSingletonIsNotReusedAcrossContainers),
("testThatSingletonIsReleasedWhenDefinitionIsRemoved", testThatSingletonIsReleasedWhenDefinitionIsRemoved),
("testThatSingletonIsReleasedWhenDefinitionIsOverridden", testThatSingletonIsReleasedWhenDefinitionIsOverridden),
("testThatSingletonIsReleasedWhenContainerIsReset", testThatSingletonIsReleasedWhenContainerIsReset),
("testThatItReusesInstanceInObjectGraphScopeDuringResolve", testThatItReusesInstanceInObjectGraphScopeDuringResolve),
("testThatItDoesNotReuseInstanceInObjectGraphScopeInNextResolve", testThatItDoesNotReuseInstanceInObjectGraphScopeInNextResolve),
("testThatItDoesNotReuseInstanceInObjectGraphScopeResolvedForNilTag", testThatItDoesNotReuseInstanceInObjectGraphScopeResolvedForNilTag)
]
}
func setUp() {
container.reset()
}
#else
override func setUp() {
container.reset()
}
#endif
func testThatPrototypeIsDefaultScope() {
let def = container.register { ServiceImp1() as Service }
XCTAssertEqual(def.scope, ComponentScope.Prototype)
}
func testThatScopeCanBeChanged() {
let def = container.register(.Singleton) { ServiceImp1() as Service }
XCTAssertEqual(def.scope, ComponentScope.Singleton)
}
func testThatItResolvesTypeAsNewInstanceForPrototypeScope() {
//given
container.register { ServiceImp1() as Service }
//when
let service1 = try! container.resolve() as Service
let service2 = try! container.resolve() as Service
//then
XCTAssertFalse(service1 === service2)
}
func testThatItReusesInstanceForSingletonScope() {
//given
container.register(.Singleton) { ServiceImp1() as Service }
//when
let service1 = try! container.resolve() as Service
let service2 = try! container.resolve() as Service
//then
XCTAssertTrue(service1 === service2)
}
func testThatSingletonIsNotReusedAcrossContainers() {
//given
let def = container.register(.Singleton) { ServiceImp1() as Service }
let secondContainer = DependencyContainer()
secondContainer.register(def, forTag: nil)
//when
let service1 = try! container.resolve() as Service
let service2 = try! secondContainer.resolve() as Service
//then
XCTAssertTrue(service1 !== service2, "Singleton instances should not be reused across containers")
}
func testThatSingletonIsReleasedWhenDefinitionIsRemoved() {
//given
let def = container.register(.Singleton) { ServiceImp1() as Service }
let service1 = try! container.resolve() as Service
//when
container.remove(def, forTag: nil)
container.register(def, forTag: nil)
//then
let service2 = try! container.resolve() as Service
XCTAssertTrue(service1 !== service2, "Singleton instances should be released when definition is removed from the container")
}
func testThatSingletonIsReleasedWhenDefinitionIsOverridden() {
//given
let def = container.register(.Singleton) { ServiceImp1() as Service }
let service1 = try! container.resolve() as Service
//when
container.register(def, forTag: nil)
//then
let service2 = try! container.resolve() as Service
XCTAssertTrue(service1 !== service2, "Singleton instances should be released when definition is overridden")
}
func testThatSingletonIsReleasedWhenContainerIsReset() {
//given
let def = container.register(.Singleton) { ServiceImp1() as Service }
let service1 = try! container.resolve() as Service
//when
container.reset()
container.register(def, forTag: nil)
//then
let service2 = try! container.resolve() as Service
XCTAssertTrue(service1 !== service2, "Singleton instances should be released when container is reset")
}
func testThatItReusesInstanceInObjectGraphScopeDuringResolve() {
//given
container.register(.ObjectGraph) { Client(server: try self.container.resolve()) as Client }
container.register(.ObjectGraph) { Server() as Server }.resolveDependencies { container, server in
server.client = try container.resolve() as Client
}
//when
let client = try! container.resolve() as Client
//then
let server = client.server
XCTAssertTrue(server.client === client)
}
func testThatItDoesNotReuseInstanceInObjectGraphScopeInNextResolve() {
//given
container.register(.ObjectGraph) { Client(server: try self.container.resolve()) as Client }
container.register(.ObjectGraph) { Server() as Server }.resolveDependencies { container, server in
server.client = try container.resolve() as Client
}
//when
let client = try! container.resolve() as Client
let server = client.server
let anotherClient = try! container.resolve() as Client
let anotherServer = anotherClient.server
//then
XCTAssertFalse(server === anotherServer)
XCTAssertFalse(client === anotherClient)
}
func testThatItDoesNotReuseInstanceInObjectGraphScopeResolvedForNilTag() {
//given
var service2: Service?
container.register(.ObjectGraph) { ServiceImp1() as Service }.resolveDependencies { (c, _) in
service2 = try c.resolve(tag: "service") as Service
//then
//when service1 is resolved using this definition due to fallback to nil tag
//we don't want every next resolve of service reuse it
XCTAssertTrue(service2 is ServiceImp2)
}
container.register(tag: "service", .ObjectGraph) { ServiceImp2() as Service}
//when
let service1 = try! container.resolve(tag: "tag") as Service
//then
XCTAssertTrue(service1 is ServiceImp1)
}
}
-333
View File
@@ -1,333 +0,0 @@
//
// Dip
//
// Copyright (c) 2015 Olivier Halligon <olivier@halligon.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import XCTest
@testable import Dip
private protocol Service: class { }
private class ServiceImp1: Service { }
private class ServiceImp2: Service { }
private protocol Server: class {
weak var client: Client? { get }
}
private protocol Client: class {
var server: Server? { get }
}
class DipTests: XCTestCase {
let container = DependencyContainer()
#if os(Linux)
var allTests: [(String, () throws -> Void)] {
return [
("testThatItResolvesInstanceRegisteredWithoutTag", testThatItResolvesInstanceRegisteredWithoutTag),
("testThatItResolvesInstanceRegisteredWithTag", testThatItResolvesInstanceRegisteredWithTag),
("testThatItResolvesDifferentInstancesRegisteredForDifferentTags", testThatItResolvesDifferentInstancesRegisteredForDifferentTags),
("testThatNewRegistrationOverridesPreviousRegistration", testThatNewRegistrationOverridesPreviousRegistration),
("testThatItCallsResolveDependenciesOnDefinition", testThatItCallsResolveDependenciesOnDefinition),
("testThatItThrowsErrorIfCanNotFindDefinitionForType", testThatItThrowsErrorIfCanNotFindDefinitionForType),
("testThatItThrowsErrorIfCanNotFindDefinitionForTag", testThatItThrowsErrorIfCanNotFindDefinitionForTag),
("testThatItThrowsErrorIfCanNotFindDefinitionForFactoryWithArguments", testThatItThrowsErrorIfCanNotFindDefinitionForFactoryWithArguments),
("testThatItThrowsErrorIfConstructorThrows", testThatItThrowsErrorIfConstructorThrows),
("testThatItThrowsErrorIfFailsToResolveDependency", testThatItThrowsErrorIfFailsToResolveDependency),
("testThatItCallsDidResolveDependenciesOnResolvableIntance", testThatItCallsDidResolveDependenciesOnResolvableIntance),
("testThatItResolvesCircularDependencies", testThatItResolvesCircularDependencies)
]
}
func setUp() {
container.reset()
}
#else
override func setUp() {
container.reset()
}
#endif
func testThatItResolvesInstanceRegisteredWithoutTag() {
//given
container.register { ServiceImp1() as Service }
//when
let serviceInstance = try! container.resolve() as Service
//then
XCTAssertTrue(serviceInstance is ServiceImp1)
}
func testThatItResolvesInstanceRegisteredWithTag() {
//given
container.register(tag: "service") { ServiceImp1() as Service }
//when
let serviceInstance = try! container.resolve(tag: "service") as Service
//then
XCTAssertTrue(serviceInstance is ServiceImp1)
}
func testThatItResolvesDifferentInstancesRegisteredForDifferentTags() {
//given
container.register(tag: "service1") { ServiceImp1() as Service }
container.register(tag: "service2") { ServiceImp2() as Service }
//when
let service1Instance = try! container.resolve(tag: "service1") as Service
let service2Instance = try! container.resolve(tag: "service2") as Service
//then
XCTAssertTrue(service1Instance is ServiceImp1)
XCTAssertTrue(service2Instance is ServiceImp2)
}
func testThatNewRegistrationOverridesPreviousRegistration() {
//given
container.register { ServiceImp1() as Service }
let service1 = try! container.resolve() as Service
//when
container.register { ServiceImp2() as Service }
let service2 = try! container.resolve() as Service
//then
XCTAssertTrue(service1 is ServiceImp1)
XCTAssertTrue(service2 is ServiceImp2)
}
func testThatItCallsResolveDependenciesOnDefinition() {
//given
var resolveDependenciesCalled = false
container.register { ServiceImp1() as Service }.resolveDependencies { (c, s) in
resolveDependenciesCalled = true
}
//when
try! container.resolve() as Service
//then
XCTAssertTrue(resolveDependenciesCalled)
}
func testThatItThrowsErrorIfCanNotFindDefinitionForType() {
//given
container.register { ServiceImp1() as ServiceImp1 }
//when
AssertThrows(expression: try container.resolve() as Service) { error in
guard case let DipError.DefinitionNotFound(key) = error else { return false }
//then
typealias F = () throws -> Service
let expectedKey = DefinitionKey(protocolType: Service.self, factoryType: F.self, associatedTag: nil)
XCTAssertEqual(key, expectedKey)
return true
}
}
func testThatItThrowsErrorIfCanNotFindDefinitionForTag() {
//given
container.register(tag: "some tag") { ServiceImp1() as Service }
//when
AssertThrows(expression: try container.resolve(tag: "other tag") as Service) { error in
guard case let DipError.DefinitionNotFound(key) = error else { return false }
//then
typealias F = () throws -> Service
let expectedKey = DefinitionKey(protocolType: Service.self, factoryType: F.self, associatedTag: "other tag")
XCTAssertEqual(key, expectedKey)
return true
}
}
func testThatItThrowsErrorIfCanNotFindDefinitionForFactoryWithArguments() {
//given
container.register { ServiceImp1() as Service }
//when
AssertThrows(expression: try container.resolve(withArguments: "some string") as Service) { error in
guard case let DipError.DefinitionNotFound(key) = error else { return false }
//then
typealias F = (String) throws -> Service
let expectedKey = DefinitionKey(protocolType: Service.self, factoryType: F.self, associatedTag: nil)
XCTAssertEqual(key, expectedKey)
return true
}
}
func testThatItThrowsErrorIfConstructorThrows() {
//given
let failedKey = DefinitionKey(protocolType: Any.self, factoryType: Any.self)
let expectedError = DipError.DefinitionNotFound(key: failedKey)
container.register { () throws -> Service in throw expectedError }
//when
AssertThrows(expression: try container.resolve() as Service) { error in
switch error {
case let DipError.DefinitionNotFound(key) where key == failedKey: return true
default: return false
}
}
}
func testThatItThrowsErrorIfFailsToResolveDependency() {
//given
let failedKey = DefinitionKey(protocolType: Any.self, factoryType: Any.self)
let expectedError = DipError.DefinitionNotFound(key: failedKey)
container.register { ServiceImp1() as Service }
.resolveDependencies { container, service in
//simulate throwing error when resolving dependency
throw expectedError
}
//when
AssertThrows(expression: try container.resolve() as Service) { error in
switch error {
case let DipError.DefinitionNotFound(key) where key == failedKey: return true
default: return false
}
}
}
func testThatItCallsDidResolveDependenciesOnResolvableIntance() {
class ResolvableService: Service, Resolvable {
var didResolveDependenciesCalled = false
func didResolveDependencies() {
XCTAssertFalse(didResolveDependenciesCalled, "didResolveDependencies should be called only once per instance")
didResolveDependenciesCalled = true
}
}
container.register { ResolvableService() as Service }
.resolveDependencies { _, service in
XCTAssertFalse((service as! ResolvableService).didResolveDependenciesCalled, "didResolveDependencies should not be called yet")
return
}
container.register(tag: "graph", .ObjectGraph) { ResolvableService() as Service }
.resolveDependencies { _, service in
XCTAssertFalse((service as! ResolvableService).didResolveDependenciesCalled)
return
}
container.register(tag: "singleton", .Singleton) { ResolvableService() as Service }
.resolveDependencies { _, service in
XCTAssertFalse((service as! ResolvableService).didResolveDependenciesCalled)
return
}
let service = try! container.resolve() as Service
XCTAssertTrue((service as! ResolvableService).didResolveDependenciesCalled)
let graphService = try! container.resolve(tag: "graph") as Service
XCTAssertTrue((graphService as! ResolvableService).didResolveDependenciesCalled)
let singletonService = try! container.resolve(tag: "singleton") as Service
let _ = try! container.resolve(tag: "singleton") as Service
XCTAssertTrue((singletonService as! ResolvableService).didResolveDependenciesCalled)
}
func testThatItResolvesCircularDependencies() {
class ResolvableServer: Server, Resolvable {
weak var client: Client?
weak var secondClient: Client?
init(client: Client) {
self.client = client
}
var didResolveDependenciesCalled = false
func didResolveDependencies() {
XCTAssertFalse(didResolveDependenciesCalled, "didResolveDependencies should be called only once per instance")
didResolveDependenciesCalled = true
XCTAssertNotNil(self.client)
XCTAssertNotNil(self.secondClient)
XCTAssertNotNil(self.client?.server)
XCTAssertNotNil(self.secondClient)
XCTAssertNotNil(self.secondClient?.server)
}
}
class ResolvableClient: Client, Resolvable {
var server: Server?
var secondServer: Server?
init() {}
var didResolveDependenciesCalled = false
func didResolveDependencies() {
XCTAssertFalse(didResolveDependenciesCalled, "didResolveDependencies should be called only once per instance")
didResolveDependenciesCalled = true
XCTAssertNotNil(self.server)
XCTAssertNotNil(self.secondServer)
XCTAssertNotNil(self.server?.client)
XCTAssertNotNil(self.secondServer?.client)
}
}
container.register(.ObjectGraph) { try ResolvableServer(client: self.container.resolve()) as Server }
.resolveDependencies { (container: DependencyContainer, server: Server) in
let server = server as! ResolvableServer
server.secondClient = try container.resolve() as Client
}
container.register(.ObjectGraph) { ResolvableClient() as Client }
.resolveDependencies { (container: DependencyContainer, client: Client) in
let client = client as! ResolvableClient
client.server = try container.resolve() as Server
client.secondServer = try container.resolve() as Server
}
let client = (try! container.resolve() as Client) as! ResolvableClient
let server = client.server as! ResolvableServer
let secondServer = client.secondServer as! ResolvableServer
let secondClient = server.secondClient as! ResolvableClient
XCTAssertTrue(client === server.client)
XCTAssertTrue(client === server.secondClient)
XCTAssertTrue(client === secondServer.client)
XCTAssertTrue(client === secondServer.secondClient)
XCTAssertTrue(client === secondClient)
XCTAssertTrue(server === secondServer)
XCTAssertTrue(client.didResolveDependenciesCalled)
XCTAssertTrue(server.didResolveDependenciesCalled)
}
}
-88
View File
@@ -1,88 +0,0 @@
//
// Dip
//
// Copyright (c) 2015 Olivier Halligon <olivier@halligon.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import XCTest
func AssertThrows<T>(file: StaticString = #file, line: UInt = #line, @autoclosure expression: () throws -> T) {
AssertThrows(file, line: line, expression: expression, "")
}
func AssertThrows<T>(file: StaticString = #file, line: UInt = #line, @autoclosure expression: () throws -> T, _ message: String) {
AssertThrows(expression: expression, checkError: { _ in true }, message)
}
func AssertThrows<T>(file: StaticString = #file, line: UInt = #line, @autoclosure expression: () throws -> T, checkError: ErrorType -> Bool) {
AssertThrows(file, line: line, expression: expression, checkError: checkError, "")
}
func AssertThrows<T>(file: StaticString = #file, line: UInt = #line, @autoclosure expression: () throws -> T, checkError: ErrorType -> Bool, _ message: String) {
do {
try expression()
XCTFail(message, file: file, line: line)
}
catch {
XCTAssertTrue(checkError(error), "Thrown unexpected error: \(error)", file: file, line: line)
}
}
func AssertNoThrow<T>(file: StaticString = #file, line: UInt = #line, @autoclosure expression: () throws -> T) {
AssertNoThrow(file, line: line, expression: expression, "")
}
func AssertNoThrow<T>(file: StaticString = #file, line: UInt = #line, @autoclosure expression: () throws -> T, _ message: String) {
do {
try expression()
}
catch {
XCTFail(message, file: file, line: line)
}
}
#if os(Linux)
import Glibc
typealias TMain = @convention(c) (UnsafeMutablePointer<Void>) -> UnsafeMutablePointer<Void>
func dispatch_async(block: TMain) {
var pid: pthread_t = 0
pthread_create(&pid, nil, block, nil)
}
func dispatch_sync(block: TMain) -> UnsafeMutablePointer<Void> {
var pid: pthread_t = 0
var result: UnsafeMutablePointer<Void> = nil
pthread_create(&pid, nil, block, nil)
pthread_join(pid, &result)
return result
}
extension pthread_spinlock_t {
mutating func lock() {
pthread_spin_lock(&self)
}
mutating func unlock() {
pthread_spin_unlock(&self)
}
}
#endif
-11
View File
@@ -1,11 +0,0 @@
import XCTest
XCTMain([
DipTests(),
DefinitionTests(),
RuntimeArgumentsTests(),
ComponentScopeTests(),
AutoInjectionTests(),
ThreadSafetyTests(),
AutoWiringTests()
])
+75
View File
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder.AppleTV.Storyboard" version="3.0" toolsVersion="14313.18" targetRuntime="AppleTV" propertyAccessControl="none" useAutolayout="YES" colorMatched="YES" initialViewController="ctX-Lj-Yrr">
<device id="appleTV" orientation="landscape">
<adaptation id="light"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14283.14"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Dip View Controller-->
<scene sceneID="0jz-eb-APg">
<objects>
<viewController storyboardIdentifier="DipViewController" id="ctX-Lj-Yrr" customClass="DipViewController" customModule="DipTests" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="TrC-Rh-efi"/>
<viewControllerLayoutGuide type="bottom" id="arQ-XW-qWa"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="syn-UA-YGd">
<rect key="frame" x="0.0" y="0.0" width="200" height="100"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
</view>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<size key="freeformSize" width="200" height="100"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="string" keyPath="dipTag" value="vc"/>
</userDefinedRuntimeAttributes>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="mq6-CB-g6V" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="563" y="-228"/>
</scene>
<!--View Controller-->
<scene sceneID="gDU-un-krd">
<objects>
<viewController storyboardIdentifier="ViewController" id="UwR-h2-tgS" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="qKG-R0-Nkp"/>
<viewControllerLayoutGuide type="bottom" id="vP4-Si-HAL"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="r35-FI-kgS">
<rect key="frame" x="0.0" y="0.0" width="200" height="100"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
</view>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<size key="freeformSize" width="200" height="100"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="AIY-qB-Dbe" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="563" y="-16"/>
</scene>
<!--Nil Tag View Controller-->
<scene sceneID="PiH-4i-Txa">
<objects>
<viewController storyboardIdentifier="NilTagViewController" id="ZLb-1s-1ne" customClass="NilTagViewController" customModule="DipTests" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="arU-Ca-km8"/>
<viewControllerLayoutGuide type="bottom" id="jw1-4B-Xlt"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="NMo-Oi-l7H">
<rect key="frame" x="0.0" y="0.0" width="200" height="100"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
</view>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<size key="freeformSize" width="200" height="100"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="nil" keyPath="dipTag"/>
</userDefinedRuntimeAttributes>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Cid-Yu-0N6" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="821" y="-228"/>
</scene>
</scenes>
</document>
+78
View File
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="5AO-J3-7R4">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14283.14"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="Ole-tM-Q3l">
<objects>
<viewController storyboardIdentifier="ViewController" id="ehZ-7Y-MeO" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="bZ0-tI-Yi9"/>
<viewControllerLayoutGuide type="bottom" id="UX3-8G-Z4L"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="Jmi-i3-vAY">
<rect key="frame" x="0.0" y="0.0" width="200" height="100"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<size key="freeformSize" width="200" height="100"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="H12-WL-igv" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="646" y="461"/>
</scene>
<!--Dip View Controller-->
<scene sceneID="Lgf-SY-hfd">
<objects>
<viewController storyboardIdentifier="DipViewController" id="5AO-J3-7R4" userLabel="Dip View Controller" customClass="DipViewController" customModule="DipTests" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="JYl-DM-U2W"/>
<viewControllerLayoutGuide type="bottom" id="Xg8-gz-BQL"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="aU0-uz-rHf">
<rect key="frame" x="0.0" y="0.0" width="200" height="100"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<size key="freeformSize" width="200" height="100"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="string" keyPath="dipTag" value="vc"/>
</userDefinedRuntimeAttributes>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="4iF-mX-EX6" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="646" y="200"/>
</scene>
<!--Dip View Controller-->
<scene sceneID="AUA-qF-7ky">
<objects>
<viewController storyboardIdentifier="NilTagViewController" id="fFP-hb-OdS" userLabel="Dip View Controller" customClass="NilTagViewController" customModule="DipTests" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Sio-ii-jPl"/>
<viewControllerLayoutGuide type="bottom" id="bhs-zK-dln"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="4xq-UV-htt">
<rect key="frame" x="0.0" y="0.0" width="200" height="100"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<size key="freeformSize" width="200" height="100"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="nil" keyPath="dipTag"/>
</userDefinedRuntimeAttributes>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="xvW-k9-I9y" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="898" y="200"/>
</scene>
</scenes>
</document>
Binary file not shown.
Binary file not shown.
+2
View File
@@ -0,0 +1,2 @@
[
]
@@ -1,4 +1,4 @@
//: [Previous: Shared Instances](@previous)
//: [Previous: Auto-wiring](@previous)
import UIKit
import Dip
@@ -8,12 +8,12 @@ let container = DependencyContainer()
### Auto-Injection
On the previous page you saw how auto-wiring helps us get rid of boilerplate code when registering and resolving components with consturctor injection. Auto-injection solves the same problem for property injection.
On the previous page you saw how auto-wiring helps us to get rid of boilerplate code when registering and resolving components with consturctor injection. Auto-injection solves the same problem for property injection.
Let's say you have following related components:
*/
protocol Service: class {
protocol Service: AnyObject {
var logger: Logger? { get }
var tracker: Tracker? { get }
}
@@ -31,7 +31,7 @@ container.register() { TrackerImp() as Tracker }
container.register() { LoggerImp() as Logger }
container.register() { ServiceImp() as Service }
.resolveDependencies { container, service in
.resolvingProperties { container, service in
let service = service as! ServiceImp
service.logger = try container.resolve() as Logger
service.tracker = try container.resolve() as Tracker
@@ -47,10 +47,10 @@ With auto-injection your code transforms to this:
*/
class AutoInjectedServiceImp: Service {
private var injectedLogger = Injected<Logger>()
private let injectedLogger = Injected<Logger>()
var logger: Logger? { return injectedLogger.value }
private var injectedTracker = Injected<Tracker>()
private let injectedTracker = Injected<Tracker>()
var tracker: Tracker? { return injectedTracker.value }
}
@@ -83,9 +83,7 @@ container.register { ServerWithRequiredClient() }
do {
let serverWithClient = try container.resolve() as ServerWithRequiredClient
}
catch {
print(error)
}
catch {}
/*:
You can make auto-injected property optional by passing `false` to `required` parameter of `Injected<T>`/`InjectedWeak<T>` constructor. For such properties container will ignore any errors when it resolves this property (or any of its dependencies).
@@ -103,11 +101,11 @@ serverWithNoClient.optionalClient.value
Another example of using auto-injection is circular dependencies. Let's say you have a `Server` and a `ServerClient` both referencing each other.
*/
protocol Server: class {
protocol Server: AnyObject {
weak var client: ServerClient? { get }
}
protocol ServerClient: class {
protocol ServerClient: AnyObject {
var server: Server? { get }
}
@@ -127,12 +125,12 @@ class ServerClientImp: ServerClient {
The standard way to register such components in `DependencyContainer` will lead to such code:
*/
container.register(.ObjectGraph) {
container.register {
ServerClientImp(server: try container.resolve()) as ServerClient
}
container.register(.ObjectGraph) { ServerImp() as Server }
.resolveDependencies { (container: DependencyContainer, server: Server) in
container.register { ServerImp() as Server }
.resolvingProperties { (container: DependencyContainer, server: Server) in
(server as! ServerImp).client = try container.resolve() as ServerClient
}
@@ -153,8 +151,8 @@ class InjectedClientImp: ServerClient {
var server: Server? { get { return injectedServer.value } }
}
container.register(.ObjectGraph) { InjectedServerImp() as Server }
container.register(.ObjectGraph) { InjectedClientImp() as ServerClient }
container.register { InjectedServerImp() as Server }
container.register { InjectedClientImp() as ServerClient }
let injectedClient = try! container.resolve() as ServerClient
injectedClient.server
@@ -176,7 +174,7 @@ class ViewController: UIViewController {
}
container.register { ViewController() }
.resolveDependencies { container, controller in
.resolvingProperties { container, controller in
controller.logger = try container.resolve() as Logger
controller.tracker = try container.resolve() as Tracker
controller.dataProvider = try container.resolve() as DataProvider
@@ -210,4 +208,4 @@ In such scenario when view controller is created by storyboard you will need to
So as you can see there are certain advantages and disadvatages of using auto-injection. It makes your definitions simpler, especially if there are circular dependencies involved or the number of dependencies is high, removing boilerplate calls to `resolve` method in `resolveDependencies` block of your definitions. But it requires additional properties and some boilerplate code in your implementations, makes your implementatios tightly coupled with Dip. You can avoid tight coupoling by using your own boxing classes instead of `Injected<T>` and `InjectedWeak<T>` (see `AutoInjectedPropertyBox`).
*/
//: [Next: Testing](@next)
//: [Next: Type Forwarding](@next)
@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
@@ -7,7 +7,7 @@ import UIKit
### Auto-wiring
Among three main DI patterns - _constructor_, _property_ and _method_ injection - construction injection should be your choise by default. Dip makes use of this pattern very simple.
Among three main DI patterns - _constructor_, _property_ and _method_ injection - constructor injection should be your choise by default. Dip makes using this pattern very simple.
Let's say you have some VIPER module with following components:
*/
@@ -28,7 +28,7 @@ class View: UIView, ViewOutput {}
class ServiceImp: Service {}
/*:
VIPER module by its nature consists of a lot of components, wired up using protocols. Using construction injection you can end up with following constructors for presenter and interactor:
VIPER module by its nature consists of a lot of components, wired up using protocols. Using constructor injection you can end up with following constructors for presenter and interactor:
*/
class InteractorImp: Interactor {
@@ -86,10 +86,10 @@ But then to resolve presenter or interactor you will first need to resolve their
*/
let service = try! container.resolve() as Service
let interactor = try! container.resolve(withArguments: service) as Interactor
let interactor = try! container.resolve(arguments: service) as Interactor
let view = try! container.resolve() as ViewOutput
let router = try! container.resolve() as Router
presenter = try! container.resolve(withArguments: view, interactor, router) as Presenter
presenter = try! container.resolve(arguments: view, interactor, router) as Presenter
presenter.interactor.service
/*:
@@ -109,7 +109,7 @@ You don't need to call `resolve` in a factory and care about order of arguments
The only requirement is that all constructor arguments should be registered in the container and there should be no several factories with the same _number_ of arguments registered for the same components.
In very rare case when you have several different factories with different set of runtime arguments registered for the same component, when you try to resolve it container will try to use these factories one by one until one of them succeeds starting with a factory with most numbers of arguments. If it finds two factories with the same number of arguments it will throw an error.
In very rare cases when you have several factories for the same component with different set of runtime arguments, when you try to resolve it container will try to use factory registered for the same type and tag (if provided, otherwise registered without tag) and with the maximum number of runtime arguments. If it finds two factories registered for the same type and tag and with the same number but different types of arguments it will throw an error.
You can use auto-wiring with tags. The tag that you pass to `resolve` method will be used to resolve each of the constructor arguments.
*/
@@ -1,6 +1,7 @@
//: [Previous: Scopes](@previous)
import Dip
import Foundation
let container = DependencyContainer()
@@ -12,11 +13,11 @@ Very often we encounter situations when we have circular dependencies between co
Let's say you have some network client and it's delegate defined like this:
*/
protocol NetworkClientDelegate: class {
protocol NetworkClientDelegate: AnyObject {
var networkClient: NetworkClient { get }
}
protocol NetworkClient: class {
protocol NetworkClient: AnyObject {
weak var delegate: NetworkClientDelegate? { get set }
}
@@ -25,7 +26,7 @@ class NetworkClientImp: NetworkClient {
init() {}
}
class Interactor: NetworkClientDelegate {
class Interactor: NSObject, NetworkClientDelegate {
let networkClient: NetworkClient
init(networkClient: NetworkClient) {
self.networkClient = networkClient
@@ -43,12 +44,12 @@ It's very important that _at least one_ of them uses property injection, because
Now you can register those classes in container:
*/
container.register(.ObjectGraph) {
container.register {
Interactor(networkClient: try container.resolve()) as NetworkClientDelegate
}
container.register(.ObjectGraph) { NetworkClientImp() as NetworkClient }
.resolveDependencies { (container, client) -> () in
container.register { NetworkClientImp() as NetworkClient }
.resolvingProperties { (container, client) -> () in
client.delegate = try container.resolve() as NetworkClientDelegate
}
@@ -77,26 +78,26 @@ let networkClient = try! container.resolve() as NetworkClient
networkClient.delegate // delegate was alread released =(
/*:
Note also that we used `.ObjectGraph` scope to register implementations. This is also very important to preserve consistency of objects relationships.
Note also that we used `.shared` scope to register implementations. This is also very important to preserve consistency of objects relationships.
If we would have used `.Prototype` scope for both components then container would not reuse instances and we would have an infinite loop:
If we would have used `.unique` scope for both components then container would not reuse instances and we would have an infinite loop:
- Each attempt to resolve `NetworkClientDelegate` will create new instance of `Interactor`.
- It will resolve `NetworkClient` which will create new instance of `NetworkClientImp`.
- It will try to resolve it's delegate property and that will create new instance of `Interactor`
- And so on and so on.
If we would have used `.Prototype` for one of the components it will lead to the same infinite loop or one of the relationships will be invalid:
If we would have used `.unique` for one of the components it will lead to the same infinite loop or one of the relationships will be invalid:
*/
container.reset()
container.register(.Prototype) {
container.register(.unique) {
Interactor(networkClient: try container.resolve()) as NetworkClientDelegate
}
container.register(.ObjectGraph) { NetworkClientImp() as NetworkClient }
.resolveDependencies { (container, client) -> () in
container.register { NetworkClientImp() as NetworkClient }
.resolvingProperties { (container, client) -> () in
client.delegate = try container.resolve() as NetworkClientDelegate
}
@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
@@ -0,0 +1,71 @@
//: [Previous: Type Forwarding](@previous)
import Dip
/*:
### Containers collaboration
Sometimes it makes sence to break your configuration in separate modules. For that you can use containers collaboration. You can link containers with each other and when you try to resolve a type using container where it was not registered, this container will forward request to its collaborating container. This way you can share core configurations or break them in separate modules, for example matching user stories, and still be able to link components from different modules.
*/
protocol DataStore {}
class CoreDataStore: DataStore {}
class AddEventWireframe {
var eventsListWireframe: EventsListWireframe?
}
class EventsListWireframe {
var addEventWireframe: AddEventWireframe?
let dataStore: DataStore
init(dataStore: DataStore) {
self.dataStore = dataStore
}
}
let rootContainer = DependencyContainer()
rootContainer.register(.singleton) { CoreDataStore() as DataStore }
let eventsListModule = DependencyContainer()
eventsListModule.register { EventsListWireframe(dataStore: $0) }
.resolvingProperties { container, wireframe in
wireframe.addEventWireframe = try container.resolve()
}
let addEventModule = DependencyContainer()
addEventModule.register { AddEventWireframe() }
eventsListModule.collaborate(with: addEventModule, rootContainer)
var eventsListWireframe = try eventsListModule.resolve() as EventsListWireframe
eventsListWireframe.dataStore
eventsListWireframe.addEventWireframe
/*:
As you can see dependencies were resolved even though not all components were registered in the same container.
It is even safe to make circular references between containers. This way you can resolve circular dependencies between components registered in different containers.
*/
eventsListModule.reset()
addEventModule.reset()
eventsListModule.register { EventsListWireframe(dataStore: $0) }
.resolvingProperties { container, wireframe in
wireframe.addEventWireframe = try container.resolve()
}
addEventModule.register { AddEventWireframe() }
.resolvingProperties { container, wireframe in
wireframe.eventsListWireframe = try container.resolve()
}
addEventModule.collaborate(with: eventsListModule)
eventsListWireframe = try eventsListModule.resolve() as EventsListWireframe
eventsListWireframe.addEventWireframe
eventsListWireframe.addEventWireframe?.eventsListWireframe === eventsListWireframe
/*:
If you try to link container with itself it will be silently ignored. When forwarding request collaborating containers will be iterated in the same order that they were added.
*/
//: [Next: Testing](@next)
@@ -21,6 +21,9 @@ or using a configuration block:
*/
container = DependencyContainer { container in
//do not forget to use unowned reference if you will need
//to reference container inside definition's factory
unowned let container = container
//register components here
}
@@ -35,7 +35,7 @@ You can use `DependencyContainer.Tag` enum, String or Integer literals, or insta
*/
container.register(tag: "tag") { ServiceImp1() as Service }
container.register(tag: .Int(0)) { ServiceImp1() as Service }
container.register(tag: 0) { ServiceImp1() as Service }
enum MyCustomTag: String, DependencyTagConvertible {
case SomeTag
@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
@@ -1,9 +1,8 @@
//: [Previous: Registering Components](@previous)
import Dip
let container = DependencyContainer { container in
container.register { ServiceImp1() as Service }
}
let container = DependencyContainer()
container.register { ServiceImp1() as Service }
/*:
@@ -42,6 +41,6 @@ let defaultService = try! container.resolve() as Service
You can use runtime arguments to resolve components. Dip supports up to six arguments. For more details see ["Runtime arguments"](Runtime%20arguments).
*/
container.register { service in ClientImp1(service: service) as Client }
let client = try! container.resolve(withArguments: service) as Client
let client = try! container.resolve(arguments: service) as Client
//: [Next: Runtime Arguments](@next)
@@ -15,18 +15,15 @@ Note that __types__, __number__ and __order__ of arguments matters and you can r
container.register { (url: NSURL, port: Int) in ServiceImp4(name: "1", baseURL: url, port: port) as Service }
container.register { (port: Int, url: NSURL) in ServiceImp4(name: "2", baseURL: url, port: port) as Service }
container.register { (port: Int, url: NSURL?) in ServiceImp4(name: "3", baseURL: url!, port: port) as Service }
container.register { (port: Int, url: NSURL!) in ServiceImp4(name: "4", baseURL: url, port: port) as Service }
let url: NSURL = NSURL(string: "http://example.com")!
let service1 = try! container.resolve(withArguments: url, 80) as Service
let service2 = try! container.resolve(withArguments: 80, url) as Service
let service3 = try! container.resolve(withArguments: 80, NSURL(string: "http://example.com")) as Service
let service4 = try! container.resolve(withArguments: 80, NSURL(string: "http://example.com")! as NSURL!) as Service
let service1 = try! container.resolve(arguments: url, 80) as Service
let service2 = try! container.resolve(arguments: 80, url) as Service
let service3 = try! container.resolve(arguments: 80, NSURL(string: "http://example.com")) as Service
(service1 as! ServiceImp4).name
(service2 as! ServiceImp4).name
(service3 as! ServiceImp4).name
(service4 as! ServiceImp4).name
/*:
Note that all of the services were resolved using different factories.
@@ -35,12 +32,16 @@ _Dip_ supports up to six runtime arguments. If that is not enougth you can exten
*/
extension DependencyContainer {
public func register<T, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7>(tag tag: Tag? = nil, _ scope: ComponentScope = .Prototype, factory: (Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7) throws -> T) -> DefinitionOf<T, (Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7) throws -> T> {
return registerFactory(tag: tag, scope: scope, factory: factory)
@discardableResult
public func register<T, A, B, C, D, E, F, G>(_ scope: ComponentScope = .shared, type: T.Type = T.self, tag: DependencyTagConvertible? = nil, factory: @escaping (A, B, C, D, E, F, G) throws -> T) -> Definition<T, (A, B, C, D, E, F, G)> {
return register(scope: scope, type: type, tag: tag, factory: factory, numberOfArguments: 7) { container, tag in
try factory(container.resolve(tag: tag), container.resolve(tag: tag), container.resolve(tag: tag), container.resolve(tag: tag), container.resolve(tag: tag), container.resolve(tag: tag), container.resolve(tag: tag))
}
}
public func resolve<T, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7>(tag tag: Tag? = nil, _ arg1: Arg1, _ arg2: Arg2, _ arg3: Arg3, _ arg4: Arg4, _ arg5: Arg5, _ arg6: Arg6, _ arg7: Arg7) throws -> T {
return try resolve(tag: tag) { (factory: (Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7) throws -> T) in try factory(arg1, arg2, arg3, arg4, arg5, arg6, arg7) }
public func resolve<T, A, B, C, D, E, F, G>(tag: DependencyTagConvertible? = nil, _ arg1: A, _ arg2: B, _ arg3: C, _ arg4: D, _ arg5: E, _ arg6: F, _ arg7: G) throws -> T {
return try resolve(tag: tag) { factory in try factory(arg1, arg2, arg3, arg4, arg5, arg6, arg7) }
}
}
@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
@@ -8,40 +8,60 @@ let container = DependencyContainer()
### Scopes
Dip supports three different scopes of objects: _Prototype_, _ObjectGraph_ and _Singleton_.
Dip supports three different scopes of objects: _Unique_, _Shared_ and _Singleton_.
* The `.Prototype` scope will make the `DependencyContainer` resolve your type as __a new instance every time__ you call `resolve`. This is the default scope.
* The `.ObjectGraph` scope is like `.Prototype` scope, but it will make the `DependencyContainer` to reuse resolved instances during one (recursive) call to `resolve` method. When this call returns, all resolved instances will be discarded and next call to `resolve` will produce new instances. This scope should be used to resolve [circular dependencies](Circular%20dependencies).
* The `.Singleton` scope will make the `DependencyContainer` retain the instance once resolved the first time, and reuse it in the next calls to `resolve` during the container lifetime.
* The `Unique` scope will make the `DependencyContainer` resolve your type as __a new instance every time__ you call `resolve`. This is the default scope.
* The `Shared` scope is like `Unique` scope, but it will make the `DependencyContainer` to reuse resolved instances during one (recursive) call to `resolve` method. When this call returns, all resolved instances will be discarded and next call to `resolve` will produce new instances. This scope should be used to resolve [circular dependencies](Circular%20dependencies).
* The `Singleton` scope will make the `DependencyContainer` retain the instance once resolved the first time, and reuse it in the next calls to `resolve` during the container lifetime.
* The `EagerSingleton` scope is the same as `Singleton` scope but instances with this scope will be created when you call `bootstrap()` method on the container.
* The `WeakSingleton` scope is the same as `Singleton` scope but instances are stored in container as weak references. This scope can be usefull when you need to recreate object graph without reseting container.
The `.Prototype` scope is the default. To set a scope you pass it as an argument to `register` method.
The `Unique` scope is the default. To set a scope you pass it as an argument to `register` method.
*/
container.register { ServiceImp1() as Service }
container.register(tag: "prototype", .Prototype) { ServiceImp1() as Service }
container.register(tag: "object graph", .ObjectGraph) { ServiceImp2() as Service }
container.register(tag: "shared instance", .Singleton) { ServiceImp3() as Service }
container.register(.unique, tag: "prototype") { ServiceImp1() as Service }
container.register(.shared, tag: "object graph") { ServiceImp2() as Service }
container.register(.singleton, tag: "shared instance") { ServiceImp3() as Service }
let service = try! container.resolve() as Service
let anotherService = try! container.resolve() as Service
// They are different instances as the scope defaults to .Prototype
// They are different instances as the scope defaults to .unique
service as! ServiceImp1 === anotherService as! ServiceImp1 // false
let prototypeService = try! container.resolve(tag: "prototype") as Service
let anotherPrototypeService = try! container.resolve(tag: "prototype") as Service
let anotherUniqueService = try! container.resolve(tag: "prototype") as Service
// They are different instances:
prototypeService as! ServiceImp1 === anotherPrototypeService as! ServiceImp1 // false
prototypeService === anotherUniqueService // false
let graphService = try! container.resolve(tag: "object graph") as Service
let anotherGraphService = try! container.resolve(tag: "object graph") as Service
// still different instances the ObjectGraph scope only keep instances during one (recursive) resolution call,
// still different instances the Shared scope only keep instances during one (recursive) resolution call,
// so the two calls on the two lines above are different calls and use different instances
graphService as! ServiceImp2 === anotherGraphService as! ServiceImp2 // false
graphService === anotherGraphService // false
let sharedService = try! container.resolve(tag: "shared instance") as Service
let sameSharedService = try! container.resolve(tag: "shared instance") as Service
// same instances, the singleton scope keep and reuse instances during the lifetime of the container
sharedService as! ServiceImp3 === sameSharedService as! ServiceImp3
/*:
### Bootstrapping
You can use `bootstrap()` method to fix your container setup and initialise components registered with `EagerSingleton` scope.
After bootstrapping if you try to add or remove any definition it will cause runtime exception. Call `boostrap` when you registered all the components, for example at the end of initialization block if you use `init(configBlock:)`.
*/
var resolvedEagerSingleton = false
let definition = container.register(.eagerSingleton, tag: "eager shared instance") { ServiceImp1() as Service }
.resolvingProperties { _ in resolvedEagerSingleton = true }
try! container.bootstrap()
resolvedEagerSingleton
let eagerSharedService = try! container.resolve(tag: "eager shared instance") as Service
container.remove(definition)
//: [Next: Circular Dependencies](@next)
@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
@@ -33,7 +33,7 @@ class ApiClientSingleton {
}
class MyViewControllerWithSingleton: UIViewController {
override func viewDidAppear(amimated: Bool) {
override func viewDidAppear(_ amimated: Bool) {
super.viewDidAppear(amimated)
ApiClientSingleton.sharedInstance.get("/users") { /* refresh your UI */ }
}
@@ -71,13 +71,13 @@ class MyViewController: UIViewController {
var apiClient: ApiClientProtocol!
override func viewDidAppear(amimated: Bool) {
super.viewDidAppear(amimated)
super.viewDidAppear(_ amimated)
apiClient.get("path") {}
}
convenience init(apiClient: ApiClientProtocol) {
self.init()
self.apiClient = apiClient
self.init()
}
init() {
@@ -90,7 +90,7 @@ class MyViewController: UIViewController {
}
//inject with constructor
let viewController = MyViewController(apiClient: ApiClient())
var viewController = MyViewController(apiClient: ApiClient())
//or with property
viewController.apiClient = ApiClient()
@@ -98,61 +98,18 @@ viewController.apiClient = ApiClient()
With Dip this code can look like this:
*/
let container = DependencyContainer { container in
container.register { ApiClient() as ApiClientProtocol }
}
let container = DependencyContainer()
container.register { ApiClient() as ApiClientProtocol }
class DipViewController: UIViewController {
var apiClient: ApiClientProtocol!
override func viewDidAppear(amimated: Bool) {
super.viewDidAppear(amimated)
apiClient.get("path") {}
}
convenience init(dependencies: DependencyContainer) {
self.init()
self.apiClient = try! dependencies.resolve() as ApiClientProtocol
}
init() {
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
var dipController = DipViewController(dependencies: container)
//inject with constructor
viewController = try MyViewController(apiClient: container.resolve())
//or with property
viewController.apiClient = try container.resolve()
/*:
Of cource `DependencyContainer` should not be a singleton too. Instead, inject it to objects that need to access it. And use a protocol for that. For example if your view controller needs to access API client, it does not need a reference to `DependencyContainer`, it only needs a reference to _something_ that can provide it an API client instance.
*/
Of cource `DependencyContainer` should not be a singleton too. There is just no need for that because you never should call `DependencyContainer` from inside of your components. That will make it a [service locator antipattern]((http://blog.ploeh.dk/2010/02/03/ServiceLocatorisanAnti-Pattern/)). You may only call `DependencyContainer` from the _Composition root_ - the place where all the components are configured and wired together.
protocol ApiClientProvider {
func apiClient() -> ApiClientProtocol
}
extension DependencyContainer: ApiClientProvider {
func apiClient() -> ApiClientProtocol {
return try! self.resolve() as ApiClientProtocol
}
}
extension DipViewController {
convenience init(apiClientProvider: ApiClientProvider) {
self.init()
self.apiClient = apiClientProvider.apiClient()
}
}
dipController = DipViewController(apiClientProvider: container)
/*:
This way you also does not depend directly on Dip. Instead you provide a boundary between Dip that you don't have control of and your source code. So when something chagnes in Dip, you update only the boundary code.
Dependency Injection is a pattern (more precisely - a set of patterns) as well as a singleton. And any pattern can be abused. DI can be used in a [wrong way]((http://www.loosecouplings.com/2011/01/dependency-injection-using-di-container.html)), container can easily become a [service locator](http://blog.ploeh.dk/2010/02/03/ServiceLocatorisanAnti-Pattern/). You should carefully decide when to use DI, you should not inject everything and everywhere and define a protocol for every single class you use. For every tool there is a right time and the same way as singleton can harm you the same way DI and protocols abuse can make your code unnececerry complex.
Dependency Injection is a pattern (more precisely - a set of patterns) as well as a singleton. And any pattern can be abused. DI can be used in a [wrong way]((http://www.loosecouplings.com/2011/01/dependency-injection-using-di-container.html)), container can easily become a service locator. You should carefully decide when to use DI, you should not inject everything and everywhere and define a protocol for every single class you use. For every tool there is a right time and the same way as singleton can harm you the same way DI and protocols abuse can make your code unnececerry complex.
If you want to know more about Dependency Injection in general we recomend you to read ["Dependency Injection in .Net" by Mark Seemann](https://www.manning.com/books/dependency-injection-in-dot-net). Dip was inspired by implementations of IoC container for .Net platform and shares core principles described in that book.
@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
@@ -1,37 +1,49 @@
//: [Previous: Shared Instances](@previous)
//: [Previous: Containers Collaboration](@previous)
//import XCTest
import Dip
let container = DependencyContainer()
/*:
### Testing
If you use Dependency Injection patterns like contructor and property injection it will be much easier
to unit test your components. When it comes to integration tests you may want to mock some real services.
In these tests you can register mock implementation in the container and it will be injected instead of the real implementation.
Dip is convenient to use for testing. Here is s simple example of how you can write tests with Dip.
__Note__: That's a very simple example just to demostrate use of Dip in tests, not how you should or should not tests your code in general.
> That's a very simple example just to demonstrate use of Dip in tests, not how you should or should not test your code in general.
You can learn more about testing based on state verification vs behavior verification [here](http://martinfowler.com/articles/mocksArentStubs.html).
> XCTest is not supported by playgrounds so to be able to compile this page we commented out XCTest specific code.
*/
protocol Service {
protocol ServiceType {
func doSomething()
}
class RealService: ServiceType {
func doSomething() {
//do something real
}
}
class Client {
var service: Service!
var service: ServiceType!
func callService() {
service.doSomething()
}
}
import XCTest
import Dip
/*:
Instead of the real `Service` implementation, provide a _fake_ implementation with test hooks that you need:
*/
class FakeService: Service {
class FakeService: ServiceType {
var doSomethingCalled = false
func doSomething() {
@@ -41,30 +53,60 @@ class FakeService: Service {
init() {}
}
class MyTests: XCTestCase {
var container: DependencyContainer!
override func setUp() {
super.setUp()
/*:
Register fake implementation as `Service`:
Somewhere in your production code you register real implementations:
*/
func configure(container: DependencyContainer) {
container.register { RealService() as ServiceType }
container.register { Client() }
.resolvingProperties { container, client in
client.service = try container.resolve()
}
}
class MyTests/*: XCTestCase*/ {
/*override*/ func setUp() {
//super.setUp()
/*:
Reset container configuration to normal state:
*/
container = DependencyContainer { container in
container.register { FakeService() as Service }
}
container.reset()
configure(container: container)
}
func testThatDoSomethingIsCalled() {
let sut = Client()
sut.service = try! container.resolve() as Service
/*:
Register fake implementation as `Service`:
*/
container.register { FakeService() as ServiceType }
let sut = try! container.resolve() as Client
sut.callService()
/*:
And finally you test it was called:
*/
XCTAssertTrue((sut.service as! FakeService).doSomethingCalled)
let service = sut.service as! FakeService
//XCTAssertTrue(service.doSomethingCalled)
}
}
/*:
You can also validate your container configuration. You can do that either in a separate test suit or when runnging application in `DEBUG` mode.
During validation container will try to resolve all the definitions registered in it. If some of definitions requires runtime arguments you can provide them as arguments to `validate` method. They should exactly match types of arguments required by factories. Multiple arguments for the single factory should be grouped in a tuple. If you don't provide arguments validation will fail.
*/
container.register { (url: NSURL, port: Int) in ServiceImp4(name: "1", baseURL: url, port: port) as Service }
try! container.validate((NSURL(string: "https://github.com/AliSoftware/Dip")!, 80))
do {
try container.validate()
}
catch {
print(error)
}
@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
@@ -0,0 +1,110 @@
//: [Previous: Auto-injection](@previous)
import Foundation
import Dip
let container = DependencyContainer()
/*:
### Type Forwarding
Very often we end up with single class that implements several protocols. This is normal even in [VIPER architecture](https://github.com/mutualmobile/VIPER-SWIFT/blob/master/VIPER-SWIFT/Classes/Modules/List/User%20Interface/Presenter/ListPresenter.swift#L12) that constantly strives for Single Responsibility Principle.
Let's look at example of VIPER architecture:
*/
extension ListPresenter: ListInteractorOutput, ListModuleInterface, AddModuleDelegate {}
extension ListInteractor: ListInteractorInput {}
extension AddPresenter: AddModuleInterface {}
/*:
In VIPER we need to create several objects (presenters, wireframes, interactors) which should be accessed thorugh different interfaces. We need to wire them all together so that we have the same instances in place for different types.
- `ListInteractor` referenced by `ListPresenter` in its `listInteractor` property (via `ListInteractorInput` protocol) should hold a backward reference to the same presenter in its `output` property
- `ListWireframe` referenced by `ListPresenter` should also hold a backward reference to the same presenter in its `listPresenter` property
- `AddWireframe` should hold a reference to `AddPresenter` that should hold reference to the same `ListPresenter` in its `addModuleDelegate` property (via `AddModuleDelegate` protocol).
We can achieve this result by explicitly rosolving concrete types:
*/
container.register { ListWireframe(addWireFrame: $0, listPresenter: $1) }
container.register { AddWireframe(addPresenter: $0) }
var listInteractorDefinition = container.register { ListInteractor() }
.resolvingProperties { container, interactor in
interactor.output = try container.resolve() as ListPresenter
}
var listPresenterDefinition = container.register { ListPresenter() }
.resolvingProperties { container, presenter in
presenter.listInteractor = try container.resolve() as ListInteractor
presenter.listWireframe = try container.resolve()
}
var addPresenterDefinition = container.register { AddPresenter() }
.resolvingProperties { container, presenter in
presenter.addModuleDelegate = try container.resolve() as ListPresenter
}
var addPresenter = try! container.resolve() as AddPresenter
var listPresenter = addPresenter.addModuleDelegate as! ListPresenter
var listInteractor = listPresenter.listInteractor as! ListInteractor
listInteractor.output === listPresenter
var listWireframe = listPresenter.listWireframe
listWireframe?.listPresenter === listPresenter
/*:
Alternatively we can use type-forwarding. With type-forwarding we register definition for one (source) type and also for another (forwarded) type. When container will try to resolve forwarded type it will use the same definition as for source type, and (if registered in `Shared` scope or as a singleton) will reuse the same instance. With that you don't need to resolve concrete types in definitions:
*/
listInteractorDefinition = container.register { ListInteractor() }
.resolvingProperties { container, interactor in
interactor.output = try container.resolve()
}
listPresenterDefinition = container.register { ListPresenter() }
.resolvingProperties { container, presenter in
presenter.listInteractor = try container.resolve()
presenter.listWireframe = try container.resolve()
}
addPresenterDefinition = container.register { AddPresenter() }
.resolvingProperties { container, presenter in
presenter.addModuleDelegate = try container.resolve()
}
/*:
And now we register definitions for type-forwarding:
*/
listInteractorDefinition
.implements(ListInteractorInput.self)
listPresenterDefinition
.implements(ListInteractorOutput.self)
.implements(ListModuleInterface.self)
.implements(AddModuleDelegate.self)
addPresenter = try! container.resolve() as AddPresenter
listPresenter = addPresenter.addModuleDelegate as! ListPresenter
listInteractor = listPresenter.listInteractor as! ListInteractor
listInteractor.output === listPresenter
listWireframe = listPresenter.listWireframe
listWireframe?.listPresenter === listPresenter
/*:
Type forwarding will work the same way whenever your resolve dependencies with property injection using `resolveDependencies` block, or with auto-injected properties, or with constructor injection and auto-wiring.
Registering definition for type forwarding will effectively register another definition in the container, linked with original one. So the same overriding rool will be applied for such registrations - last wins. If you need to register different definitions for the same type you should register them with different tags.
You can also provide `resolveDependencies` block for forwarded definition. First container will call `resolveDependencies` block of the source definition, and then of forwarded definition:
*/
listInteractorDefinition
.resolvingProperties { container, interactor in
print("resolved ListInteractor")
}
let _ = container.register(listInteractorDefinition, type: ListInteractorInput.self)
.resolvingProperties { container, interactor in
print("resolved ListInteractorInput")
}
addPresenter = try! container.resolve() as AddPresenter
//: [Next: Containers Collaboration](@next)
+34 -2
View File
@@ -1,6 +1,6 @@
import Foundation
public protocol Service {}
public protocol Service: AnyObject {}
public class ServiceImp1: Service {
public init() {}
@@ -22,7 +22,7 @@ public class ServiceImp4: Service {
}
public protocol Client: class {
public protocol Client: AnyObject {
var service: Service {get}
init(service: Service)
}
@@ -74,5 +74,37 @@ public class DataProviderImp: DataProvider {
public init() {}
}
public protocol ListInteractorOutput: AnyObject {}
public protocol ListModuleInterface: AnyObject {}
public protocol ListInteractorInput: AnyObject {}
public class ListPresenter: NSObject {
public var listInteractor : ListInteractorInput?
public var listWireframe : ListWireframe?
public override init() {}
}
public class ListInteractor: NSObject {
public var output : ListInteractorOutput?
public override init() {}
}
public class ListWireframe : NSObject {
public let addWireframe: AddWireframe
public let listPresenter: ListPresenter
public init(addWireFrame: AddWireframe, listPresenter: ListPresenter) {
self.addWireframe = addWireFrame
self.listPresenter = listPresenter
}
}
public protocol AddModuleDelegate: AnyObject {}
public protocol AddModuleInterface: AnyObject {}
public class AddWireframe: NSObject {
let addPresenter : AddPresenter
public init(addPresenter: AddPresenter) {
self.addPresenter = addPresenter
}
}
public class AddPresenter: NSObject {
public var addModuleDelegate : AddModuleDelegate?
public override init() {}
}
@@ -11,6 +11,8 @@
<page name='Shared Instances'/>
<page name='Auto-wiring'/>
<page name='Auto-injection'/>
<page name='Type Forwarding'/>
<page name='Containers Collaboration'/>
<page name='Testing'/>
</pages>
</playground>
+3
View File
@@ -0,0 +1,3 @@
source "https://rubygems.org"
gem "cocoapods", '=1.4.0'
+76
View File
@@ -0,0 +1,76 @@
GEM
remote: https://rubygems.org/
specs:
CFPropertyList (3.0.0)
activesupport (4.2.11)
i18n (~> 0.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
atomos (0.1.3)
claide (1.0.2)
cocoapods (1.4.0)
activesupport (>= 4.0.2, < 5)
claide (>= 1.0.2, < 2.0)
cocoapods-core (= 1.4.0)
cocoapods-deintegrate (>= 1.0.2, < 2.0)
cocoapods-downloader (>= 1.1.3, < 2.0)
cocoapods-plugins (>= 1.0.0, < 2.0)
cocoapods-search (>= 1.0.0, < 2.0)
cocoapods-stats (>= 1.0.0, < 2.0)
cocoapods-trunk (>= 1.3.0, < 2.0)
cocoapods-try (>= 1.1.0, < 2.0)
colored2 (~> 3.1)
escape (~> 0.0.4)
fourflusher (~> 2.0.1)
gh_inspector (~> 1.0)
molinillo (~> 0.6.4)
nap (~> 1.0)
ruby-macho (~> 1.1)
xcodeproj (>= 1.5.4, < 2.0)
cocoapods-core (1.4.0)
activesupport (>= 4.0.2, < 6)
fuzzy_match (~> 2.0.4)
nap (~> 1.0)
cocoapods-deintegrate (1.0.2)
cocoapods-downloader (1.2.2)
cocoapods-plugins (1.0.0)
nap
cocoapods-search (1.0.0)
cocoapods-stats (1.0.0)
cocoapods-trunk (1.3.1)
nap (>= 0.8, < 2.0)
netrc (~> 0.11)
cocoapods-try (1.1.0)
colored2 (3.1.2)
concurrent-ruby (1.1.4)
escape (0.0.4)
fourflusher (2.0.1)
fuzzy_match (2.0.4)
gh_inspector (1.1.3)
i18n (0.9.5)
concurrent-ruby (~> 1.0)
minitest (5.11.3)
molinillo (0.6.6)
nanaimo (0.2.6)
nap (1.1.0)
netrc (0.11.0)
ruby-macho (1.3.1)
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
xcodeproj (1.7.0)
CFPropertyList (>= 2.3.3, < 4.0)
atomos (~> 0.1.3)
claide (>= 1.0.2, < 2.0)
colored2 (~> 3.1)
nanaimo (~> 0.2.6)
PLATFORMS
ruby
DEPENDENCIES
cocoapods (= 1.4.0)
BUNDLED WITH
1.16.5
+8
View File
@@ -0,0 +1,8 @@
import XCTest
import DipTests
var tests = [XCTestCaseEntry]()
tests += DipTests.__allTests()
XCTMain(tests)
+9 -24
View File
@@ -1,30 +1,15 @@
//
// Dip
//
// Copyright (c) 2015 Olivier Halligon <olivier@halligon.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// swift-tools-version:5.0
import PackageDescription
let package = Package(
name: "Dip"
name: "Dip",
products: [
.library(name: "Dip", targets: ["Dip"]),
],
targets: [
.target(name: "Dip", dependencies: [], path: "Sources"),
.testTarget(name: "DipTests", dependencies: ["Dip"], path: "Tests"),
]
)
+145 -334
View File
@@ -1,10 +1,12 @@
# Dip
[![CI Status](http://img.shields.io/travis/AliSoftware/Dip.svg?style=flat)](https://travis-ci.org/AliSoftware/Dip)
[![CI Status](https://travis-ci.org/AliSoftware/Dip.svg?branch=develop)](https://travis-ci.org/AliSoftware/Dip)
[![Version](https://img.shields.io/cocoapods/v/Dip.svg?style=flat)](http://cocoapods.org/pods/Dip)
[![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![License](https://img.shields.io/cocoapods/l/Dip.svg?style=flat)](http://cocoapods.org/pods/Dip)
[![Platform](https://img.shields.io/cocoapods/p/Dip.svg?style=flat)](http://cocoapods.org/pods/Dip)
[![Swift Version](https://img.shields.io/badge/Swift-4.0--4.2-F16D39.svg?style=flat)](https://developer.apple.com/swift)
[![Swift Version](https://img.shields.io/badge/Linux-4.0--4.2-4BC51D.svg?style=flat)](https://developer.apple.com/swift)
![Animated Dipping GIF](cinnamon-pretzels-caramel-dipping.gif)
_Photo courtesy of [www.kevinandamanda.com](http://www.kevinandamanda.com/recipes/appetizer/homemade-soft-cinnamon-sugar-pretzel-bites-with-salted-caramel-dipping-sauce.html)_
@@ -15,364 +17,173 @@ _Photo courtesy of [www.kevinandamanda.com](http://www.kevinandamanda.com/recipe
It's aimed to be as simple as possible yet provide rich functionality usual for DI containers on other platforms. It's inspired by `.NET`'s [Unity Container](https://msdn.microsoft.com/library/ff647202.aspx) and other DI containers.
* You start by creating `let dc = DependencyContainer()` and **register all your dependencies, by associating a `protocol` to a `factory`**.
* Then you can call `dc.resolve()` to **resolve a `protocol` into an instance of a concrete type** using that `DependencyContainer`.
* You start by creating `let container = DependencyContainer()` and **registering your dependencies, by associating a _protocol_ or _type_ to a `factory`** using `container.register { MyService() as Service }`.
* Then you can call `container.resolve() as Service` to **resolve an instance of _protocol_ or _type_** using that `DependencyContainer`.
* You can easily use Dip along with **Storyboards and Nibs** . There is also a **[code generator](https://github.com/ilyapuchka/dipgen)** that can help to simplify registering new components.
This allows you to define the real, concrete types only in one place ([e.g. like this in your app](SampleApp/DipSampleApp/DependencyContainers.swift#L22-L27), and [resetting it in your `setUp` for each Unit Tests](SampleApp/Tests/SWAPIPersonProviderTests.swift#L17-L21)) and then [only work with `protocols` in your code](SampleApp/DipSampleApp/Providers/SWAPIStarshipProvider.swift#L12) (which only define an API contract), without worrying about the real implementation.
<details>
<summary>Basic usage</summary>
> You can easily use Dip along with Storyboards and Nibs - checkout [Dip-UI](https://github.com/AliSoftware/Dip-UI) extensions.
```swift
import Dip
## Advantages of DI and loose coupling
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
// Create the container
private let container = DependencyContainer { container in
// Register some factory. ServiceImp here implements protocol Service
container.register { ServiceImp() as Service }
}
* Define clear API contracts before even thinking about implementation, and make your code loosly coupled with the real implementation.
* Easily switch between implementations — as long as they respect the same API contact (the `protocol`), making your app modular and scalable.
* Greatly improve testability, as you can register a real instance in your app but a fake instance in your tests dedicated for testing / mocking the fonctionnality
* Enable parallel development in your team. You and your teammates can work independently on different parts of the app after you agree on the interfaces.
* As a bonus get rid of those `sharedInstances` and avoid the singleton pattern at all costs.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Resolve a concrete instance. Container will instantiate new instance of ServiceImp
let service = try! container.resolve() as Service
...
}
}
```
</details>
<details>
<summary>More sophisticated example</summary>
```swift
import Dip
class AppDelegate: UIResponder, UIApplicationDelegate {
private let container = DependencyContainer.configure()
...
}
//CompositionRoot.swift
import Dip
import DipUI
extension DependencyContainer {
static func configure() -> DependencyContainer {
return DependencyContainer { container in
unowned let container = container
DependencyContainer.uiContainers = [container]
container.register(tag: "ViewController") { ViewController() }
.resolvingProperties { container, controller in
controller.animationsFactory = try container.resolve() as AnimatonsFactory
}
container.register { AuthFormBehaviourImp(apiClient: $0) as AuthFormBehaviour }
container.register { container as AnimationsFactory }
container.register { view in ShakeAnimationImp(view: view) as ShakeAnimation }
container.register { APIClient(baseURL: NSURL(string: "http://localhost:2368")!) as ApiClient }
}
}
}
extension DependencyContainer: AnimationsFactory {
func shakeAnimation(view: UIView) -> ShakeAnimation {
return try! self.resolve(withArguments: view)
}
}
extension ViewController: StoryboardInstantiatable {}
//ViewController.swift
class ViewController {
var animationsFactory: AnimationsFactory?
private let _formBehaviour = Injected<AuthFormBehaviour>()
var formBehaviour: AuthFormBehaviour? {
return _formBehaviour.value
}
...
}
```
</details>
## Documentation & Usage Examples
Dip is completely [documented](http://cocoadocs.org/docsets/Dip/5.0.0/) and comes with a Playground that lets you try all its features and become familiar with API. You can find it in `Dip.xcworkspace`.
> Note: it may happen that you will need to build Dip framework before playground will be able to use it. For that select `Dip` scheme and build for iPhone Simulator.
You can find bunch of usage examples and usfull tips in a [wiki](../../wiki).
If your are using [VIPER](https://www.objc.io/issues/13-architecture/viper/) architecture - [here](https://github.com/ilyapuchka/VIPER-SWIFT) is VIPER demo app that uses Dip instead of manual dependency injection.
There are also several blog posts that describe how to use Dip and some of its implementation details:
- [IoC container in Swift](http://ilya.puchka.me/ioc-container-in-swift/)
- [IoC container in Swift. Circular dependencies and auto-injection](http://ilya.puchka.me/ioc-container-in-swift-circular-dependencies-and-auto-injection/)
- [Dependency injection with Dip](http://ilya.puchka.me/dependency-injecinjection-with-dip/)
File an issue if you have any question. Pull requests are warmly welcome too.
If you want to know more about Dependency Injection in general we recomend you to read ["Dependency Injection in .Net"](https://www.manning.com/books/dependency-injection-in-dot-net) by Mark Seemann. Dip was inspired particularly by implementations of some DI containers for .Net platform and shares core principles described in that book (even if you are not familiar with .Net platform the prenciples described in that book are platform agnostic).
## Features
- **[Scopes](../../wiki/scopes)**. Dip supports 5 different scopes (or life cycle strategies): _Unique_, _Shared_, _Singleton_, _EagerSingleton_, _WeakSingleton_;
- **[Auto-wiring](../../wiki/auto-wiring)** & **[Auto-injection](../../wiki/auto-injection)**. Dip can infer your components' dependencies injected in constructor and automatically resolve them as well as dependencies injected with properties.
- **[Resolving optionals](../../wiki/resolving-optionals)**. Dip is able to resolve constructor or property dependencies defined as optionals.
- **[Type forwarding](../../wiki/type-forwarding)**. You can register the same factory to resolve different types implemeted by a single class.
- **[Circular dependencies](../../wiki/circular-dependencies)**. Dip will be able to resolve circular dependencies if you will follow some simple rules;
- **[Storyboards integration](../../wiki/storyboards-integration)**. You can easily use Dip along with storyboards and Xibs without ever referencing container in your view controller's code;
- **[Named definitions](../../wiki/named-definitions)**. You can register different factories for the same protocol or type by registering them with [tags]();
- **[Runtime arguments](../../wiki/runtime-arguments)**. You can register factories that accept up to 6 runtime arguments (and extend it if you need);
- **[Easy configuration](../../wiki/containers-collaboration)** & **Code generation**. No complex containers hierarchy, no unneeded functionality. Tired of writing all registrations by hand? There is a [cool code generator](https://github.com/ilyapuchka/dipgen) that will create them for you. The only thing you need is to annotate your code with some comments.
- **Weakly typed components**. Dip can resolve "weak" types when they are unknown at compile time.
- **Thread safety**. Registering and resolving components is thread safe;
- **Helpful error messages and configuration validation**. You can validate your container configuration. If something can not be resolved at runtime Dip throws an error that completely describes the issue;
## Installation
Since version 4.3.1 Dip is built with Swift 2.2. The lates version built with Swift 2.1 is 4.3.0.
You can install Dip using your favorite dependency manager:
Dip is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:
<details>
<summary>CocoaPods</summary>
```ruby
pod "Dip"
```
`pod "Dip"`
If you use _Carthage_ add this line to your Cartfile:
</details>
<details>
<summary>Carthage</summary>
```
github "AliSoftware/Dip"
```
If you use [_Swift Package Manager_](https://swift.org/package-manager/) add Dip as dependency to you `Package.swift`:
To build for Swift 2.3 run Carthage with `--toolchain com.apple.dt.toolchain.Swift_2_3` option.
</details>
<details>
<summary>Swift Package Manager</summary>
```swift
.Package(url: "https://github.com/AliSoftware/Dip", majorVersion: 5, minor: 0)
```
let package = Package(
name: "MyPackage",
dependencies: [
.Package(url: "https://github.com/AliSoftware/Dip.git", "4.3.1")
]
)
```
</details>
## Running tests
On OSX you can run tests from Xcode. On Linux you need to have Swift Package Manager installed and use it to build test executable:
```
cd Dip/DipTests
swift build
./.build/debug/DipTests
```
## Playground
Dip comes with a **Playground** to introduce you to Inversion of Control, Dependency Injection, and how to use Dip in practice.
To play with it, [open `Dip.xcworkspace`](Dip/Dip.xcworkspace), then click on the `DipPlayground` entry in Xcode's Project Navigator and let it be your guide.
_Note: Do not open the `DipPlayground.playground` file directly, as it needs to be part of the workspace to access the Dip framework so that the demo code it contains can work._
The next paragraphs give you an overview of the Usage of _Dip_ directly, but if you're new to Dependency Injection, the Playground is probably a better start.
## Usage
### Register instance factories
First, create a `DependencyContainer` and use it to register instance factories with protocols, using those methods:
* `register() { … as SomeType }` will register provided factory with a given type.
* if you want to register an concrete implementation for some abstraction (protocol) you need **cast the instance to that protocol type** (e.g. `register { PlistUsersProvider() as UsersListProviderType }`).
* if you want just to register concrete type in container you may not need a type cast
Typically, to register your dependencies as early as possible in your app life-cycle, you will declare a `let dip: DependencyContainer = { … }()` somewhere, most likely in your `AppDelegate`. In unit tests you may configure container in each test method specifically and then reset it in `tearDown()`.
### Resolve dependencies
* `try resolve() as SomeType` will return a new instance matching the requested type (protocol or concrete type).
* `resolve()` is a generic method so you need to explicitly specify the return type (using `as` or explicitly providing type of a variable that will hold the resulting value) so that Swift's type inference knows which type you're trying to resolve.
```swift
container.register { ServiceImp() as Service }
let service = try! container.resolve() as Service
```
Ususally you will use _abstractions_ for your dependencies, but container can also resolve concrete types, if you register them. You can use that in cases where abstraction is not really required.
```swift
container.register { ServiceImp() }
let service: ServiceImp = try! container.resolve()
```
### Scopes
Dip provides three _scopes_ that you can use to register dependencies:
* The `.Prototype` scope will make the `DependencyContainer` resolve your type as __a new instance every time__ you call `resolve`. It's a default scope.
* The `.ObjectGraph` scope is like `.Prototype` scope but it will make the `DependencyContainer` to reuse resolved instances during one call to `resolve` method. When this call returns all resolved insances will be discarded and next call to `resolve` will produce new instances. This scope _must_ be used to properly resolve circular dependencies.
* The `.Singleton` scope will make the `DependencyContainer` retain the instance once resolved the first time, and reuse it in the next calls to `resolve` during the container lifetime.
You specify scope when you register dependency like that:
```swift
container.register() { ServiceImp() as Service } //.Prototype is a default
container.register(.ObjectGraph) { ServiceImp() as Service }
container.register(.Singleton) { ServiceImp() as Service }
```
### Using block-based initialization
When calling the initializer of `DependencyContainer()`, you can pass a block that will be called right after the initialization. This allows you to have a nice syntax to do all your `register(…)` calls in there, instead of having to do them separately.
It may not seem to provide much, but it gets very useful, because instead of having to do setup the container like this:
```swift
let dip: DependencyContainer = {
let dip = DependencyContainer()
dip.register { ProductionEnvironment(analytics: true) as EnvironmentType }
dip.register { WebService() as WebServiceAPI }
return dip
}()
```
you can instead write this exact equivalent code, which is more compact, and indent better in Xcode (as the final closing brack is properly aligned):
```swift
let dip = DependencyContainer { dip in
dip.register { ProductionEnvironment(analytics: true) as EnvironmentType }
dip.register { WebService() as WebServiceAPI }
}
```
### Using tags to associate various factories to one type
* If you give a `tag` in the parameter to `register()`, it will associate that instance or factory with this tag, which can be used later during `resolve` (see below).
* `resolve(tag: tag)` will try to find a factory that match both the requested protocol _and_ the tag. If it doesn't find any, it will fallback to the factory that only match the requested type.
* The tags can be `StringLiteralType` or `IntegerLiteralType`. That said you can use plain strings or integers as tags.
```swift
enum WebService: String {
case Production
case Development
var tag: DependencyContainer.Tag { return DependencyContainer.Tag.String(self.rawValue) }
}
let wsDependencies = DependencyContainer() { dip in
dip.register(tag: WebService.Production.tag) { URLSessionNetworkLayer(baseURL: "http://prod.myapi.com/api/")! as NetworkLayer }
dip.register(tag: WebService.Development.tag) { URLSessionNetworkLayer(baseURL: "http://dev.myapi.com/api/")! as NetworkLayer }
}
let networkLayer = try! dip.resolve(tag: WebService.PersonWS.tag) as NetworkLayer
```
### Runtime arguments
You can register factories that accept up to six arguments. When you resolve dependency you can pass those arguments to `resolve()` method and they will be passed to the factory. Note that _number_, _types_ and _order_ of parameters matters (see _Runtime arguments_ page of the Playground).
```swift
let webServices = DependencyContainer() { webServices in
webServices.register { (url: NSURL, port: Int) in WebServiceImp1(url, port: port) as WebServiceAPI }
}
let service = try! webServices.resolve(withArguments: NSURL(string: "http://example.url")!, 80) as WebServiceAPI
```
Though Dip provides support for up to six runtime arguments out of the box you can extend that.
### Circular dependencies
_Dip_ supports circular dependencies. For that you need to register your components with `ObjectGraph` scope and use `resolveDependencies` method of `DefinitionOf` returned by `register` method like this:
```swift
container.register(.ObjectGraph) {
ClientImp(server: try container.resolve() as Server) as Client
}
container.register(.ObjectGraph) { ServerImp() as Server }
.resolveDependencies { container, server in
server.client = try container.resolve() as Client
}
```
More information about circular dependencies you can find in the Playground.
### Auto-wiring
When you use constructor injection to inject dependencies in your component auto-wiring enables you to resolve it just with one call to `resolve` method without carying about how to resolve all constructor arguments - container will resolve them for you.
```swift
class PresenterImp: Presenter {
init(view: ViewOutput, interactor: Interactor, router: Router) { ... }
...
}
container.register { RouterImp() as Router }
container.register { View() as ViewOutput }
container.register { InteractorImp() as Interactor }
container.register { PresenterImp(view: $0, interactor: $1, router: $2) as Presenter }
let presenter = try! container.resolve() as Presenter
```
### Auto-injection
Auto-injection lets your resolve all property dependencies of the instance resolved by container with just one call, also allowing a simpler syntax to register circular dependencies.
```swift
protocol Server {
weak var client: Client? { get }
}
protocol Client: class {
var server: Server? { get }
}
class ServerImp: Server {
private var injectedClient = InjectedWeak<Client>()
var client: Client? { return injectedClient.value }
}
class ClientImp: Client {
private var injectedServer = Injected<Server>()
var server: Server? { get { return injectedServer.value} }
}
container.register(.ObjectGraph) { ServerImp() as Server }
container.register(.ObjectGraph) { ClientImp() as Client }
let client = try! container.resolve() as Client
```
You can find more use cases for auto-injection in the Playground available in this repository.
> Tip: You can use either `Injected<T>` and `InjectedWeak<T>` wrappers provided by Dip, or your own wrappers (even plain `Box<T>`) that conform to `AutoInjectedPropertyBox` protocol.
### Thread safety
`DependencyContainer` is thread safe, you can register and resolve components from different threads.
Still we encourage you to register components in the main thread early in the application lifecycle to prevent race conditions
when you try to resolve component from one thread while it was not yet registered in container by another thread.
### Errors
The resolve operation has a potential to fail because you can use the wrong type, factory or a wrong tag. For that reason Dip throws a `DipError` if it fails to resolve a type. Thus when calling `resolve` you need to use a `try` operator.
There are very rare use cases when your application can recover from this kind of error. In most of the cases you can use `try!` to cause an exception at runtime if error was thrown or `try?` if a dependency is optional. This way `try!` serves as an additional mark for developers that resolution can fail.
Dip also provides helpful descriptions for errors that can occur when you call `resolve`. See the source code documentation to know more about that.
### Concrete Example
Let's say you have some view model that depends on some data provider and web service:
```swift
struct WebService {
let env: EnvironmentType
init(env: EnvironmentType) {
self.env = env
}
func sendRequest(path: String, ) {
// use stuff like env.baseURL here
}
}
struct SomeViewModel {
let ws: WebServiceType
let friendsProvider: FriendsProviderType
init(friendsProvider: FriendsProviderType, webService: WebServiceType) {
self.friendsProvider = friendsProvider
self.ws = webService
}
func foo() {
ws.someMethodDeclaredOnWebServiceType()
let friends = friendsProvider.someFriendsProviderTypeMethod()
print("friends: \(friends)")
}
}
```
As you can see we have few layers of dependencies here. All of them together represent _dependency graph_.
To be able to resolve this graph with Dip we need to make it aware of those types.
For that we register the dependencies somewhere early in app life cycle (most likely in AppDelegate):
```swift
let dip: DependencyContainer = {
let dip = DependencyContainer()
let enableAnalytics = //i.e. read the setting from plist
dip.register(.Singleton) { ProductionEnvironment(analytics: enableAnalytics) as EnvironmentType }
dip.register(.Singleton) { WebService(env: try dip.resolve()) as WebServiceType }
dip.register() { userName in DummyFriendsProvider(user: name) as FriendsProviderType }
dip.register(tag: "me") { (_: String) in PlistFriendsProvider(plist: "myfriends") as FriendsProviderType }
dip.register() { userName in
let webService = try dip.resolve() as WebServiceType
let friendsProvider = try dip.resolve(tag: userName, withArguments: userName) as
return SomeViewModel(friendsProvider: freindsProvider, webService: webService)
}
return dip
}
```
> Do the same in your Unit Tests target & test cases, but obviously with different _implementations_ (test doubles) registered.
Then to resolve the graph use `dip.resolve()`, like this:
```swift
let viewModel = try! dip.resolve(withArguments: userName) as SomeViewModel
//now you can use view model or pass it to it's consumer
```
This way with just one call to `resolve()` you will have the whole graph of your dependencies resolved and ready to use:
* environmet will be resolved as a singleton instance of `ProductionEnvironment` with enabled analitycs;
* `ws` will be resolved as a singleton instance of `WebService` and will have it's `env` property set to `ProductionEnvironment`, already resolved (and reatined) by container.
* `friendsProvider` will be resolved as a new instance each time you create a view model, which will be an instance created via
`PlistFriendsProvider(plist: "myfriends")` if `userName` is `me` and created via `DummyFriendsProvider(userName)` for any other
`userName` value (because `resolve(tag: userName, withArguments: userName)` will fallback to `resolve(tag: nil, withArguments: userName)` in that case, using
the instance factory which was registered without a tag, but will pass `userName` as an argument).
* view model will be created using `init(friendsProvider:webService:)` with `friendsProvider` and `webService` that have been
already resolved by container.
When running your Unit tests target, it may be resolved with other instances, depending on how you registered your dependencies in your Test Case.
> Try to constrain calls to `resolve()` method to one place and try to use one call to `resolve()` to instantiate the whole graph of the dependencies.
The same should be applied to dependencies registration - it should be performed with one call and should be done in one place.
Don't scatter calls to container all around your code. Using `resolve` inside your implementations will be equal to creating dependencies directly and is actually against DI. Moreover it will drag the dependency on Dip everywhere and will make requirements of your types implicit instead of explicit.
Instead you should combine use of container with DI patterns like _Constructor Injection_ and _Property Injection_. Any DI container is just a tool, not a goal.
You should aplly DI patterns in your code first and only then think about using DI container as a tool to make dependencies management easier.
You will find some other advices on how to use the container in the Playground.
We hope that after reading this README and going through the Playground you will admit the benifits of DI and loose coupling that it enables whether you use Dip or not.
### Complete Example Project
In addition to this Usage overview and to the aforementioned playground, you can also find a complete example in the `SampleApp/DipSampleApp` project provided in this repository.
This sample project is a bit more complex, but closer to real-world applications (even if this sample is all about StarWars!),
by declaring protocols like `NetworkLayer` which can be resolved to a `URLSessionNetworkLayer` in the real app, but to a dummy
network layer returning fixture data during the Unit Tests.
This sample uses the Star Wars API provided by swapi.co to fetch Star Wars characters and starships info and display them in TableViews.
On OSX you can run tests from Xcode. On Linux you need to have Swift Package Manager installed and use it to build and run tests using this command: `swift build --clean && swift build && swift test`
## Credits
This library has been created by [**Olivier Halligon**](olivier@halligon.net).
I'd also like to thank [**Ilya Puchka**](https://twitter.com/ilyapuchka) for his big contribution to it, as he added a lot of great features to it.
This library has been created by [**Olivier Halligon**](olivier@halligon.net) and is maintained by [**Ilya Puchka**](https://twitter.com/ilyapuchka).
**Dip** is available under the **MIT license**. See the `LICENSE` file for more info.
@@ -279,7 +279,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0700;
LastUpgradeCheck = 1020;
ORGANIZATIONNAME = AliSoftware;
TargetAttributes = {
0990225E1BC123C000E76F43 = {
@@ -292,7 +292,7 @@
};
buildConfigurationList = 607FACCB1AFB9204008FA782 /* Build configuration list for PBXProject "DipSampleApp" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
@@ -414,6 +414,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.alisoftware.DipSampleApp;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
@@ -422,17 +423,28 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
@@ -460,6 +472,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
};
name = Debug;
};
@@ -467,17 +480,28 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
@@ -496,6 +520,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.3;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_VERSION = 4.0;
VALIDATE_PRODUCT = YES;
};
name = Release;
@@ -521,6 +546,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
};
name = Release;
};
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0700"
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
@@ -40,8 +40,8 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
codeCoverageEnabled = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
+2 -2
View File
@@ -16,11 +16,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
private let container = DependencyContainer()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
// Override point for customization after application launch.
//This is a composition root where container is configured and all dependencies are resolved
configureContainer(container)
configure(container: container)
let personProvider = try! container.resolve() as PersonProviderAPI
let starshipProvider = try! container.resolve() as StarshipProviderAPI
@@ -1,5 +1,15 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
@@ -31,6 +41,16 @@
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "29x29",
@@ -62,6 +82,16 @@
"idiom" : "ipad",
"filename" : "Icon-152.png",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
},
{
"idiom" : "ios-marketing",
"size" : "1024x1024",
"scale" : "1x"
}
],
"info" : {
+7 -7
View File
@@ -12,8 +12,8 @@ protocol BaseCell {
static var identifier: String { get }
static var nib: UINib? { get }
static func register(tableView: UITableView)
static func dequeueFromTableView(tableView: UITableView, forIndexPath indexPath: NSIndexPath) -> Self
static func register(_ tableView: UITableView)
static func dequeueFromTableView(_ tableView: UITableView, forIndexPath indexPath: IndexPath) -> Self
}
extension BaseCell where Self : UITableViewCell {
@@ -22,16 +22,16 @@ extension BaseCell where Self : UITableViewCell {
}
static var nib: UINib? { return nil }
static func register(tableView: UITableView) {
static func register(_ tableView: UITableView) {
if let cellNib = self.nib {
tableView.registerNib(cellNib, forCellReuseIdentifier: identifier)
tableView.register(cellNib, forCellReuseIdentifier: identifier)
} else {
tableView.registerClass(Self.self as AnyClass, forCellReuseIdentifier: identifier)
tableView.register(Self.self as AnyClass, forCellReuseIdentifier: identifier)
}
}
static func dequeueFromTableView(tableView: UITableView, forIndexPath indexPath: NSIndexPath) -> Self {
return tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! Self
static func dequeueFromTableView(_ tableView: UITableView, forIndexPath indexPath: IndexPath) -> Self {
return tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! Self
}
}
@@ -16,22 +16,22 @@ final class PersonCell : UITableViewCell, FillableCell {
@IBOutlet weak var hairLabel: UILabel!
@IBOutlet weak var eyesLabel: UILabel!
let heightFormatter: NSLengthFormatter = {
let f = NSLengthFormatter()
f.forPersonHeightUse = true
let heightFormatter: LengthFormatter = {
let f = LengthFormatter()
f.isForPersonHeightUse = true
return f
}()
let massFormatter: NSMassFormatter = {
let f = NSMassFormatter()
f.forPersonMassUse = true
let massFormatter: MassFormatter = {
let f = MassFormatter()
f.isForPersonMassUse = true
return f
}()
func fillWithObject(person: Person) {
func fillWithObject(object person: Person) {
nameLabel.text = person.name
genderImageView.image = person.gender.flatMap { UIImage(named: $0.rawValue) }
heightLabel.text = heightFormatter.stringFromValue(Double(person.height), unit: .Centimeter)
massLabel.text = massFormatter.stringFromValue(Double(person.mass), unit: .Kilogram)
heightLabel.text = heightFormatter.string(fromValue: Double(person.height), unit: .centimeter)
massLabel.text = massFormatter.string(fromValue: Double(person.mass), unit: .kilogram)
hairLabel.text = person.hairColor
eyesLabel.text = person.eyeColor
}
@@ -15,13 +15,13 @@ final class StarshipCell : UITableViewCell, FillableCell {
@IBOutlet weak var crewLabel: UILabel!
@IBOutlet weak var passengersLabel: UILabel!
let numberFormatter = NSNumberFormatter()
let numberFormatter = NumberFormatter()
func fillWithObject(starship: Starship) {
func fillWithObject(object starship: Starship) {
nameLabel.text = starship.name
modelLabel.text = starship.model
manufacturerLabel.text = starship.manufacturer
crewLabel.text = numberFormatter.stringFromNumber(starship.crew)
passengersLabel.text = numberFormatter.stringFromNumber(starship.passengers)
crewLabel.text = numberFormatter.string(from: NSNumber(integerLiteral: starship.crew))
passengersLabel.text = numberFormatter.string(from: NSNumber(integerLiteral: starship.passengers))
}
}
@@ -23,17 +23,17 @@ enum DependencyTags: Int, DependencyTagConvertible {
}
// MARK: Dependency Container for Providers
func configureContainer(dip: DependencyContainer) {
func configure(container dip: DependencyContainer) {
// Register the NetworkLayer, same for everyone here (but we have the ability to register a different one for a specific WebService if we wanted to)
dip.register(.Singleton) { URLSessionNetworkLayer(baseURL: "http://swapi.co/api/")! as NetworkLayer }
dip.register(.singleton) { URLSessionNetworkLayer(baseURL: "http://swapi.co/api/")! as NetworkLayer }
if FAKE_PERSONS {
// 1) Register fake persons provider
//Here we use constructor injection for one of the dependencies property injection for another, and we provide dependencies manually
dip.register() { FakePersonsProvider(dummyProvider: DummyPilotProvider()) as PersonProviderAPI }
.resolveDependencies { (_, resolved: PersonProviderAPI) in
.resolvingProperties { (_, resolved: PersonProviderAPI) in
//here we resolve optional dependencies
//see what happens when you comment this out
(resolved as! FakePersonsProvider).plistProvider = PlistPersonProvider(plist: "mainPilot")
@@ -60,7 +60,7 @@ func configureContainer(dip: DependencyContainer) {
//Here we use constructor injection, but instead of providing dependencies manually container resolves them for us
dip.register() {
FakeStarshipProvider(
dummyProvider: try dip.resolve(tag: DependencyTags.Dummy, withArguments: "Main Pilot"),
dummyProvider: try dip.resolve(tag: DependencyTags.Dummy, arguments: "Main Pilot"),
hardCodedProvider: try dip.resolve(tag: DependencyTags.Hardcoded)) as StarshipProviderAPI
}
+1 -1
View File
@@ -9,7 +9,7 @@
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<string>7.1.1</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
@@ -11,12 +11,12 @@ import Foundation
///Provides some dummy Person entities
struct DummyPilotProvider : PersonProviderAPI {
func fetchIDs(completion: [Int] -> Void) {
func fetchIDs(completion: @escaping ([Int]) -> Void) {
completion(Array(0..<5))
}
func fetch(id: Int, completion: Person? -> Void) {
completion(dummyPerson(id))
func fetch(id: Int, completion: @escaping (Person?) -> Void) {
completion(dummyPerson(idx: id))
}
private func dummyPerson(idx: Int) -> Person {
@@ -40,9 +40,9 @@ class PlistPersonProvider : PersonProviderAPI {
init(plist basename: String) {
guard
let path = NSBundle.mainBundle().pathForResource(basename, ofType: "plist"),
let path = Bundle.main.path(forResource: basename, ofType: "plist"),
let list = NSArray(contentsOfFile: path),
peopleDict = list as? [[String:AnyObject]]
let peopleDict = list as? [[String:AnyObject]]
else {
fatalError("PLIST for \(basename) not found")
}
@@ -50,11 +50,11 @@ class PlistPersonProvider : PersonProviderAPI {
self.people = peopleDict.map(PlistPersonProvider.personFromDict)
}
func fetchIDs(completion: [Int] -> Void) {
func fetchIDs(completion: @escaping ([Int]) -> Void) {
completion(Array(0..<people.count))
}
func fetch(id: Int, completion: Person? -> Void) {
func fetch(id: Int, completion: @escaping (Person?) -> Void) {
guard id < people.count else {
completion(nil)
return
@@ -65,12 +65,12 @@ class PlistPersonProvider : PersonProviderAPI {
private static func personFromDict(dict: [String:AnyObject]) -> Person {
guard
let name = dict["name"] as? String,
height = dict["height"] as? Int,
mass = dict["mass"] as? Int,
hairColor = dict["hairColor"] as? String,
eyeColor = dict["eyeColor"] as? String,
genderStr = dict["gender"] as? String,
starshipsIDs = dict["starships"] as? [Int]
let height = dict["height"] as? Int,
let mass = dict["mass"] as? Int,
let hairColor = dict["hairColor"] as? String,
let eyeColor = dict["eyeColor"] as? String,
let genderStr = dict["gender"] as? String,
let starshipsIDs = dict["starships"] as? [Int]
else {
fatalError("Invalid Plist")
}
@@ -98,16 +98,16 @@ class FakePersonsProvider: PersonProviderAPI {
self.dummyProvider = dummyProvider
}
func fetchIDs(completion: [Int] -> Void) {
dummyProvider.fetchIDs(completion)
func fetchIDs(completion: @escaping ([Int]) -> Void) {
dummyProvider.fetchIDs(completion: completion)
}
func fetch(id: Int, completion: Person? -> Void) {
if let plistProvider = plistProvider where id == 0 {
plistProvider.fetch(id, completion: completion)
func fetch(id: Int, completion: @escaping (Person?) -> Void) {
if let plistProvider = plistProvider, id == 0 {
plistProvider.fetch(id: id, completion: completion)
}
else {
dummyProvider.fetch(id, completion: completion)
dummyProvider.fetch(id: id, completion: completion)
}
}
@@ -12,13 +12,13 @@ import Foundation
struct DummyStarshipProvider : StarshipProviderAPI {
var pilotName: String
func fetchIDs(completion: [Int] -> Void) {
let nbShips = pilotName.characters.count
func fetchIDs(completion: @escaping ([Int]) -> Void) {
let nbShips = pilotName.count
completion(Array(0..<nbShips))
}
func fetch(id: Int, completion: Starship? -> Void) {
completion(dummyStarship(id))
func fetch(id: Int, completion: @escaping (Starship?) -> Void) {
completion(dummyStarship(idx: id))
}
private func dummyStarship(idx: Int) -> Starship {
@@ -42,11 +42,11 @@ class HardCodedStarshipProvider : StarshipProviderAPI {
Starship(name: "Third Ship", model: "AwesomeShip Cargo", manufacturer: "HardCoded Inc.", crew: 12, passengers: 150, pilotIDs: [2]),
] + Array(4..<75).map { Starship(name: "Ship #\($0)", model: "AwesomeShip Fighter", manufacturer: "HardCoded Inc.", crew: 1, passengers: 2, pilotIDs: [1]) }
func fetchIDs(completion: [Int] -> Void) {
func fetchIDs(completion: @escaping ([Int]) -> Void) {
completion(Array(0..<starships.count))
}
func fetch(id: Int, completion: Starship? -> Void) {
func fetch(id: Int, completion: @escaping (Starship?) -> Void) {
guard id < starships.count else {
completion(nil)
return
@@ -66,16 +66,16 @@ class FakeStarshipProvider: StarshipProviderAPI {
self.hardCodedProvider = hardCodedProvider
}
func fetchIDs(completion: [Int] -> Void) {
hardCodedProvider.fetchIDs(completion)
func fetchIDs(completion: @escaping ([Int]) -> Void) {
hardCodedProvider.fetchIDs(completion: completion)
}
func fetch(id: Int, completion: Starship? -> Void) {
func fetch(id: Int, completion: @escaping (Starship?) -> Void) {
if id == 0 {
dummyProvider.fetch(id, completion: completion)
dummyProvider.fetch(id: id, completion: completion)
}
else {
hardCodedProvider.fetch(id, completion: completion)
hardCodedProvider.fetch(id: id, completion: completion)
}
}
@@ -9,21 +9,21 @@
import Foundation
enum NetworkResponse {
case Success(NSData, NSHTTPURLResponse)
case Success(Data, HTTPURLResponse)
case Error(NSError)
func unwrap() throws -> (NSData, NSHTTPURLResponse) {
func unwrap() throws -> (Data, HTTPURLResponse) {
switch self {
case Success(let data, let response):
case .Success(let data, let response):
return (data, response)
case Error(let error):
case .Error(let error):
throw error
}
}
func json<T>() throws -> T {
let (data, _) = try self.unwrap()
let obj = try NSJSONSerialization.JSONObjectWithData(data, options: [])
let obj = try JSONSerialization.jsonObject(with: data, options: [])
guard let json = obj as? T else {
throw SWAPIError.InvalidJSON
}
@@ -32,5 +32,5 @@ enum NetworkResponse {
}
protocol NetworkLayer {
func request(path: String, completion: NetworkResponse -> Void)
func request(path: String, completion: @escaping (NetworkResponse) -> Void)
}
@@ -9,6 +9,6 @@
import Foundation
protocol PersonProviderAPI {
func fetchIDs(completion: [Int] -> Void)
func fetch(id: Int, completion: Person? -> Void)
func fetchIDs(completion: @escaping ([Int]) -> Void)
func fetch(id: Int, completion: @escaping (Person?) -> Void)
}
@@ -9,7 +9,7 @@
import Foundation
import Dip
enum SWAPIError: ErrorType {
enum SWAPIError: Error {
case InvalidJSON
}
@@ -8,7 +8,7 @@
import Foundation
///Provides Person entitis fetching them with web service
///Provides Person entities fetching them with web service
struct SWAPIPersonProvider : PersonProviderAPI {
let ws: NetworkLayer
@@ -20,15 +20,15 @@ struct SWAPIPersonProvider : PersonProviderAPI {
self.ws = webService
}
func fetchIDs(completion: [Int] -> Void) {
ws.request("people") { response in
func fetchIDs(completion: @escaping ([Int]) -> Void) {
ws.request(path: "people") { response in
do {
let dict = try response.json() as NSDictionary
guard let results = dict["results"] as? [NSDictionary] else { throw SWAPIError.InvalidJSON }
// Extract URLs (flatten to ignore invalid ones)
let urlStrings = results.flatMap({ $0["url"] as? String })
let ids = urlStrings.flatMap(idFromURLString)
let urlStrings = results.compactMap({ $0["url"] as? String })
let ids = urlStrings.compactMap(idFromURLString)
completion(ids)
}
@@ -38,14 +38,14 @@ struct SWAPIPersonProvider : PersonProviderAPI {
}
}
func fetch(id: Int, completion: Person? -> Void) {
ws.request("people/\(id)") { response in
func fetch(id: Int, completion: @escaping (Person?) -> Void) {
ws.request(path: "people/\(id)") { response in
do {
let json = try response.json() as NSDictionary
guard
let name = json["name"] as? String,
let heightStr = json["height"] as? String, height = Int(heightStr),
let massStr = json["mass"] as? String, mass = Int(massStr),
let heightStr = json["height"] as? String, let height = Int(heightStr),
let massStr = json["mass"] as? String, let mass = Int(massStr),
let hairColor = json["hair_color"] as? String,
let eyeColor = json["eye_color"] as? String,
let gender = json["gender"] as? String,
@@ -61,7 +61,7 @@ struct SWAPIPersonProvider : PersonProviderAPI {
hairColor: hairColor,
eyeColor: eyeColor,
gender: Gender(rawValue: gender),
starshipIDs: starshipURLStrings.flatMap(idFromURLString)
starshipIDs: starshipURLStrings.compactMap(idFromURLString)
)
completion(person)
}
@@ -20,15 +20,15 @@ struct SWAPIStarshipProvider : StarshipProviderAPI {
self.ws = webService
}
func fetchIDs(completion: [Int] -> Void) {
ws.request("starships") { response in
func fetchIDs(completion: @escaping ([Int]) -> Void) {
ws.request(path: "starships") { response in
do {
let dict = try response.json() as NSDictionary
guard let results = dict["results"] as? [NSDictionary] else { throw SWAPIError.InvalidJSON }
// Extract URLs (flatten to ignore invalid ones)
let urlStrings = results.flatMap({ $0["url"] as? String })
let ids = urlStrings.flatMap(idFromURLString)
let urlStrings = results.compactMap({ $0["url"] as? String })
let ids = urlStrings.compactMap(idFromURLString)
completion(ids)
}
@@ -38,16 +38,16 @@ struct SWAPIStarshipProvider : StarshipProviderAPI {
}
}
func fetch(id: Int, completion: Starship? -> Void) {
ws.request("starships/\(id)") { response in
func fetch(id: Int, completion: @escaping (Starship?) -> Void) {
ws.request(path: "starships/\(id)") { response in
do {
let json = try response.json() as NSDictionary
guard
let name = json["name"] as? String,
let model = json["model"] as? String,
let manufacturer = json["manufacturer"] as? String,
let crewStr = json["crew"] as? String, crew = Int(crewStr),
let passengersStr = json["passengers"] as? String, passengers = Int(passengersStr),
let crewStr = json["crew"] as? String, let crew = Int(crewStr),
let passengersStr = json["passengers"] as? String, let passengers = Int(passengersStr),
let pilotIDStrings = json["pilots"] as? [String]
else {
throw SWAPIError.InvalidJSON
@@ -59,7 +59,7 @@ struct SWAPIStarshipProvider : StarshipProviderAPI {
manufacturer: manufacturer,
crew: crew,
passengers: passengers,
pilotIDs: pilotIDStrings.flatMap(idFromURLString)
pilotIDs: pilotIDStrings.compactMap(idFromURLString)
)
completion(ship)
}
@@ -9,6 +9,6 @@
import Foundation
protocol StarshipProviderAPI {
func fetchIDs(completion: [Int] -> Void)
func fetch(id: Int, completion: Starship? -> Void)
func fetchIDs(completion: @escaping ([Int]) -> Void)
func fetch(id: Int, completion: @escaping (Starship?) -> Void)
}
@@ -10,33 +10,33 @@ import Foundation
///NetworkLayer implementation on top of NSURLSession
struct URLSessionNetworkLayer : NetworkLayer {
let baseURL: NSURL
let session: NSURLSession
let responseQueue: dispatch_queue_t
let baseURL: URL
let session: URLSession
let responseQueue: DispatchQueue
init?(baseURL: String, session: NSURLSession = .sharedSession(), responseQueue: dispatch_queue_t = dispatch_get_main_queue()) {
guard let url = NSURL(string: baseURL) else { return nil }
init?(baseURL: String, session: URLSession = URLSession.shared, responseQueue: DispatchQueue = DispatchQueue.main) {
guard let url = URL(string: baseURL) else { return nil }
self.init(baseURL: url, session: session)
}
init(baseURL: NSURL, session: NSURLSession = .sharedSession(), responseQueue: dispatch_queue_t = dispatch_get_main_queue()) {
init(baseURL: URL, session: URLSession = URLSession.shared, responseQueue: DispatchQueue = DispatchQueue.main) {
self.baseURL = baseURL
self.session = session
self.responseQueue = responseQueue
}
func request(path: String, completion: NetworkResponse -> Void) {
let url = self.baseURL.URLByAppendingPathComponent(path)
let task = session.dataTaskWithURL(url) { data, response, error in
if let data = data, let response = response as? NSHTTPURLResponse {
dispatch_async(self.responseQueue) {
func request(path: String, completion: @escaping (NetworkResponse) -> Void) {
let url = self.baseURL.appendingPathComponent(path)
let task = session.dataTask(with: url) { data, response, error in
if let data = data, let response = response as? HTTPURLResponse {
self.responseQueue.async() {
completion(NetworkResponse.Success(data, response))
}
}
else {
let err = error ?? NSError(domain: NSURLErrorDomain, code: NSURLError.Unknown.rawValue, userInfo: nil)
dispatch_async(self.responseQueue) {
completion(NetworkResponse.Error(err))
let err = error ?? NSError(domain: NSURLErrorDomain, code: URLError.unknown.rawValue, userInfo: nil)
self.responseQueue.async() {
completion(NetworkResponse.Error(err as NSError))
}
}
}
@@ -21,7 +21,7 @@ extension StoryboardScene where Self.RawValue == String {
}
func viewController() -> UIViewController {
return Self.storyboard().instantiateViewControllerWithIdentifier(self.rawValue)
return Self.storyboard().instantiateViewController(withIdentifier: self.rawValue)
}
static func viewController(identifier: Self) -> UIViewController {
return identifier.viewController()
@@ -8,14 +8,14 @@
import UIKit
protocol FetchableTrait: class {
protocol FetchableTrait: AnyObject {
associatedtype ObjectType
var objects: [ObjectType]? { get set }
var batchRequestID: Int { get set }
var tableView: UITableView! { get }
func fetchIDs(completion: [Int] -> Void)
func fetchOne(id: Int, completion: ObjectType? -> Void)
func fetchIDs(completion: @escaping ([Int]) -> Void)
func fetchOne(id: Int, completion: @escaping (ObjectType?) -> Void)
var fetchProgress: (current: Int, total: Int?) { get set }
}
@@ -27,10 +27,10 @@ extension FetchableTrait {
objects?.removeAll()
fetchProgress = (0,objectIDs.count)
for objectID in objectIDs {
fetchOne(objectID) { (object: ObjectType?) in
fetchOne(id: objectID) { (object: ObjectType?) in
// Exit if we failed to retrive an object for this ID, or if the request
// should be ignored because a new batch request has been started since
guard let object = object where batch == self.batchRequestID else { return }
guard let object = object, batch == self.batchRequestID else { return }
if self.objects == nil { self.objects = [] }
self.objects?.append(object)
@@ -46,7 +46,7 @@ extension FetchableTrait {
fetchProgress = (0, nil)
fetchIDs() { objectIDs in
guard batch == self.batchRequestID else { return }
self.loadObjects(objectIDs)
self.loadObjects(objectIDs: objectIDs)
}
}
@@ -63,8 +63,8 @@ extension FetchableTrait {
}
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
label.text = text
label.textColor = .grayColor()
label.font = .systemFontOfSize(12)
label.textColor = .gray
label.font = UIFont.systemFont(ofSize: 12)
label.sizeToFit()
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: label)
}
@@ -14,45 +14,46 @@ class PersonListViewController: UITableViewController, FetchableTrait {
var personProvider: PersonProviderAPI!
var starshipProvider: StarshipProviderAPI!
func fetchIDs(completion: [Int] -> Void) {
return personProvider.fetchIDs(completion)
func fetchIDs(completion: @escaping ([Int]) -> Void) {
return personProvider.fetchIDs(completion: completion)
}
func fetchOne(personID: Int, completion: Person? -> Void) {
return personProvider.fetch(personID, completion: completion)
func fetchOne(id personID: Int, completion: @escaping (Person?) -> Void) {
return personProvider.fetch(id: personID, completion: completion)
}
var fetchProgress: (current: Int, total: Int?) = (0, nil) {
didSet {
displayProgressInNavBar(self.navigationItem)
displayProgressInNavBar(navigationItem: self.navigationItem)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard
let id = segue.identifier, segueID = UIStoryboard.Segue.Main(rawValue: id)
where segueID == .StarshipsSegue,
let id = segue.identifier,
let segueID = UIStoryboard.Segue.Main(rawValue: id),
segueID == .StarshipsSegue,
let indexPath = self.tableView.indexPathForSelectedRow,
let destVC = segue.destinationViewController as? StarshipListViewController,
let destVC = segue.destination as? StarshipListViewController,
let person = self.objects?[indexPath.row]
else {
fatalError()
}
destVC.starshipProvider = starshipProvider
destVC.loadObjects(person.starshipIDs)
destVC.loadObjects(objectIDs: person.starshipIDs)
}
}
extension PersonListViewController {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects?.count ?? 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let object = self.objects?[indexPath.row] else { fatalError() }
let cell = PersonCell.dequeueFromTableView(tableView, forIndexPath: indexPath)
cell.fillWithObject(object)
cell.fillWithObject(object: object)
return cell
}
}
@@ -16,44 +16,45 @@ class StarshipListViewController : UITableViewController, FetchableTrait {
var starshipProvider: StarshipProviderAPI!
var personProvider: PersonProviderAPI!
func fetchIDs(completion: [Int] -> Void) {
starshipProvider.fetchIDs(completion)
func fetchIDs(completion: @escaping ([Int]) -> Void) {
starshipProvider.fetchIDs(completion: completion)
}
func fetchOne(shipID:Int, completion: Starship? -> Void) {
starshipProvider.fetch(shipID, completion: completion)
func fetchOne(id shipID:Int, completion: @escaping (Starship?) -> Void) {
starshipProvider.fetch(id: shipID, completion: completion)
}
var fetchProgress: (current: Int, total: Int?) = (0, nil) {
didSet {
displayProgressInNavBar(self.navigationItem)
displayProgressInNavBar(navigationItem: self.navigationItem)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard
let id = segue.identifier, segueID = UIStoryboard.Segue.Main(rawValue: id)
where segueID == .PilotsSegue,
let id = segue.identifier,
let segueID = UIStoryboard.Segue.Main(rawValue: id),
segueID == .PilotsSegue,
let indexPath = self.tableView.indexPathForSelectedRow,
let destVC = segue.destinationViewController as? PersonListViewController,
let destVC = segue.destination as? PersonListViewController,
let starship = self.objects?[indexPath.row]
else {
fatalError()
}
destVC.personProvider = personProvider
destVC.loadObjects(starship.pilotIDs)
destVC.loadObjects(objectIDs: starship.pilotIDs)
}
}
extension StarshipListViewController {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects?.count ?? 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let object = self.objects?[indexPath.row] else { fatalError() }
let cell = StarshipCell.dequeueFromTableView(tableView, forIndexPath: indexPath)
cell.fillWithObject(object)
cell.fillWithObject(object: object)
return cell
}
}
+1 -1
View File
@@ -9,7 +9,7 @@
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<string>7.1.1</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
+8 -8
View File
@@ -14,24 +14,24 @@ var wsDependencies = DependencyContainer()
// MARK: - Mock object used for tests
struct NetworkMock : NetworkLayer {
let fakeData: NSData?
let fakeData: Data?
init(json: AnyObject) {
init(json: Any) {
do {
fakeData = try NSJSONSerialization.dataWithJSONObject(json, options: [])
fakeData = try JSONSerialization.data(withJSONObject: json, options: [])
} catch {
fakeData = nil
}
}
func request(path: String, completion: NetworkResponse -> Void) {
let fakeURL = NSURL(string: "stub://")!.URLByAppendingPathComponent(path)
func request(path: String, completion: @escaping (NetworkResponse) -> Void) {
let fakeURL = NSURL(string: "http://stub")?.appendingPathComponent(path)
if let data = fakeData {
let response = NSHTTPURLResponse(URL: fakeURL, statusCode: 200, HTTPVersion: "1.1", headerFields:nil)!
let response = HTTPURLResponse(url: fakeURL!, statusCode: 200, httpVersion: "1.1", headerFields:nil)!
completion(.Success(data, response))
} else {
let response = NSHTTPURLResponse(URL: fakeURL, statusCode: 204, HTTPVersion: "1.1", headerFields:nil)!
completion(.Success(NSData(), response))
let response = HTTPURLResponse(url: fakeURL!, statusCode: 204, httpVersion: "1.1", headerFields:nil)!
completion(.Success(Data(), response))
}
}
}
@@ -10,10 +10,10 @@ import XCTest
import Dip
class SWAPIPersonProviderTests: XCTestCase {
let fakePerson1 = ["name": "John Doe", "mass": "72", "height": "172", "eye_color": "brown", "hair_color": "black", "gender": "male",
"starships": ["stub://starship/7/", "stub://starship/15"], "url": "stub://people/1"]
let fakePerson2 = ["name": "Jane Doe", "mass": "63", "height": "167", "eye_color": "blue", "hair_color": "red", "gender": "female",
"starships": ["stub://starship/11/"], "url": "stub://people/12"]
let fakePerson1: [String : Any] = ["name": "John Doe", "mass": "72", "height": "172", "eye_color": "brown", "hair_color": "black", "gender": "male",
"starships": ["http://starship/7/", "http://starship/15"], "url": "http://people/1"]
let fakePerson2: [String: Any] = ["name": "Jane Doe", "mass": "63", "height": "167", "eye_color": "blue", "hair_color": "red", "gender": "female",
"starships": ["http://starship/11/"], "url": "http://people/12"]
override func setUp() {
super.setUp()
@@ -23,7 +23,7 @@ class SWAPIPersonProviderTests: XCTestCase {
func testFetchPersonIDs() {
let mock = NetworkMock(json: ["results": [fakePerson1, fakePerson2]])
wsDependencies.register(.Singleton) { mock as NetworkLayer }
wsDependencies.register(.singleton) { mock as NetworkLayer }
let provider = SWAPIPersonProvider(webService: try! wsDependencies.resolve())
provider.fetchIDs { personIDs in
@@ -37,10 +37,10 @@ class SWAPIPersonProviderTests: XCTestCase {
func testFetchOnePerson() {
let mock = NetworkMock(json: fakePerson1)
wsDependencies.register(.Singleton) { mock as NetworkLayer }
wsDependencies.register(.singleton) { mock as NetworkLayer }
let provider = SWAPIPersonProvider(webService: try! wsDependencies.resolve())
provider.fetch(1) { person in
provider.fetch(id: 1) { person in
XCTAssertNotNil(person)
XCTAssertEqual(person?.name, "John Doe")
XCTAssertEqual(person?.mass, 72)
@@ -57,10 +57,10 @@ class SWAPIPersonProviderTests: XCTestCase {
func testFetchInvalidPerson() {
let json = ["error":"whoops"]
let mock = NetworkMock(json: json)
wsDependencies.register(.Singleton) { mock as NetworkLayer }
wsDependencies.register(.singleton) { mock as NetworkLayer }
let provider = SWAPIPersonProvider(webService: try! wsDependencies.resolve())
provider.fetch(12) { person in
provider.fetch(id: 12) { person in
XCTAssertNil(person)
}
}
@@ -10,10 +10,10 @@ import XCTest
import Dip
class SWAPIStarshipProviderTests: XCTestCase {
let fakeShip1 = ["name": "Falcon", "model": "Fighter", "manufacturer": "Fake Industries", "crew": "7", "passengers": "15",
"pilots": ["stub://people/1/", "stub://people/9"], "url": "stub://starship/4"]
let fakeShip2 = ["name": "Voyager", "model": "Cargo", "manufacturer": "Fake Industries", "crew": "18", "passengers": "150",
"pilots": ["stub://people/2/", "stub://people/3"], "url": "stub://starship/31"]
let fakeShip1: [String: Any] = ["name": "Falcon", "model": "Fighter", "manufacturer": "Fake Industries", "crew": "7", "passengers": "15",
"pilots": ["http://people/1/", "http://people/9"], "url": "http://starship/4"]
let fakeShip2: [String: Any] = ["name": "Voyager", "model": "Cargo", "manufacturer": "Fake Industries", "crew": "18", "passengers": "150",
"pilots": ["http://people/2/", "http://people/3"], "url": "http://starship/31"]
override func setUp() {
super.setUp()
@@ -23,7 +23,7 @@ class SWAPIStarshipProviderTests: XCTestCase {
func testFetchStarshipIDs() {
let mock = NetworkMock(json: ["results": [fakeShip1, fakeShip2]])
wsDependencies.register(.Singleton) { mock as NetworkLayer }
wsDependencies.register(.singleton) { mock as NetworkLayer }
let provider = SWAPIStarshipProvider(webService: try! wsDependencies.resolve())
provider.fetchIDs { shipIDs in
@@ -38,10 +38,10 @@ class SWAPIStarshipProviderTests: XCTestCase {
func testFetchOneStarship() {
let mock = NetworkMock(json: fakeShip1)
wsDependencies.register(.Singleton) { mock as NetworkLayer }
wsDependencies.register(.singleton) { mock as NetworkLayer }
let provider = SWAPIStarshipProvider(webService: try! wsDependencies.resolve())
provider.fetch(1) { starship in
provider.fetch(id: 1) { starship in
XCTAssertNotNil(starship)
XCTAssertEqual(starship?.name, "Falcon")
XCTAssertEqual(starship?.model, "Fighter")
@@ -57,10 +57,10 @@ class SWAPIStarshipProviderTests: XCTestCase {
func testFetchInvalidStarship() {
let json = ["error":"whoops"]
let mock = NetworkMock(json: json)
wsDependencies.register(.Singleton) { mock as NetworkLayer }
wsDependencies.register(.singleton) { mock as NetworkLayer }
let provider = SWAPIStarshipProvider(webService: try! wsDependencies.resolve())
provider.fetch(12) { starship in
provider.fetch(id: 12) { starship in
XCTAssertNil(starship)
}
}
+269 -82
View File
@@ -27,19 +27,31 @@ extension DependencyContainer {
/**
Resolves properties of passed object wrapped with `Injected<T>` or `InjectedWeak<T>`
*/
func autoInjectProperties(instance: Any) throws {
try Mirror(reflecting: instance).children.forEach(_resolveChild)
func autoInjectProperties(in instance: Any) throws {
let mirror = Mirror(reflecting: instance)
//mirror only contains class own properties
//so we need to walk through super class mirrors
//to resolve super class auto-injected properties
var superClassMirror = mirror.superclassMirror
while superClassMirror != nil {
try superClassMirror?.children.forEach(resolveChild)
superClassMirror = superClassMirror?.superclassMirror
}
try mirror.children.forEach(resolveChild)
}
private func _resolveChild(child: Mirror.Child) throws {
private func resolveChild(child: Mirror.Child) throws {
//HOTFIX for https://bugs.swift.org/browse/SR-2282
guard !String(describing: type(of: child.value)).has(prefix: "ImplicitlyUnwrappedOptional") else { return }
guard let injectedPropertyBox = child.value as? AutoInjectedPropertyBox else { return }
do {
let wrappedType = type(of: injectedPropertyBox).wrappedType
let contextKey = DefinitionKey(type: wrappedType, typeOfArguments: Void.self, tag: context.tag)
try inContext(key:contextKey, injectedInType: context?.resolvingType, injectedInProperty: child.label, logErrors: false) {
try injectedPropertyBox.resolve(self)
}
catch {
throw DipError.AutoInjectionFailed(label: child.label, type: injectedPropertyBox.dynamicType.wrappedType, underlyingError: error)
}
}
}
@@ -49,6 +61,7 @@ extension DependencyContainer {
instead of using `Injected<T>` or `InjectedWeak<T>` types.
**Example**:
```swift
class MyCustomBox<T> {
private(set) var value: T?
@@ -65,7 +78,7 @@ extension DependencyContainer {
```
*/
public protocol AutoInjectedPropertyBox: class {
public protocol AutoInjectedPropertyBox {
///The type of wrapped property.
static var wrappedType: Any.Type { get }
@@ -75,64 +88,144 @@ public protocol AutoInjectedPropertyBox: class {
- parameter container: A container to be used to resolve an instance
-note: This method is not intended to be called manually, `DependencyContainer` will call it by itself.
- note: This method is not intended to be called manually, `DependencyContainer` will call it by itself.
*/
func resolve(container: DependencyContainer) throws
func resolve(_ container: DependencyContainer) throws
}
#if swift(>=5.1)
/**
Use this wrapper to identify _strong_ properties of the instance that should be
auto-injected by `DependencyContainer`. Type T can be any type.
- warning: Do not define this property as optional or container will not be able to inject it.
Instead define it with initial value of `Injected<T>()`.
**Example**:
```swift
class ClientImp: Client {
@Injected var service: Service?
}
```
- seealso: `InjectedWeak`
*/
@propertyWrapper
public struct Injected<T>: _InjectedPropertyBox, AutoInjectedPropertyBox {
let valueBox: NullableBox<T> = NullableBox(nil)
///Wrapped value.
public var wrappedValue: T? {
get {
return valueBox.unboxed
}
set {
guard (required && newValue != nil) || !required else {
fatalError("Can not set required property to nil.")
}
valueBox.unboxed = newValue
}
}
let required: Bool
let didInject: (T) -> ()
let tag: DependencyContainer.Tag?
let overrideTag: Bool
public init(wrappedValue initialValue: T?) {
self.init()
}
}
#else
/**
Use this wrapper to identify _strong_ properties of the instance that should be
auto-injected by `DependencyContainer`. Type T can be any type.
- warning: Do not define this property as optional or container will not be able to inject it.
Instead define it with initial value of `Injected<T>()`.
**Example**:
```swift
class ClientImp: Client {
var service = Injected<Service>()
}
```
- seealso: `InjectedWeak`
*/
public final class Injected<T>: _InjectedPropertyBox<T>, AutoInjectedPropertyBox {
*/
public struct Injected<T>: _InjectedPropertyBox, AutoInjectedPropertyBox {
let valueBox: NullableBox<T> = NullableBox(nil)
public static var wrappedType: Any.Type {
return T.self
///Wrapped value.
public var value: T? {
return valueBox.unboxed
}
///Wrapped value.
public private(set) var value: T? {
didSet {
if let value = value { didInject(value) }
let required: Bool
let didInject: (T) -> ()
let tag: DependencyContainer.Tag?
let overrideTag: Bool
/// Returns a new wrapper with provided value.
func setValue(_ value: T?) -> Injected {
guard (required && value != nil) || !required else {
fatalError("Can not set required property to nil.")
}
return Injected(value: value, required: required, tag: tag, overrideTag: overrideTag, didInject: didInject)
}
}
#endif
public extension Injected {
///The type of wrapped property.
static var wrappedType: Any.Type {
return T.self
}
init(value: T?, required: Bool = true, tag: DependencyTagConvertible?, overrideTag: Bool, didInject: @escaping (T) -> ()) {
self.init(required: required, tag: tag, overrideTag: overrideTag, didInject: didInject)
self.valueBox.unboxed = value
}
init(required: Bool = true, tag: DependencyTagConvertible?, overrideTag: Bool, didInject: @escaping (T) -> () = { _ in }) {
self.required = required
self.tag = tag?.dependencyTag
self.overrideTag = overrideTag
self.didInject = didInject
}
/**
Creates a new wrapper for auto-injected property.
- parameters:
- required: Defines if the property is required or not.
If container fails to inject required property it will als fail to resolve
- required: Defines if the property is required or not.
If container fails to inject required property it will als fail to resolve
the instance that defines that property. Default is `true`.
- tag: An optional tag to use to lookup definitions when injecting this property. Default is `nil`.
- didInject: block that will be called when concrete instance is injected in this property.
- didInject: Block that will be called when concrete instance is injected in this property.
Similar to `didSet` property observer. Default value does nothing.
*/
public override init(required: Bool = true, tag: DependencyTagConvertible? = nil, didInject: T -> () = { _ in }) {
super.init(required: required, tag: tag, didInject: didInject)
*/
init(required: Bool = true, didInject: @escaping (T) -> () = { _ in }) {
self.init(value: nil, required: required, tag: nil, overrideTag: false, didInject: didInject)
}
public func resolve(container: DependencyContainer) throws {
let resolved: T? = try super.resolve(container)
value = resolved
init(required: Bool = true, tag: DependencyTagConvertible?, didInject: @escaping (T) -> () = { _ in }) {
self.init(value: nil, required: required, tag: tag, overrideTag: true, didInject: didInject)
}
func resolve(_ container: DependencyContainer) throws {
let resolved: T? = try self.resolve(with: container, tag: tag, overrideTag: overrideTag, required: required)
valueBox.unboxed = resolved
if let resolved = resolved {
didInject(resolved)
}
}
}
#if swift(>=5.1)
/**
Use this wrapper to identify _weak_ properties of the instance that should be
auto-injected by `DependencyContainer`. Type T should be a **class** type.
@@ -144,7 +237,7 @@ public final class Injected<T>: _InjectedPropertyBox<T>, AutoInjectedPropertyBox
For that reason if you resolve instance that has a _weak_ auto-injected property this property
will be released when `resolve` will complete.
Use `InjectedWeak<T>` to define one of two circular dependecies if another dependency is defined as `Injected<U>`.
Use `InjectedWeak<T>` to define one of two circular dependencies if another dependency is defined as `Injected<U>`.
This will prevent a retain cycle between resolved instances.
- warning: Do not define this property as optional or container will not be able to inject it.
@@ -154,34 +247,122 @@ public final class Injected<T>: _InjectedPropertyBox<T>, AutoInjectedPropertyBox
```swift
class ServiceImp: Service {
var client = InjectedWeak<Client>()
@InjectedWeak var client: Client?
}
```
- seealso: `Injected`
*/
public final class InjectedWeak<T>: _InjectedPropertyBox<T>, AutoInjectedPropertyBox {
@propertyWrapper
public struct InjectedWeak<T>: _InjectedPropertyBox, AutoInjectedPropertyBox {
//Only classes (means AnyObject) can be used as `weak` properties
//but we can not make <T: AnyObject> because that will prevent using protocol as generic type
//so we just rely on user reading documentation and passing AnyObject in runtime
//also we will throw fatal error if type can not be casted to AnyObject during resolution.
public static var wrappedType: Any.Type {
return T.self
}
weak var _value: AnyObject? = nil {
didSet {
if let value = value { didInject(value) }
let valueBox: WeakBox<T> = WeakBox(nil)
///Wrapped value.
public var wrappedValue: T? {
get {
return valueBox.value
}
set {
guard (required && newValue != nil) || !required else {
fatalError("Can not set required property to nil.")
}
valueBox.unboxed = newValue as AnyObject
}
}
let required: Bool
let didInject: (T) -> ()
let tag: DependencyContainer.Tag?
let overrideTag: Bool
public init(wrappedValue initialValue: T?) {
self.init()
}
}
#else
/**
Use this wrapper to identify _weak_ properties of the instance that should be
auto-injected by `DependencyContainer`. Type T should be a **class** type.
Otherwise it will cause runtime exception when container will try to resolve the property.
Use this wrapper to define one of two circular dependencies to avoid retain cycle.
- note: The only difference between `InjectedWeak` and `Injected` is that `InjectedWeak` uses
_weak_ reference to store underlying value, when `Injected` uses _strong_ reference.
For that reason if you resolve instance that has a _weak_ auto-injected property this property
will be released when `resolve` will complete.
Use `InjectedWeak<T>` to define one of two circular dependencies if another dependency is defined as `Injected<U>`.
This will prevent a retain cycle between resolved instances.
- warning: Do not define this property as optional or container will not be able to inject it.
Instead define it with initial value of `InjectedWeak<T>()`.
**Example**:
```swift
class ServiceImp: Service {
var client = InjectedWeak<Client>()
}
```
- seealso: `Injected`
*/
public struct InjectedWeak<T>: _InjectedPropertyBox, AutoInjectedPropertyBox {
//Only classes (means AnyObject) can be used as `weak` properties
//but we can not make <T: AnyObject> because that will prevent using protocol as generic type
//so we just rely on user reading documentation and passing AnyObject in runtime
//also we will throw fatal error if type can not be casted to AnyObject during resolution.
let valueBox: WeakBox<T> = WeakBox(nil)
///Wrapped value.
public var value: T? {
return _value as? T
return valueBox.value
}
let required: Bool
let didInject: (T) -> ()
let tag: DependencyContainer.Tag?
let overrideTag: Bool
/// Returns a new wrapper with provided value.
func setValue(_ value: T?) -> InjectedWeak {
guard (required && value != nil) || !required else {
fatalError("Can not set required property to nil.")
}
return InjectedWeak(value: value, required: required, tag: tag, overrideTag: overrideTag, didInject: didInject)
}
}
#endif
public extension InjectedWeak {
///The type of wrapped property.
static var wrappedType: Any.Type {
return T.self
}
init(value: T?, required: Bool = true, tag: DependencyTagConvertible?, overrideTag: Bool, didInject: @escaping (T) -> ()) {
self.init(required: required, tag: tag, overrideTag: overrideTag, didInject: didInject)
self.valueBox.unboxed = value as AnyObject
}
init(required: Bool = true, tag: DependencyTagConvertible?, overrideTag: Bool, didInject: @escaping (T) -> () = { _ in }) {
self.required = required
self.tag = tag?.dependencyTag
self.overrideTag = overrideTag
self.didInject = didInject
}
/**
@@ -192,46 +373,52 @@ public final class InjectedWeak<T>: _InjectedPropertyBox<T>, AutoInjectedPropert
If container fails to inject required property it will als fail to resolve
the instance that defines that property. Default is `true`.
- tag: An optional tag to use to lookup definitions when injecting this property. Default is `nil`.
- didInject: block that will be called when concrete instance is injected in this property.
- didInject: Block that will be called when concrete instance is injected in this property.
Similar to `didSet` property observer. Default value does nothing.
*/
public override init(required: Bool = true, tag: DependencyTagConvertible? = nil, didInject: T -> () = { _ in }) {
super.init(required: required, tag: tag, didInject: didInject)
init(required: Bool = true, didInject: @escaping (T) -> () = { _ in }) {
self.init(value: nil, required: required, tag: nil, overrideTag: false, didInject: didInject)
}
public func resolve(container: DependencyContainer) throws {
let resolved: T? = try super.resolve(container)
if required && !(resolved is AnyObject) {
fatalError("\(T.self) can not be casted to AnyObject. InjectedWeak wrapper should be used to wrap only classes.")
init(required: Bool = true, tag: DependencyTagConvertible?, didInject: @escaping (T) -> () = { _ in }) {
self.init(value: nil, required: required, tag: tag, overrideTag: true, didInject: didInject)
}
func resolve(_ container: DependencyContainer) throws {
let resolved: T? = try self.resolve(with: container, tag: tag, overrideTag: overrideTag, required: required)
valueBox.unboxed = resolved as AnyObject
if let resolved = resolved {
didInject(resolved)
}
_value = resolved as? AnyObject
}
}
protocol _InjectedPropertyBox {}
extension _InjectedPropertyBox {
func resolve<T>(with container: DependencyContainer, tag: DependencyContainer.Tag?, overrideTag: Bool, required: Bool) throws -> T? {
let tag = overrideTag ? tag : container.context.tag
do {
container.context.key = container.context.key.tagged(with: tag)
let key = DefinitionKey(type: T.self, typeOfArguments: Void.self, tag: tag?.dependencyTag)
return try resolve(with: container, key: key, builder: { (factory: (Any) throws -> Any) in try factory(()) }) as? T
}
catch {
let error = DipError.autoInjectionFailed(label: container.context.injectedInProperty, type: container.context.resolvingType, underlyingError: error)
if required {
throw error
}
else {
log(level: .Errors, error)
return nil
}
}
}
func resolve<U>(with container: DependencyContainer, key: DefinitionKey, builder: ((U) throws -> Any) throws -> Any) throws -> Any {
return try container._resolve(key: key, builder: { definition throws -> Any in
try builder(definition.weakFactory)
})
}
}
private class _InjectedPropertyBox<T> {
let required: Bool
let didInject: T -> ()
let tag: DependencyContainer.Tag?
init(required: Bool = true, tag: DependencyTagConvertible?, didInject: T -> () = { _ in }) {
self.required = required
self.tag = tag?.dependencyTag
self.didInject = didInject
}
private func resolve(container: DependencyContainer) throws -> T? {
let resolved: T?
if required {
resolved = try container.resolve(tag: tag) as T
}
else {
resolved = try? container.resolve(tag: tag) as T
}
return resolved
}
}
+50 -70
View File
@@ -22,85 +22,65 @@
// THE SOFTWARE.
//
protocol AutoWiringDefinition: DefinitionType {
var numberOfArguments: Int { get }
var autoWiringFactory: ((DependencyContainer, DependencyContainer.Tag?) throws -> Any)? { get }
}
extension DependencyContainer {
/// Tries to resolve instance using auto-wire factories
func _resolveByAutoWiring<T>(key: DefinitionKey) throws -> T? {
typealias NoArgumentsFactory = () throws -> T
guard key.factoryType == NoArgumentsFactory.self else { return nil }
/// Tries to resolve instance using auto-wiring
func autowire<T>(key aKey: DefinitionKey) throws -> T {
let key = aKey
guard key.typeOfArguments == Void.self else {
throw DipError.definitionNotFound(key: key)
}
let autoWiringDefinitions = self.autoWiringDefinitionsFor(T.self, tag: key.associatedTag)
return try _resolveEnumeratingKeys(autoWiringDefinitions, tag: key.associatedTag)
}
private func autoWiringDefinitionsFor(type: Any.Type, tag: DependencyContainer.Tag?) -> [(DefinitionKey, _Definition)] {
var definitions = self.definitions
.map({ ($0.0, $0.1 as! _Definition) })
let autoWiringKey = try autoWiringDefinition(byKey: key).key
//filter definitions
definitions = definitions
.filter({ $0.1.supportsAutoWiring() })
.filter({ $0.0.protocolType == type })
.filter({ $0.0.associatedTag == tag || $0.0.associatedTag == nil })
//order definitions
definitions = definitions
.sort({ $0.1.numberOfArguments > $1.1.numberOfArguments })
definitions =
//first will try to use tagged definitions
definitions.filter({ $0.0.associatedTag == tag }) +
//then will use not tagged definitions
definitions.filter({ $0.0.associatedTag != tag })
return definitions
}
/// Tries definitions one by one until one of them succeeds, otherwise returns nil
private func _resolveEnumeratingKeys<T>(definitions: [(DefinitionKey, _Definition)], tag: DependencyContainer.Tag?) throws -> T? {
for (index, definition) in definitions.enumerate() {
//If the next definition matches current definition then they are ambigous
if case definition? = definitions[next: index] {
throw DipError.AmbiguousDefinitions(
type: definition.0.protocolType,
definitions: [definition.1, definitions[next: index]!.1]
)
}
if let resolved: T = _resolveKey(definition.0, tag: tag) {
return resolved
do {
let key = autoWiringKey.tagged(with: key.tag ?? context.tag)
return try _resolve(key: key) { definition in
try definition.autoWiringFactory!(self, key.tag) as! T
}
}
return nil
catch {
throw DipError.autoWiringFailed(type: key.type, underlyingError: error)
}
}
private func _resolveKey<T>(key: DefinitionKey, tag: DependencyContainer.Tag?) -> T? {
let key = DefinitionKey(protocolType: key.protocolType, factoryType: key.factoryType, associatedTag: tag)
return try? _resolveKey(key, builder: { definition throws -> T in
guard let resolved = try definition._autoWiringFactory!(self, tag) as? T else {
fatalError("Internal inconsistency exception! Expected type: \(T.self); Definition: \(definition)")
private func autoWiringDefinition(byKey key: DefinitionKey) throws -> KeyDefinitionPair {
do {
return try autoWiringDefinition(byKey: key, strictByTag: true)
} catch {
if key.tag != nil {
return try autoWiringDefinition(byKey: key, strictByTag: false)
} else {
throw error
}
return resolved
})
}
}
private func autoWiringDefinition(byKey key: DefinitionKey, strictByTag: Bool) throws -> KeyDefinitionPair {
var definitions = self.definitions.map({ (key: $0.0, definition: $0.1) })
definitions = filter(definitions: definitions, byKey: key, strictByTag: strictByTag)
definitions = definitions.sorted(by: { $0.definition.numberOfArguments > $1.definition.numberOfArguments })
guard definitions.count > 0 && definitions[0].definition.numberOfArguments > 0 else {
throw DipError.definitionNotFound(key: key)
}
let maximumNumberOfArguments = definitions.first?.definition.numberOfArguments
definitions = definitions.filter({ $0.definition.numberOfArguments == maximumNumberOfArguments })
//when there are several definitions with the same number of arguments but different arguments types
if definitions.count > 1 && definitions[0].key.typeOfArguments != definitions[1].key.typeOfArguments {
let error = DipError.ambiguousDefinitions(type: key.type, definitions: definitions.map({ $0.definition }))
throw DipError.autoWiringFailed(type: key.type, underlyingError: error)
} else {
return definitions[0]
}
}
}
extension CollectionType {
subscript(safe index: Index) -> Generator.Element? {
guard indices.contains(index) else { return nil }
return self[index]
}
subscript(next index: Index) -> Generator.Element? {
return self[safe: index.advancedBy(1)]
}
}
/// Definitions are matched if they are registered for the same tag and thier factoris accept the same number of runtime arguments.
private func ~=(lhs: (DefinitionKey, _Definition), rhs: (DefinitionKey, _Definition)) -> Bool {
return
lhs.0.protocolType == rhs.0.protocolType &&
lhs.0.associatedTag == rhs.0.associatedTag &&
lhs.1.numberOfArguments == rhs.1.numberOfArguments
}
+5
View File
@@ -0,0 +1,5 @@
extension String {
func has(prefix aPrefix: String) -> Bool {
return hasPrefix(aPrefix)
}
}
+120
View File
@@ -0,0 +1,120 @@
//
// Dip
//
// Copyright (c) 2015 Olivier Halligon <olivier@halligon.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///Component scope defines a strategy used by the `DependencyContainer` to manage resolved instances life cycle.
public enum ComponentScope {
/**
A new instance will be created every time it's resolved.
This is a default strategy. Use this strategy when you don't want instances to be shared
between different consumers (i.e. if it is not thread safe).
**Example**:
```
container.register { ServiceImp() as Service }
container.register {
ServiceConsumerImp(
service1: try container.resolve() as Service
service2: try container.resolve() as Service
) as ServiceConsumer
}
let consumer = container.resolve() as ServiceConsumer
consumer.service1 !== consumer.service2 //true
```
*/
case unique
/**
Instance resolved with the same definition will be reused until topmost `resolve(tag:)` method returns.
When you resolve the same object graph again the container will create new instances.
Use this strategy if you want different object in objects graph to share the same instance.
- warning: Make sure this component is thread safe or accessed always from the same thread.
**Example**:
```
container.register { ServiceImp() as Service }
container.register {
ServiceConsumerImp(
service1: try container.resolve() as Service
service2: try container.resolve() as Service
) as ServiceConsumer
}
let consumer1 = container.resolve() as ServiceConsumer
let consumer2 = container.resolve() as ServiceConsumer
consumer1.service1 === consumer1.service2 //true
consumer2.service1 === consumer2.service2 //true
consumer1.service1 !== consumer2.service1 //true
```
*/
case shared
/**
Resolved instance will be retained by the container and always reused.
Do not mix this life cycle with _singleton pattern_.
Instance will be not shared between different containers unless they collaborate.
- warning: Make sure this component is thread safe or accessed always from the same thread.
- note: When you override or remove definition from the container an instance
that was resolved with this definition will be released. When you reset
the container it will release all singleton instances.
**Example**:
```
container.register(.singleton) { ServiceImp() as Service }
container.register {
ServiceConsumerImp(
service1: try container.resolve() as Service
service2: try container.resolve() as Service
) as ServiceConsumer
}
let consumer1 = container.resolve() as ServiceConsumer
let consumer2 = container.resolve() as ServiceConsumer
consumer1.service1 === consumer1.service2 //true
consumer2.service1 === consumer2.service2 //true
consumer1.service1 === consumer2.service1 //true
```
*/
case singleton
/**
The same scope as a `Singleton`, but instance will be created when container is bootstrapped.
- seealso: `bootstrap()`
*/
case eagerSingleton
/**
The same scope as a `Singleton`, but container stores week reference to the resolved instance.
While a strong reference to the resolved instance exists resolve will return the same instance.
After the resolved instance is deallocated next resolve will produce a new instance.
*/
case weakSingleton
}
+239 -164
View File
@@ -23,226 +23,301 @@
//
///A key used to store definitons in a container.
public struct DefinitionKey : Hashable, CustomStringConvertible {
public let protocolType: Any.Type
public let factoryType: Any.Type
public let associatedTag: DependencyContainer.Tag?
init(protocolType: Any.Type, factoryType: Any.Type, associatedTag: DependencyContainer.Tag? = nil) {
self.protocolType = protocolType
self.factoryType = factoryType
self.associatedTag = associatedTag
public struct DefinitionKey: Hashable, CustomStringConvertible {
public let type: Any.Type
public let typeOfArguments: Any.Type
public private(set) var tag: DependencyContainer.Tag?
init(type: Any.Type, typeOfArguments: Any.Type, tag: DependencyContainer.Tag? = nil) {
self.type = type
self.typeOfArguments = typeOfArguments
self.tag = tag
}
public var hashValue: Int {
return "\(protocolType)-\(factoryType)-\(associatedTag)".hashValue
public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(type))
hasher.combine(ObjectIdentifier(typeOfArguments))
hasher.combine(tag.desc)
}
public var description: String {
return "type: \(protocolType), factory: \(factoryType), tag: \(associatedTag.desc)"
return "type: \(String(reflecting: type)), arguments: \(typeOfArguments), tag: \(tag.desc)"
}
func tagged(with tag: DependencyContainer.Tag?) -> DefinitionKey {
var tagged = self
tagged.tag = tag
return tagged
}
/// Check two definition keys on equality by comparing their `type`, `factoryType` and `tag` properties.
public static func ==(lhs: DefinitionKey, rhs: DefinitionKey) -> Bool {
return
lhs.type == rhs.type &&
lhs.typeOfArguments == rhs.typeOfArguments &&
lhs.tag.desc == rhs.tag.desc
}
}
/// Check two definition keys on equality by comparing their `protocolType`, `factoryType` and `associatedTag` properties.
public func ==(lhs: DefinitionKey, rhs: DefinitionKey) -> Bool {
return
lhs.protocolType == rhs.protocolType &&
lhs.factoryType == rhs.factoryType &&
lhs.associatedTag == rhs.associatedTag
}
///Component scope defines a strategy used by the `DependencyContainer` to manage resolved instances life cycle.
public enum ComponentScope {
/**
A new instance will be created every time it's resolved.
This is a default strategy. Use this strategy when you don't want instances to be shared
between different consumers (i.e. if it is not thread safe).
**Example**:
```
container.register { ServiceImp() as Service }
container.register {
ServiceConsumerImp(
service1: try container.resolve() as Service
service2: try container.resolve() as Service
) as ServiceConsumer
}
let consumer = container.resolve() as ServiceConsumer
consumer.service1 !== consumer.service2 //true
```
*/
case Prototype
/**
Instance resolved with the same definition will be reused until topmost `resolve(tag:)` method returns.
When you resolve the same object graph again the container will create new instances.
Use this strategy if you want different object in objects graph to share the same instance.
- warning: Make sure this component is thread safe or accessed always from the same thread.
**Example**:
```
container.register(.ObjectGraph) { ServiceImp() as Service }
container.register {
ServiceConsumerImp(
service1: try container.resolve() as Service
service2: try container.resolve() as Service
) as ServiceConsumer
}
let consumer1 = container.resolve() as ServiceConsumer
let consumer2 = container.resolve() as ServiceConsumer
consumer1.service1 === consumer1.service2 //true
consumer2.service1 === consumer2.service2 //true
consumer1.service1 !== consumer2.service1 //true
```
*/
case ObjectGraph
/**
Resolved instance will be retained by the container and always reused.
Do not mix this life cycle with _singleton pattern_.
Instance will be not shared between different containers.
- warning: Make sure this component is thread safe or accessed always from the same thread.
- note: When you override or remove definition from the container an instance
that was resolved with this definition will be released. When you reset
the container it will release all singleton instances.
**Example**:
```
container.register(.Singleton) { ServiceImp() as Service }
container.register {
ServiceConsumerImp(
service1: try container.resolve() as Service
service2: try container.resolve() as Service
) as ServiceConsumer
}
let consumer1 = container.resolve() as ServiceConsumer
let consumer2 = container.resolve() as ServiceConsumer
consumer1.service1 === consumer1.service2 //true
consumer2.service1 === consumer2.service2 //true
consumer1.service1 === consumer2.service1 //true
```
*/
case Singleton
}
///Dummy protocol to store definitions for different types in collection
public protocol DefinitionType: AnyObject { }
/**
`DefinitionOf<T, F>` describes how instances of type `T` should be created when this type is resolved by the `DependencyContainer`.
`Definition<T, U>` describes how instances of type `T` should be created when this type is resolved by the `DependencyContainer`.
- `T` is the type of the instance to resolve
- `F` is the type of the factory that will create an instance of T.
- `U` is the type of runtime arguments accepted by factory that will create an instance of T.
For example `DefinitionOf<Service, (String) -> Service>` is the type of definition that will create an instance of type `Service` using factory that accepts `String` argument.
For example `Definition<Service, String>` is the type of definition that will create an instance of type `Service` using factory that accepts `String` argument.
*/
public final class DefinitionOf<T, F>: Definition {
public final class Definition<T, U>: DefinitionType {
public typealias F = (U) throws -> T
//MARK: - _Definition
weak var container: DependencyContainer?
let factory: F
let scope: ComponentScope
var weakFactory: ((Any) throws -> Any)!
var resolveProperties: ((DependencyContainer, Any) throws -> ())?
var autoInjectProperties: Bool?
init(scope: ComponentScope, factory: @escaping F) {
self.factory = factory
self.scope = scope
}
/**
Set the block that will be used to resolve dependencies of the instance.
This block will be called before `resolve(tag:)` returns. It can be set only once.
This block will be called before `resolve(tag:)` returns.
- parameter block: The block to use to resolve dependencies of the instance.
- parameter block: The block to resolve property dependencies of the instance.
- returns: modified definition
- note: To resolve circular dependencies at least one of them should use this block
to resolve its dependencies. Otherwise the application will enter an infinite loop and crash.
to resolve its dependencies. Otherwise the application will enter an infinite loop and crash.
- note: You can call this method several times on the same definition.
Container will call all provided blocks in the same order.
**Example**
```swift
container.register { ClientImp(service: try container.resolve() as Service) as Client }
container.register { ServiceImp() as Service }
.resolveDependencies { container, service in
.resolvingProperties { container, service in
service.client = try container.resolve() as Client
}
}
```
*/
public func resolveDependencies(block: (DependencyContainer, T) throws -> ()) -> DefinitionOf<T, F> {
guard resolveDependenciesBlock == nil else {
fatalError("You can not change resolveDependencies block after it was set.")
@discardableResult public func resolvingProperties(_ block: @escaping (DependencyContainer, T) throws -> ()) -> Definition {
if let oldBlock = self.resolveProperties {
self.resolveProperties = {
try oldBlock($0, $1 as! T)
try block($0, $1 as! T)
}
}
else {
self.resolveProperties = { try block($0, $1 as! T) }
}
self.resolveDependenciesBlock = block
return self
}
@discardableResult public func resolvingProperty<Root, V>(_ keyPath: ReferenceWritableKeyPath<Root, V>, as type: Any.Type = V.self, tag: DependencyTagConvertible? = nil) -> Definition {
return resolvingProperties { (container, instance) in
precondition(instance is Root, "Type of resolved instance \(Swift.type(of: instance)) does not match expected type \(Root.self)")
let resolved = try container.resolve(type, tag: tag)
precondition(resolved is V, "Type of resolved property \(Swift.type(of: resolved)) does not match expected type \(type)")
(instance as! Root)[keyPath: keyPath] = resolved as! V
}
}
@discardableResult public func resolvingProperty<Root, V>(_ keyPath: ReferenceWritableKeyPath<Root, V>, factory: @escaping (DependencyContainer) throws -> V = { try $0.resolve() }) -> Definition {
return resolvingProperties { (container, instance) in
precondition(instance is Root, "Type of resolved instance \(Swift.type(of: instance)) does not match expected type \(Root.self)")
(instance as! Root)[keyPath: keyPath] = try factory(container)
}
}
/**
Whether container should perform properties auto-injection when resolving using this definition.
If called will override container configuration. Can be called together with `resolvingProperties`
to resolve properties that are not automatically injected.
- parameter shouldAutoInject: Whether container should perform properties auto-injection when resolving using this definition. Default is `true`.
*/
@discardableResult public func autoInjectingProperties(_ shouldAutoInject: Bool = true) -> Definition {
autoInjectProperties = shouldAutoInject
return self
}
/// Calls `resolveDependencies` block if it was set.
func resolveDependenciesOf(resolvedInstance: Any, withContainer container: DependencyContainer) throws {
guard let resolvedInstance = resolvedInstance as? T else { return }
try self.resolveDependenciesBlock?(container, resolvedInstance)
func resolveProperties(of instance: Any, container: DependencyContainer) throws {
guard let resolvedInstance = instance as? T else { return }
if let forwardsTo = forwardsTo {
try forwardsTo.resolveProperties(of: resolvedInstance, container: container)
}
if let resolveProperties = self.resolveProperties {
try resolveProperties(container, resolvedInstance)
}
}
let factory: F
private(set) var scope: ComponentScope = .Prototype
//MARK: - AutoWiringDefinition
private(set) var resolveDependenciesBlock: ((DependencyContainer, T) throws -> ())?
var autoWiringFactory: ((DependencyContainer, DependencyContainer.Tag?) throws -> Any)?
var numberOfArguments: Int = 0
public init(scope: ComponentScope, factory: F) {
self.factory = factory
self.scope = scope
//MARK: - TypeForwardingDefinition
/// Types that can be resolved using this definition.
private(set) var implementingTypes: [Any.Type] = [(T?).self]
/// Return `true` if type can be resolved using this definition
func doesImplements(type aType: Any.Type) -> Bool {
return implementingTypes.contains(where: { $0 == aType })
}
private var _resolvedInstance: T?
//Auto-wiring helpers
private(set) var autoWiringFactory: ((DependencyContainer, DependencyContainer.Tag?) throws -> T)?
private(set) var numberOfArguments: Int = 0
//MARK: - _TypeForwardingDefinition
convenience init(scope: ComponentScope, factory: F, autoWiringFactory: (DependencyContainer, DependencyContainer.Tag?) throws -> T, numberOfArguments: Int) {
self.init(scope: scope, factory: factory)
self.autoWiringFactory = autoWiringFactory
self.numberOfArguments = numberOfArguments
/// Adds type as being able to be resolved using this definition
func _implements(type aType: Any.Type) {
_implements(types: [aType])
}
/// Adds types as being able to be resolved using this definition
func _implements(types aTypes: [Any.Type]) {
implementingTypes.append(contentsOf: aTypes.filter({ !doesImplements(type: $0) }))
}
/// Definition to which resolution will be forwarded to
weak var forwardsTo: _TypeForwardingDefinition? {
didSet {
//both definitions (self and forwardsTo) can resolve
//each other types and each other implementing types
//this relationship can be used to reuse previously resolved instances
if let forwardsTo = forwardsTo {
_implements(type: forwardsTo.type)
_implements(types: forwardsTo.implementingTypes)
//definitions for types that can be resolved by `forwardsTo` definition
//can also be used to resolve self type and it's implementing types
//this way container properly reuses previously resolved instances
//when there are several forwarded definitions
//see testThatItReusesInstanceResolvedByTypeForwarding)
for definition in forwardsTo.forwardsFrom {
definition._implements(type: type)
definition._implements(types: implementingTypes)
}
//forwardsTo can be used to resolve self type and it's implementing types
forwardsTo._implements(type: type)
forwardsTo._implements(types: implementingTypes)
forwardsTo.forwardsFrom.append(self)
}
}
}
/// Definitions that will forward resolution to this definition
var forwardsFrom: [_TypeForwardingDefinition] = []
}
///Dummy protocol to store definitions for different types in collection
public protocol Definition: class { }
//MARK: - _Definition
protocol _Definition: Definition {
protocol _Definition: AutoWiringDefinition, TypeForwardingDefinition {
var type: Any.Type { get }
var scope: ComponentScope { get }
var _autoWiringFactory: ((DependencyContainer, DependencyContainer.Tag?) throws -> Any)? { get }
var _factory: Any { get }
var numberOfArguments: Int { get }
func resolveDependenciesOf(resolvedInstance: Any, withContainer container: DependencyContainer) throws
var weakFactory: ((Any) throws -> Any)! { get }
func resolveProperties(of instance: Any, container: DependencyContainer) throws
var container: DependencyContainer? { get set }
var autoInjectProperties: Bool? { get }
}
extension _Definition {
func supportsAutoWiring() -> Bool {
return _autoWiringFactory != nil && numberOfArguments > 0
//MARK: - Type Forwarding
protocol _TypeForwardingDefinition: _Definition {
var forwardsTo: _TypeForwardingDefinition? { get }
var forwardsFrom: [_TypeForwardingDefinition] { get set }
func _implements(type aType: Any.Type)
func _implements(types aTypes: [Any.Type])
}
extension Definition: _TypeForwardingDefinition {
var type: Any.Type {
return T.self
}
}
extension DefinitionOf: _Definition {
var _resolveDependenciesBlock: ((DependencyContainer, Any) throws -> ())? {
return resolveDependenciesBlock.map({ block in { try block($0, $1 as! T) } })
}
var _autoWiringFactory: ((DependencyContainer, DependencyContainer.Tag?) throws -> Any)? {
return autoWiringFactory.map({ factory in { try factory($0.0, $0.1)} })
}
var _factory: Any {
return factory
}
}
extension DefinitionOf: CustomStringConvertible {
extension Definition: CustomStringConvertible {
public var description: String {
return "type: \(T.self), factory: \(F.self), scope: \(scope)"
}
}
//MARK: - Definition Builder
/// Internal class used to build definition
class DefinitionBuilder<T, U> {
typealias F = (U) throws -> T
var scope: ComponentScope!
var factory: F!
var numberOfArguments: Int = 0
var autoWiringFactory: ((DependencyContainer, DependencyContainer.Tag?) throws -> T)?
var forwardsTo: _Definition?
init(configure: (DefinitionBuilder) -> ()) {
configure(self)
}
func build() -> Definition<T, U> {
let factory = self.factory!
let definition = Definition<T, U>(scope: scope, factory: factory)
definition.numberOfArguments = numberOfArguments
definition.autoWiringFactory = autoWiringFactory
definition.weakFactory = { try factory($0 as! U) }
definition.forwardsTo = forwardsTo as? _TypeForwardingDefinition
return definition
}
}
//MARK: - KeyDefinitionPair
typealias KeyDefinitionPair = (key: DefinitionKey, definition: _Definition)
/// Definitions are matched if they are registered for the same tag and their factories accept the same number of runtime arguments.
private func ~=(lhs: KeyDefinitionPair, rhs: KeyDefinitionPair) -> Bool {
guard lhs.key.type == rhs.key.type else { return false }
guard lhs.key.tag == rhs.key.tag else { return false }
guard lhs.definition.numberOfArguments == rhs.definition.numberOfArguments else { return false }
return true
}
/// Returns key-definition pairs with definitions able to resolve that type (directly or via type forwarding)
/// and which tag matches provided key's tag or is nil if strictByTag is false.
/// In the end filters definitions by type of runtime arguments.
func filter(definitions _definitions: [KeyDefinitionPair], byKey key: DefinitionKey, strictByTag: Bool = false, byTypeOfArguments: Bool = false) -> [KeyDefinitionPair] {
let definitions = _definitions
.filter({ $0.key.type == key.type || $0.definition.doesImplements(type: key.type) })
.filter({ $0.key.tag == key.tag || (!strictByTag && $0.key.tag == nil) })
if byTypeOfArguments {
return definitions.filter({ $0.key.typeOfArguments == key.typeOfArguments })
}
else {
return definitions
}
}
/// Orders key-definition pairs putting first definitions registered for provided tag.
func order(definitions _definitions: [KeyDefinitionPair], byTag tag: DependencyContainer.Tag?) -> [KeyDefinitionPair] {
return
_definitions.filter({ $0.key.tag == tag }) +
_definitions.filter({ $0.key.tag != tag })
}
+334 -334
View File
@@ -34,323 +34,307 @@ public final class DependencyContainer {
- seealso: `DependencyTagConvertible`
*/
public enum Tag: Equatable {
public enum Tag {
case String(StringLiteralType)
case Int(IntegerLiteralType)
}
var autoInjectProperties: Bool
var threadSafe: Bool
internal(set) public var context: Context!
var definitions = [DefinitionKey: _Definition]()
var resolvedInstances = ResolvedInstances()
private let lock = RecursiveLock()
var definitions = [DefinitionKey : Definition]()
let resolvedInstances = ResolvedInstances()
let lock = RecursiveLock()
var bootstrapped = false
var bootstrapQueue: [() throws -> ()] = []
private var _weakCollaborators: [WeakBox<DependencyContainer>] = []
var _collaborators: [DependencyContainer] {
get {
return _weakCollaborators.compactMap({ $0.value })
}
set {
_weakCollaborators = newValue.filter({ $0 !== self }).map(WeakBox.init)
}
}
/**
Designated initializer for a DependencyContainer
- parameter configBlock: A configuration block in which you typically put all you `register` calls.
- Parameters:
- autoInjectProperties: Whether container should perform properties auto-injection. Default is `true`.
- threadSafe: Whether container should be thread-safe. Default is `true`. You may want to disable it for better performance.
- configBlock: A configuration block in which you typically put all you `register` calls.
- note: The `configBlock` is simply called at the end of the `init` to let you configure everything.
It is only present for convenience to have a cleaner syntax when declaring and initializing
your `DependencyContainer` instances.
your `DependencyContainer` instances.
- warning: If you use `configBlock` you need to make sure you don't create retain cycles. For example
there will be a retain cycle between container and its definition if you reference container
inside definition's factory. You can avoid that by using unowned reference to container:
```swift
let container = DependencyContainer() { container in
unowned let container = container
//register definitions
}
```
- returns: A new DependencyContainer.
*/
public init(@noescape configBlock: (DependencyContainer->()) = { _ in }) {
public init(autoInjectProperties: Bool = true, threadSafe: Bool = true, configBlock: (DependencyContainer)->() = { _ in }) {
self.autoInjectProperties = autoInjectProperties
self.threadSafe = threadSafe
configBlock(self)
}
private func threadSafe<T>(@noescape closure: () throws -> T) rethrows -> T {
lock.lock()
defer {
lock.unlock()
/**
Call this method to complete container setup. After container is bootstrapped
you can not add or remove definitions. Trying to do so will cause runtime exception.
You can completely reset container, after reset you can bootstrap it again.
During bootstrap container will instantiate components registered with `EagerSingleton` scope.
- throws: `DipError` if failed to instantiate any component
*/
public func bootstrap() throws {
try threadSafe {
bootstrapped = true
try bootstrapQueue.forEach({ try $0() })
bootstrapQueue.removeAll()
}
}
func threadSafe<T>(_ closure: () throws -> T) rethrows -> T {
guard threadSafe else {
return try closure()
}
lock.lock()
defer { lock.unlock() }
return try closure()
}
}
// MARK: - Registering definitions
extension DependencyContainer {
/**
Register factory for type `T` and associate it with an optional tag.
- parameters:
- tag: The arbitrary tag to associate this factory with. Pass `nil` to associate with any tag. Default value is `nil`.
- scope: The scope to use for instance created by the factory.
- factory: The factory to register.
- returns: A registered definition.
- note: You should cast the factory return type to the protocol you want to register it for
(unless you want to register concrete type).
Context provides contextual information about resolution process.
You can use the context for debugging or to pass through tag when you explicitly resolve dependencies.
When auto-wiring or auto-injecting tag will be implicitly passed through by the container.
For auto-injected properties you can disable that by providing tag (some value or `nil`) when defining property.
**Example**:
```swift
container.register { ServiceImp() as Service }
container.register(tag: "service") { ServiceImp() as Service }
container.register(.ObjectGraph) { ServiceImp() as Service }
container.register { ClientImp(service: try! container.resolve() as Service) as Client }
```
*/
public func register<T>(tag tag: Tag? = nil, _ scope: ComponentScope = .Prototype, factory: () throws -> T) -> DefinitionOf<T, () throws -> T> {
let definition = DefinitionOf<T, () throws -> T>(scope: scope, factory: factory)
register(definition, forTag: tag)
return definition
}
/**
Register generic factory associated with an optional tag.
- parameters:
- tag: The arbitrary tag to associate this factory with. Pass `nil` to associate with any tag. Default value is `nil`.
- scope: The scope to use for instance created by the factory.
- factory: The factory to register.
- returns: A registered definition.
- note: You _should not_ call this method directly, instead call any of other `register` methods.
You _should_ use this method only to register dependency with more runtime arguments
than _Dip_ supports (currently it's up to six) like in the following example:
```swift
public func register<T, Arg1, Arg2, Arg3, ...>(tag: Tag? = nil, scope: ComponentScope = .Prototype, factory: (Arg1, Arg2, Arg3, ...) throws -> T) -> DefinitionOf<T, (Arg1, Arg2, Arg3, ...) throws -> T> {
return registerFactory(tag: tag, scope: scope, factory: factory)
class SomeServiceImp: SomeService {
//container will pass through the tag ("tag") used to resolve containing instance to resolve this property
let injected = Injected<SomeDependency>()
//container will use "someTag" tag to resolve this property
let injectedTagged = Injected<SomeDependency>(tag: "someTag")
//container will use `nil` tag to resolve this property
let injectedNilTag = Injected<SomeDependency>(tag: nil)
}
container.register {
//container will pass through the tag ("tag") used to resolve SomeService to resolve $0
SomeServiceImp(dependency: $0) as SomeService
}.resolvingProperties { container, service in
//container will use `nil` tag to resolve this dependency
self.dependency = try container.resolve() as SomeDependency
//container will use current context tag ("tag") to resolve this dependency
self.taggedDependency = try container.resolve(tag: container.context.tag) as SomeDependency
}
//container will use "tag" to resolve this instance
let service = try! container.resolve(tag: "tag") as SomeService
```
Though before you do so you should probably review your design and try to reduce number of depnedencies.
*/
@available(*, deprecated=4.3.0, message="Use registerFactory(tag:scope:factory:numberOfArguments:autoWiringFactory:) instead.")
public func registerFactory<T, F>(tag tag: DependencyTagConvertible? = nil, scope: ComponentScope, factory: F) -> DefinitionOf<T, F> {
let definition = DefinitionOf<T, F>(scope: scope, factory: factory)
register(definition, forTag: tag)
return definition
}
public struct Context: CustomStringConvertible, CustomDebugStringConvertible {
internal(set) public var key: DefinitionKey
/// Currently resolving type.
public var resolvingType: Any.Type {
return key.type
}
/**
Register generic factory and auto-wiring factory and associate it with an optional tag.
- parameters:
- tag: The arbitrary tag to associate this factory with. Pass `nil` to associate with any tag. Default value is `nil`.
- scope: The scope to use for instance created by the factory.
- factory: The factory to register.
- numberOfArguments: The number of factory arguments. Will be used on auto-wiring to sort definitions.
- autoWiringFactory: The factory to be used on auto-wiring to resolve component.
- returns: A registered definition.
- note: You _should not_ call this method directly, instead call any of other `register` methods.
You _should_ use this method only to register dependency with more runtime arguments
than _Dip_ supports (currently it's up to six) like in the following example:
```swift
public func register<T, Arg1, Arg2, Arg3, ...>(tag: Tag? = nil, scope: ComponentScope = .Prototype, factory: (Arg1, Arg2, Arg3, ...) throws -> T) -> DefinitionOf<T, (Arg1, Arg2, Arg3, ...) throws -> T> {
return registerFactory(tag: tag, scope: scope, factory: factory, numberOfArguments: ...) { container, tag in
try factory(try container.resolve(tag: tag), ...)
/// The tag used to resolve currently resolving type.
public var tag: Tag? {
return key.tag
}
/// The type that caused currently resolving type to be resolved.
/// `nil` for root object in a dependencies graph.
private(set) public var injectedInType: Any.Type?
/// The label of the property where resolved instance will be auto-injected.
private(set) public var injectedInProperty: String?
let inCollaboration: Bool
var logErrors: Bool = true
init(key: DefinitionKey, injectedInType: Any.Type?, injectedInProperty: String?, inCollaboration: Bool) {
self.key = key
self.injectedInType = injectedInType
self.injectedInProperty = injectedInProperty
self.inCollaboration = inCollaboration
}
public var debugDescription: String {
return "Context(key: \(key), injectedInType: \(injectedInType.desc), injectedInProperty: \(injectedInProperty.desc) logErrors: \(logErrors))"
}
public var description: String {
let resolvingDescription = "Resolving type \(key.type) with arguments \(key.typeOfArguments) \(key.tag != nil ? "tagged with \(key.tag!)" : "")"
if injectedInProperty != nil {
return "\(resolvingDescription) while auto-injecting property \(injectedInProperty.desc) of \(injectedInType.desc)"
}
else if injectedInType != nil {
return "\(resolvingDescription) while injecting in type \(injectedInType.desc)"
}
else {
return resolvingDescription
}
}
```
Though before you do so you should probably review your design and try to reduce number of depnedencies.
*/
public func registerFactory<T, F>(tag tag: DependencyTagConvertible? = nil, scope: ComponentScope, factory: F, numberOfArguments: Int, autoWiringFactory: (DependencyContainer, Tag?) throws -> T) -> DefinitionOf<T, F> {
let definition = DefinitionOf<T, F>(scope: scope, factory: factory, autoWiringFactory: autoWiringFactory, numberOfArguments: numberOfArguments)
register(definition, forTag: tag)
return definition
}
/**
Register definiton in the container and associate it with an optional tag.
Will override already registered definition for the same type and factory, associated with the same tag.
- parameters:
- tag: The arbitrary tag to associate this definition with. Pass `nil` to associate with any tag. Default value is `nil`.
- definition: The definition to register in the container.
*/
public func register<T, F>(definition: DefinitionOf<T, F>, forTag tag: DependencyTagConvertible? = nil) {
let key = DefinitionKey(protocolType: T.self, factoryType: F.self, associatedTag: tag?.dependencyTag)
register(definition, forKey: key)
}
func register(definition: Definition, forKey key: DefinitionKey) {
threadSafe {
definitions[key] = definition
resolvedInstances.singletons[key] = nil
}
}
}
// MARK: - Resolve dependencies
extension DependencyContainer {
/**
Resolve a an instance of type `T`.
If no matching definition was registered with provided `tag`,
container will lookup definition associated with `nil` tag.
- parameter tag: The arbitrary tag to use to lookup definition.
- throws: `DipError.DefinitionNotFound`, `DipError.AutoInjectionFailed`, `DipError.AmbiguousDefinitions`
- returns: An instance of type `T`.
- seealso: `register(tag:_:factory:)`
**Example**:
```swift
let service = try! container.resolve() as Service
let service = try! container.resolve(tag: "service") as Service
let service: Service = try! container.resolve()
```
*/
public func resolve<T>(tag tag: DependencyTagConvertible? = nil) throws -> T {
return try resolve(tag: tag) { (factory: () throws -> T) in try factory() }
}
/**
Resolve an instance of type `T` using generic builder closure that accepts generic factory and returns created instance.
- parameters:
- tag: The arbitrary tag to use to lookup definition.
- builder: Generic closure that accepts generic factory and returns inctance created by that factory.
- throws: `DipError.DefinitionNotFound`, `DipError.AutoInjectionFailed`, `DipError.AmbiguousDefinitions`
- returns: An instance of type `T`.
- note: You _should not_ call this method directly, instead call any of other
`resolve(tag:)` or `resolve(tag:withArguments:)` methods.
You _should_ use this method only to resolve dependency with more runtime arguments than
_Dip_ supports (currently it's up to six) like in the following example:
```swift
public func resolve<T, Arg1, Arg2, Arg3, ...>(tag tag: Tag? = nil, _ arg1: Arg1, _ arg2: Arg2, _ arg3: Arg3, ...) throws -> T {
return try resolve(tag: tag) { (factory: (Arg1, Arg2, Arg3, ...) -> T) in factory(arg1, arg2, arg3, ...) }
}
```
Though before you do so you should probably review your design and try to reduce the number of dependencies.
*/
public func resolve<T, F>(tag tag: DependencyTagConvertible? = nil, builder: F throws -> T) throws -> T {
let key = DefinitionKey(protocolType: T.self, factoryType: F.self, associatedTag: tag?.dependencyTag)
do {
//first we try to find defintion that exactly matches parameters
return try _resolveKey(key, builder: { definition throws -> T in
guard let factory = definition._factory as? F else {
throw DipError.DefinitionNotFound(key: key)
}
return try builder(factory)
})
}
catch {
switch error {
case let DipError.DefinitionNotFound(errorKey) where key == errorKey:
//then if no definition found we try atuo-wiring
return try threadSafe {
guard let resolved: T = try _resolveByAutoWiring(key) else {
throw error
/// Pushes new context created with provided values and calls block. When block returns previous context is restored.
/// When popped to initial (root) context will release all references to resolved instances and call `Resolvable` callbacks.
func inContext<T>(key aKey: DefinitionKey, injectedInType: Any.Type?, injectedInProperty: String? = nil, inCollaboration: Bool = false, container: DependencyContainer? = nil, logErrors: Bool! = nil, block: () throws -> T) rethrows -> T {
let key = aKey
return try threadSafe {
let currentContext = self.context
defer {
context = currentContext
//clean instances pool if it is owned not by other container
if context == nil {
resolvedInstances.resolvedInstances.removeAll()
for (key, instance) in resolvedInstances.sharedWeakSingletons {
if resolvedInstances.sharedWeakSingletons[key] is WeakBoxType { continue }
resolvedInstances.sharedWeakSingletons[key] = WeakBox(instance)
}
return resolved
for (key, instance) in resolvedInstances.weakSingletons {
if resolvedInstances.weakSingletons[key] is WeakBoxType { continue }
resolvedInstances.weakSingletons[key] = WeakBox(instance)
}
for resolvedInstance in resolvedInstances.resolvableInstances.reversed() {
resolvedInstance.didResolveDependencies()
}
resolvedInstances.resolvableInstances.removeAll()
}
default:
}
context = Context(
key: key,
injectedInType: injectedInType,
injectedInProperty: injectedInProperty,
inCollaboration: inCollaboration
)
context.logErrors = logErrors ?? currentContext?.logErrors ?? true
do {
return try block()
}
catch {
if context.logErrors { log(level: .Errors, error) }
throw error
}
}
}
/// Lookup definition by the key and use it to resolve instance. Fallback to the key with `nil` tag.
func _resolveKey<T>(key: DefinitionKey, builder: _Definition throws -> T) throws -> T {
return try threadSafe {
let nilTagKey = key.associatedTag.map { _ in DefinitionKey(protocolType: T.self, factoryType: key.factoryType, associatedTag: nil) }
}
guard let definition = (self.definitions[key] ?? self.definitions[nilTagKey]) as? _Definition else {
throw DipError.DefinitionNotFound(key: key)
}
return try self._resolveDefinition(definition, usingKey: key, builder: builder)
//MARK: - Collaborating containers
extension DependencyContainer {
/**
Adds collaborating containers as weak references. Circular references are allowed.
References to the container itself are ignored.
*/
public func collaborate(with containers: DependencyContainer...) {
collaborate(with: containers)
}
/**
Adds collaborating containers as weak references. Circular references are allowed.
References to the container itself are ignored.
*/
public func collaborate(with containers: [DependencyContainer]) {
_collaborators += containers
for container in containers {
container._collaborators += [self]
container.resolvedInstances.sharedSingletonsBox = self.resolvedInstances.sharedSingletonsBox
container.resolvedInstances.sharedWeakSingletonsBox = self.resolvedInstances.sharedWeakSingletonsBox
updateCollaborationReferences(between: container, and: self)
}
}
/// Actually resolve dependency.
private func _resolveDefinition<T>(definition: _Definition, usingKey key: DefinitionKey, builder: _Definition throws -> T) rethrows -> T {
return try resolvedInstances.resolve {
if let previouslyResolved: T = resolvedInstances.previouslyResolvedInstance(forKey: key, inScope: definition.scope) {
return previouslyResolved
}
else {
let resolvedInstance = try builder(definition)
//when builder calls factory it will in turn resolve sub-dependencies (if there are any)
//when it returns instance that we try to resolve here can be already resolved
//so we return it, throwing away instance created by previous call to builder
if let previouslyResolved: T = resolvedInstances.previouslyResolvedInstance(forKey: key, inScope: definition.scope) {
return previouslyResolved
}
resolvedInstances.storeResolvedInstance(resolvedInstance, forKey: key, inScope: definition.scope)
try definition.resolveDependenciesOf(resolvedInstance, withContainer: self)
try autoInjectProperties(resolvedInstance)
return resolvedInstance
}
private func updateCollaborationReferences(between container: DependencyContainer, and collaborator: DependencyContainer) {
for container in container._collaborators {
guard container.resolvedInstances.sharedSingletonsBox !== collaborator.resolvedInstances.sharedSingletonsBox else { continue }
container.resolvedInstances.sharedSingletonsBox = collaborator.resolvedInstances.sharedSingletonsBox
container.resolvedInstances.sharedWeakSingletonsBox = collaborator.resolvedInstances.sharedWeakSingletonsBox
updateCollaborationReferences(between: container, and: collaborator)
}
}
///Pool to hold instances, created during call to `resolve()`.
///Before `resolve()` returns pool is drained.
class ResolvedInstances {
var resolvedInstances = [DefinitionKey: Any]()
var singletons = [DefinitionKey: Any]()
var resolvableInstances = [Resolvable]()
func storeResolvedInstance<T>(instance: T, forKey key: DefinitionKey, inScope scope: ComponentScope) {
switch scope {
case .Singleton: singletons[key] = instance
case .ObjectGraph: resolvedInstances[key] = instance
case .Prototype: break
}
/// Tries to resolve key using collaborating containers
func collaboratingResolve<T>(key aKey: DefinitionKey, builder: (_Definition) throws -> T) -> T? {
let key = aKey
for collaborator in _collaborators {
//if container is already in a context resolving this type
//it means that it has been already called to resolve this type,
//so there is probably a circular reference between containers.
//To break it skip this container
if let context = collaborator.context, context.resolvingType == key.type && context.tag == key.tag { continue }
if let resolvable = instance as? Resolvable {
resolvableInstances.append(resolvable)
}
}
func previouslyResolvedInstance<T>(forKey key: DefinitionKey, inScope scope: ComponentScope) -> T? {
switch scope {
case .Singleton: return singletons[key] as? T
case .ObjectGraph: return resolvedInstances[key] as? T
case .Prototype: return nil
}
}
private var depth: Int = 0
func resolve<T>(@noescape block: () throws ->T) rethrows -> T {
depth = depth + 1
defer {
depth = depth - 1
if depth == 0 {
// We call didResolveDependencies only at this point
// because this is a point when dependencies graph is complete.
for resolvedInstance in resolvableInstances {
resolvedInstance.didResolveDependencies()
do {
//Pass current container's instances pool to collect instances resolved by collaborator
let resolvedInstances = collaborator.resolvedInstances
collaborator.resolvedInstances = self.resolvedInstances
//Set collaborator context to preserve current container context
let context = collaborator.context
collaborator.context = self.context
defer {
collaborator.context = context
collaborator.resolvedInstances = resolvedInstances
for (key, resolvedSingleton) in self.resolvedInstances.singletons {
collaborator.resolvedInstances.singletons[key] = resolvedSingleton
}
for (key, resolvedSingleton) in self.resolvedInstances.weakSingletons {
guard collaborator.definition(matching: key) == nil else { continue }
collaborator.resolvedInstances.weakSingletons[key] = resolvedSingleton is WeakBoxType ? resolvedSingleton : WeakBox(resolvedSingleton)
}
for (key, resolved) in self.resolvedInstances.resolvedInstances {
guard collaborator.definition(matching: key) == nil else { continue }
collaborator.resolvedInstances.resolvedInstances[key] = resolved
}
resolvedInstances.removeAll()
resolvableInstances.removeAll()
}
let resolved = try collaborator.inContext(key:key, injectedInType: self.context.injectedInType, injectedInProperty: self.context.injectedInProperty, inCollaboration: true, logErrors: false) {
try collaborator._resolve(key: key, builder: builder)
}
return resolved
}
let resolved = try block()
return resolved
catch { }
}
return nil
}
}
@@ -361,20 +345,30 @@ extension DependencyContainer {
/**
Removes definition registered in the container.
- parameters:
- tag: The tag used to register definition.
- definition: The definition to remove
*/
public func remove<T, F>(definition: DefinitionOf<T, F>, forTag tag: DependencyTagConvertible? = nil) {
let key = DefinitionKey(protocolType: T.self, factoryType: F.self, associatedTag: tag?.dependencyTag)
remove(definitionForKey: key)
public func remove<T, U>(_ definition: Definition<T, U>, tag: DependencyTagConvertible? = nil) {
_remove(definition: definition, tag: tag)
}
func remove(definitionForKey key: DefinitionKey) {
func _remove<T, U>(definition aDefinition: Definition<T, U>, tag: DependencyTagConvertible? = nil) {
let key = DefinitionKey(type: T.self, typeOfArguments: U.self, tag: tag?.dependencyTag)
_remove(definitionForKey: key)
}
func _remove(definitionForKey key: DefinitionKey) {
precondition(!bootstrapped, "You can not modify container's definitions after it was bootstrapped.")
threadSafe {
definitions[key]?.container = nil
definitions[key] = nil
resolvedInstances.singletons[key] = nil
resolvedInstances.weakSingletons[key] = nil
resolvedInstances.sharedSingletons[key] = nil
resolvedInstances.sharedWeakSingletons[key] = nil
}
}
@@ -383,33 +377,83 @@ extension DependencyContainer {
*/
public func reset() {
threadSafe {
definitions.forEach { $0.1.container = nil }
definitions.removeAll()
resolvedInstances.singletons.removeAll()
resolvedInstances.weakSingletons.removeAll()
resolvedInstances.sharedSingletons.removeAll()
resolvedInstances.sharedWeakSingletons.removeAll()
bootstrapped = false
}
}
}
// MARK: - Validation
extension DependencyContainer {
/**
Validates container configuration trying to resolve each registered definition one by one.
If definition fails to be resolved without arguments will search provided arguments array
for arguments matched by type and try to resolve this definition using these arguments.
If there are no matching arguments will rethrow original error.
- parameter arguments: set of arguments to use to resolve registered definitions.
Use a tuple for registered factories that accept several runtime arguments.
*/
public func validate(_ arguments: Any...) throws {
try _validate(arguments: arguments)
}
func _validate(arguments _arguments: [Any]) throws {
let arguments = _arguments
validateNextDefinition: for (key, _) in definitions {
do {
//try to resolve key using provided arguments
for argumentsSet in arguments {
guard type(of: argumentsSet) == key.typeOfArguments else { continue }
do {
let _ = try inContext(key:key, injectedInType: nil) {
try self._resolve(key: key, builder: { definition throws -> Any in
try definition.weakFactory(argumentsSet)
})
}
continue validateNextDefinition
}
catch let error as DipError {
throw error
}
//ignore other errors
catch { log(level: .Errors, error) }
}
//try to resolve key using auto-wiring
do {
let _ = try self.resolve(key.type, tag: key.tag)
}
catch let error as DipError {
throw error
}
//ignore other errors
catch { log(level: .Errors, error) }
}
}
}
}
extension DependencyContainer: CustomStringConvertible {
public var description: String {
return "Definitions: \(definitions.count)\n" + definitions.map({ "\($0.0)" }).joinWithSeparator("\n")
return "Definitions: \(definitions.count)\n" + definitions.map({ "\($0.0)" }).joined(separator: "\n")
}
}
//MARK: - Resolvable
/// Conform to this protocol when you need to have a callback when all the dependencies are injected.
public protocol Resolvable {
/// This method is called by the container when all dependencies of the instance are resolved.
func didResolveDependencies()
}
//MARK: - DependencyTagConvertible
/// Implement this protocol of your type if you want to use its instances as `DependencyContainer`'s tags.
/// `DependencyContainer.Tag`, `String`, `Int` and any `RawRepresentable` with `RawType` of `String` or `Int` by default confrom to this protocol.
/// Implement this protocol on your type if you want to use its instances as `DependencyContainer`'s tags.
/// `DependencyContainer.Tag`, `String`, `Int` and any `RawRepresentable` with `RawType` of `String` or `Int` by default conform to this protocol.
public protocol DependencyTagConvertible {
var dependencyTag: DependencyContainer.Tag { get }
}
@@ -444,7 +488,7 @@ extension DependencyTagConvertible where Self: RawRepresentable, Self.RawValue =
}
}
extension DependencyContainer.Tag: StringLiteralConvertible {
extension DependencyContainer.Tag: ExpressibleByStringLiteral {
public init(stringLiteral value: StringLiteralType) {
self = .String(value)
@@ -460,7 +504,7 @@ extension DependencyContainer.Tag: StringLiteralConvertible {
}
extension DependencyContainer.Tag: IntegerLiteralConvertible {
extension DependencyContainer.Tag: ExpressibleByIntegerLiteral {
public init(integerLiteral value: IntegerLiteralType) {
self = .Int(value)
@@ -468,61 +512,17 @@ extension DependencyContainer.Tag: IntegerLiteralConvertible {
}
public func ==(lhs: DependencyContainer.Tag, rhs: DependencyContainer.Tag) -> Bool {
switch (lhs, rhs) {
case let (.String(lhsString), .String(rhsString)):
return lhsString == rhsString
case let (.Int(lhsInt), .Int(rhsInt)):
return lhsInt == rhsInt
default:
return false
}
}
extension DependencyContainer.Tag: Equatable {
//MARK: - DipError
/**
Errors thrown by `DependencyContainer`'s methods.
- seealso: `resolve(tag:)`
*/
public enum DipError: ErrorType, CustomStringConvertible {
/**
Thrown by `resolve(tag:)` if no matching definition was registered in container.
- parameter key: definition key used to lookup matching definition
*/
case DefinitionNotFound(key: DefinitionKey)
/**
Thrown by `resolve(tag:)` if failed to auto-inject required property.
- parameters:
- label: The name of the property
- type: The type of the property
- underlyingError: The error that caused auto-injection to fail
*/
case AutoInjectionFailed(label: String?, type: Any.Type, underlyingError: ErrorType)
/**
Thrown by `resolve(tag:)` if found ambigous definitions registered for resolved type
- parameters:
- type: The type that failed to be resolved
- definitions: Ambiguous definitions
*/
case AmbiguousDefinitions(type: Any.Type, definitions: [Definition])
public var description: String {
switch self {
case let .DefinitionNotFound(key):
return "No definition registered for \(key).\nCheck the tag, type you try to resolve, number, order and types of runtime arguments passed to `resolve()` and match them with registered factories for type \(key.protocolType)."
case let .AutoInjectionFailed(label, type, error):
return "Failed to auto-inject property \"\(label.desc)\" of type \(type). \(error)"
case let .AmbiguousDefinitions(type, definitions):
return "Ambiguous definitions for \(type):\n" +
definitions.map({ "\($0)" }).joinWithSeparator(";\n")
public static func ==(lhs: DependencyContainer.Tag, rhs: DependencyContainer.Tag) -> Bool {
switch (lhs, rhs) {
case let (.String(lhsString), .String(rhsString)):
return lhsString == rhsString
case let (.Int(lhsInt), .Int(rhsInt)):
return lhsInt == rhsInt
default:
return false
}
}
}
+92
View File
@@ -0,0 +1,92 @@
//
// Dip
//
// Copyright (c) 2015 Olivier Halligon <olivier@halligon.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
/**
Errors thrown by `DependencyContainer`'s methods.
- seealso: `resolve(tag:)`
*/
public enum DipError: Error, CustomStringConvertible {
/**
Thrown by `resolve(tag:)` if no matching definition was registered in container.
- parameter key: definition key used to lookup matching definition
*/
case definitionNotFound(key: DefinitionKey)
/**
Thrown by `resolve(tag:)` if failed to auto-inject required property.
- parameters:
- label: The name of the property
- type: The type of the property
- underlyingError: The error that caused auto-injection to fail
*/
case autoInjectionFailed(label: String?, type: Any.Type, underlyingError: Error)
/**
Thrown by `resolve(tag:)` if failed to auto-wire a type.
- parameters:
- type: The type that failed to be resolved by auto-wiring
- underlyingError: The error that cause auto-wiring to fail
*/
case autoWiringFailed(type: Any.Type, underlyingError: Error)
/**
Thrown when auto-wiring type if several definitions with the same number of runtime arguments
are registered for that type.
- parameters:
- type: The type that failed to be resolved by auto-wiring
- definitions: Ambiguous definitions
*/
case ambiguousDefinitions(type: Any.Type, definitions: [DefinitionType])
/**
Thrown by `resolve(tag:)` if resolved instance does not implement resolved type (i.e. when type-forwarding).
- parameters:
- resolved: Resolved instance
- key: Definition key used to resolve instance
*/
case invalidType(resolved: Any?, key: DefinitionKey)
public var description: String {
switch self {
case let .definitionNotFound(key):
return "No definition registered for \(key).\nCheck the tag, type you try to resolve, number, order and types of runtime arguments passed to `resolve()` and match them with registered factories for type \(String(reflecting: key.type))."
case let .autoInjectionFailed(label, type, error):
return "Failed to auto-inject property \"\(label.desc)\" of type \(String(reflecting: type)). \(error)"
case let .autoWiringFailed(type, error):
return "Failed to auto-wire type \"\(String(reflecting: type))\". \(error)"
case let .ambiguousDefinitions(type, definitions):
return "Ambiguous definitions for \(String(reflecting: type)):\n" +
definitions.map({ "\($0)" }).joined(separator: ";\n")
case let .invalidType(resolved, key):
return "Resolved instance \(resolved ?? "nil") does not implement expected type \(String(reflecting: key.type))."
}
}
}
+84
View File
@@ -0,0 +1,84 @@
//
// Dip
//
// Copyright (c) 2015 Olivier Halligon <olivier@halligon.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
extension DependencyContainer {
/**
Registers definition for passed type.
If instance created by factory of definition, passed as a first parameter,
does not implement type passed in a `type` parameter,
container will throw `DipError.DefinitionNotFound` error when trying to resolve that type.
- parameters:
- definition: Definition to register
- type: Type to register definition for
- tag: Optional tag to associate definition with. Default is `nil`.
- returns: New definition registered for passed type.
*/
@discardableResult public func register<T, U, F>(_ definition: Definition<T, U>, type: F.Type, tag: DependencyTagConvertible? = nil) -> Definition<F, U> {
return _register(definition: definition, type: type, tag: tag)
}
/**
Register definiton in the container and associate it with an optional tag.
Will override already registered definition for the same type and factory, associated with the same tag.
- parameters:
- tag: The arbitrary tag to associate this definition with. Pass `nil` to associate with any tag. Default value is `nil`.
- definition: The definition to register in the container.
*/
public func register<T, U>(_ definition: Definition<T, U>, tag: DependencyTagConvertible? = nil) {
_register(definition: definition, tag: tag)
}
}
extension DependencyContainer {
func _register<T, U>(definition aDefinition: Definition<T, U>, tag: DependencyTagConvertible? = nil) {
precondition(!bootstrapped, "You can not modify container's definitions after it was bootstrapped.")
let definition = aDefinition
threadSafe {
let key = DefinitionKey(type: T.self, typeOfArguments: U.self, tag: tag?.dependencyTag)
if let _ = definitions[key] {
_remove(definitionForKey: key)
}
definition.container = self
definitions[key] = definition
resolvedInstances.singletons[key] = nil
resolvedInstances.weakSingletons[key] = nil
resolvedInstances.sharedSingletons[key] = nil
resolvedInstances.sharedWeakSingletons[key] = nil
if .eagerSingleton == definition.scope {
bootstrapQueue.append({ _ = try self.resolve(tag: tag) as T })
}
}
}
}
+335
View File
@@ -0,0 +1,335 @@
//
// Dip
//
// Copyright (c) 2015 Olivier Halligon <olivier@halligon.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
extension DependencyContainer {
/**
Resolve an instance of type `T`.
If no matching definition was registered with provided `tag`,
container will lookup definition associated with `nil` tag.
- parameter tag: The arbitrary tag to use to lookup definition.
- throws: `DipError.DefinitionNotFound`, `DipError.AutoInjectionFailed`, `DipError.AmbiguousDefinitions`, `DipError.InvalidType`
- returns: An instance of type `T`.
**Example**:
```swift
let service = try! container.resolve() as Service
let service = try! container.resolve(tag: "service") as Service
let service: Service = try! container.resolve()
```
- seealso: `register(_:type:tag:factory:)`
*/
public func resolve<T>(tag: DependencyTagConvertible? = nil) throws -> T {
return try _resolve(tag: tag) { (factory: () throws -> T) in try factory() }
}
/**
Resolve an instance of requested type. Weakly-typed alternative of `resolve(tag:)`
- warning: This method does not make any type checks, so there is no guaranty that
resulting instance is actually an instance of requested type.
That can happen if you register forwarded type that is not implemented by resolved instance.
- parameters:
- type: Type to resolve
- tag: The arbitrary tag to use to lookup definition.
- throws: `DipError.DefinitionNotFound`, `DipError.AutoInjectionFailed`, `DipError.AmbiguousDefinitions`, `DipError.InvalidType`
- returns: An instance of requested type.
**Example**:
```swift
let service = try! container.resolve(Service.self) as! Service
let service = try! container.resolve(Service.self, tag: "service") as! Service
```
- seealso: `resolve(tag:)`, `register(_:type:tag:factory:)`
*/
public func resolve(_ type: Any.Type, tag: DependencyTagConvertible? = nil) throws -> Any {
return try resolve(type, tag: tag) { (factory: () throws -> Any) in try factory() }
}
/**
Resolve an instance of type `T` using generic builder closure that accepts generic factory and returns created instance.
- parameters:
- tag: The arbitrary tag to use to lookup definition.
- builder: Generic closure that accepts generic factory and returns instance created by that factory.
- throws: `DipError.DefinitionNotFound`, `DipError.AutoInjectionFailed`, `DipError.AmbiguousDefinitions`, `DipError.InvalidType`
- returns: An instance of type `T`.
- note: You _should not_ call this method directly, instead call any of other
`resolve(tag:)` or `resolve(tag:withArguments:)` methods.
You _should_ use this method only to resolve dependency with more runtime arguments than
_Dip_ supports (currently it's up to six) like in the following example:
```swift
public func resolve<T, A, B, C, ...>(tag: Tag? = nil, _ arg1: A, _ arg2: B, _ arg3: C, ...) throws -> T {
return try resolve(tag: tag) { factory in factory(arg1, arg2, arg3, ...) }
}
```
Though before you do so you should probably review your design and try to reduce the number of dependencies.
*/
public func resolve<T, U>(tag: DependencyTagConvertible? = nil, builder: ((U) throws -> T) throws -> T) throws -> T {
return try _resolve(tag: tag, builder: builder)
}
public func resolve<T>(tag: DependencyTagConvertible? = nil, builder: (() throws -> T) throws -> T) throws -> T {
return try _resolve(tag: tag, builder: builder)
}
/**
Resolve an instance of provided type using builder closure. Weakly-typed alternative of `resolve(tag:builder:)`
- seealso: `resolve(tag:builder:)`
*/
public func resolve<U>(_ type: Any.Type, tag: DependencyTagConvertible? = nil, builder: ((U) throws -> Any) throws -> Any) throws -> Any {
return try _resolve(type: type, tag: tag, builder: builder)
}
public func resolve(_ type: Any.Type, tag: DependencyTagConvertible? = nil, builder: (() throws -> Any) throws -> Any) throws -> Any {
return try _resolve(type: type, tag: tag, builder: builder)
}
}
extension DependencyContainer {
func _resolve<T>(tag aTag: DependencyTagConvertible? = nil, builder: (() throws -> T) throws -> T) throws -> T {
return try resolve(T.self, tag: aTag, builder: { factory in
try withoutActuallyEscaping(factory, do: { (factory) throws -> T in
try builder({ try factory() as! T })
})
}) as! T
}
func _resolve(type aType: Any.Type, tag: DependencyTagConvertible? = nil, builder: (() throws -> Any) throws -> Any) throws -> Any {
let key = DefinitionKey(type: aType, typeOfArguments: Void.self, tag: tag?.dependencyTag)
return try inContext(key:key, injectedInType: context?.resolvingType) {
try self._resolve(key: key, builder: { definition in
try builder { try definition.weakFactory(()) }
})
}
}
func _resolve<T, U>(tag aTag: DependencyTagConvertible? = nil, builder: ((U) throws -> T) throws -> T) throws -> T {
return try resolve(T.self, tag: aTag, builder: { factory in
try withoutActuallyEscaping(factory, do: { (factory) throws -> T in
try builder({ try factory($0) as! T })
})
}) as! T
}
func _resolve<U>(type aType: Any.Type, tag: DependencyTagConvertible? = nil, builder: ((U) throws -> Any) throws -> Any) throws -> Any {
let key = DefinitionKey(type: aType, typeOfArguments: U.self, tag: tag?.dependencyTag)
return try inContext(key:key, injectedInType: context?.resolvingType) {
try self._resolve(key: key, builder: { definition in
try builder(definition.weakFactory)
})
}
}
/// Lookup definition by the key and use it to resolve instance. Fallback to the key with `nil` tag.
func _resolve<T>(key aKey: DefinitionKey, builder: (_Definition) throws -> T) throws -> T {
guard let matching = self.definition(matching: aKey) else {
do {
return try autowire(key: aKey)
} catch {
if let resolved = collaboratingResolve(key: aKey, builder: builder) {
return resolved
} else {
throw error
}
}
}
let (key, definition) = matching
//first search for already resolved instance for this type or any of forwarding types
if let previouslyResolved: T = previouslyResolved(for: definition, key: key) {
log(level: .Verbose, "Reusing previously resolved instance \(previouslyResolved)")
return previouslyResolved
}
if let context = context { log(level: .Verbose, context) }
var resolvedInstance = try builder(definition)
/*
Strongly-typed `resolve(tag:builder:)` calls weakly-typed `resolve(_:tag:builder:)`,
so `T` will be `Any` at runtime, erasing type information when this method returns.
When we try to cast result of `Any` to generic type T Swift fails to cast it.
The same happens in the following code snippet:
let optService: Service? = ServiceImp()
let anyService: Any = optService
let service: Service = anyService as! Service
That happens because when Optional is casted to Any Swift can not implicitly unwrap it with as operator.
As a workaround we detect boxing here and unwrap it so that we return not a box, but wrapped instance.
*/
if let box = resolvedInstance as? BoxType, let unboxedAny = box.unboxed, let unboxed = unboxedAny as? T {
resolvedInstance = unboxed
}
//when builder calls factory it will in turn resolve sub-dependencies (if there are any)
//when it returns instance that we try to resolve here can be already resolved
//so we return it, throwing away instance created by previous call to builder
if let previouslyResolved: T = previouslyResolved(for: definition, key: key) {
log(level: .Verbose, "Reusing previously resolved instance \(previouslyResolved)")
return previouslyResolved
}
resolvedInstances[key: key, inScope: definition.scope, context: context] = resolvedInstance
if let resolvable = resolvedInstance as? Resolvable {
resolvedInstances.resolvableInstances.append(resolvable)
resolvable.resolveDependencies(self)
}
let shouldAutoInject = definition.autoInjectProperties ?? self.autoInjectProperties
if shouldAutoInject {
try autoInjectProperties(in: resolvedInstance)
}
try definition.resolveProperties(of: resolvedInstance, container: self)
log(level: .Verbose, "Resolved type \(key.type) with \(resolvedInstance)")
return resolvedInstance
}
private func previouslyResolved<T>(for definition: _Definition, key: DefinitionKey) -> T? {
//first check if exact key was already resolved
if let previouslyResolved: T = resolvedInstances[key: key, inScope: definition.scope, context: context] {
return previouslyResolved
}
//then check if any related type was already resolved
let keys = definition.implementingTypes.map({
DefinitionKey(type: $0, typeOfArguments: key.typeOfArguments, tag: key.tag)
})
for key in keys {
if let previouslyResolved: T = resolvedInstances[key: key, inScope: definition.scope, context: context] {
return previouslyResolved
}
}
return nil
}
/// Searches for definition that matches provided key
func definition(matching key: DefinitionKey) -> KeyDefinitionPair? {
if let definition = (self.definitions[key] ?? self.definitions[key.tagged(with: nil)]) {
return (key, definition)
}
//if no definition registered for exact type try to find type-forwarding definition that can resolve the type
//that will actually happen only when resolving optionals
if definitions.filter({ $0.0.type == key.type }).isEmpty {
return typeForwardingDefinition(forKey: key)
}
return nil
}
}
///Pool to hold instances, created during call to `resolve()`.
///Before `resolve()` returns pool is drained.
class ResolvedInstances {
var resolvedInstances = [DefinitionKey: Any]()
var resolvableInstances = [Resolvable]()
//singletons are stored using reference type wrapper to be able to share them between containers
var sharedSingletonsBox = Box<[DefinitionKey: Any]>([:])
var sharedSingletons: [DefinitionKey: Any] {
get { return sharedSingletonsBox.unboxed }
set { sharedSingletonsBox.unboxed = newValue }
}
var singletons = [DefinitionKey: Any]()
var sharedWeakSingletonsBox = Box<[DefinitionKey: Any]>([:])
var sharedWeakSingletons: [DefinitionKey: Any] {
get { return sharedWeakSingletonsBox.unboxed }
set { sharedWeakSingletonsBox.unboxed = newValue }
}
var weakSingletons = [DefinitionKey: Any]()
subscript<T>(key key: DefinitionKey, inScope scope: ComponentScope, context context: DependencyContainer.Context) -> T? {
get {
let instance: Any?
switch scope {
case .singleton, .eagerSingleton:
instance = context.inCollaboration ? sharedSingletons[key] : singletons[key]
case .weakSingleton:
let singletons = context.inCollaboration ? sharedWeakSingletons : weakSingletons
if let boxed = singletons[key] as? WeakBoxType { instance = boxed.unboxed }
else { instance = singletons[key] }
case .shared:
instance = resolvedInstances[key]
case .unique:
return nil
}
return instance.flatMap { $0 as? T }
}
set {
switch scope {
case .singleton, .eagerSingleton:
sharedSingletons[key] = newValue
singletons[key] = newValue
case .weakSingleton:
sharedWeakSingletons[key] = newValue
weakSingletons[key] = newValue
case .shared:
resolvedInstances[key] = newValue
case .unique:
break
}
}
}
}
//MARK: - Resolvable
/// Resolvable protocol provides some extension points for resolving dependencies with property injection.
public protocol Resolvable {
/// This method will be called right after instance is created by the container.
func resolveDependencies(_ container: DependencyContainer)
/// This method will be called when all dependencies of the instance are resolved.
/// When resolving objects graph the last resolved instance will receive this callback first.
func didResolveDependencies()
}
extension Resolvable {
func resolveDependencies(_ container: DependencyContainer) {}
func didResolveDependencies() {}
}
+179 -55
View File
@@ -22,34 +22,128 @@
// THE SOFTWARE.
//
// MARK: - Register/resolve dependencies with runtime arguments
extension DependencyContainer {
/**
Register factory for type `T` and associate it with an optional tag.
- parameters:
- scope: The scope to use for instance created by the factory. Default value is `Shared`.
- type: Type to register definition for. Default value is return value of factory.
- tag: The arbitrary tag to associate this factory with. Pass `nil` to associate with any tag. Default value is `nil`.
- factory: The factory that produces instance of `type`. Will be used to resolve instances of `type`.
- returns: A registered definition.
- note: You should cast the factory return type to the protocol you want to register it for
(unless you want to register concrete type) or provide `type` parameter.
- seealso: `Definition`, `ComponentScope`, `DependencyTagConvertible`
**Example**:
```swift
//Register ServiceImp as Service
container.register { ServiceImp() as Service }
//Register ServiceImp as Service named by "service"
container.register(tag: "service") { ServiceImp() as Service }
//Register unique ServiceImp as Service
container.register(.unique) { ServiceImp() as Service }
//Register ClientImp as Client and resolve it's service dependency
container.register { try ClientImp(service: container.resolve() as Service) as Client }
//Register ServiceImp as concrete type
container.register { ServiceImp() }
container.register(factory: ServiceImp.init)
//Register ServiceImp as Service
container.register(Service.self, factory: ServiceImp.init)
//Register ClientImp as Client
container.register(Client.self, factory: ClientImp.init(service:))
```
*/
@discardableResult public func register<T>(_ scope: ComponentScope = .shared, type: T.Type = T.self, tag: DependencyTagConvertible? = nil, factory: @escaping (()) throws -> T) -> Definition<T, ()> {
let definition = DefinitionBuilder<T, ()> {
$0.scope = scope
$0.factory = factory
}.build()
register(definition, tag: tag)
return definition
}
/**
Register generic factory and auto-wiring factory and associate it with an optional tag.
- parameters:
- tag: The arbitrary tag to associate this factory with. Pass `nil` to associate with any tag. Default value is `nil`.
- scope: The scope to use for instance created by the factory.
- factory: The factory to register.
- numberOfArguments: The number of factory arguments. Will be used on auto-wiring to sort definitions.
- autoWiringFactory: The factory to be used on auto-wiring to resolve component.
- returns: A registered definition.
- note: You _should not_ call this method directly, instead call any of other `register` methods.
You _should_ use this method only to register dependency with more runtime arguments
than _Dip_ supports (currently it's up to six) like in the following example:
```swift
public func register<T, A, B, C, ...>(_ scope: ComponentScope = .shared, type: T.Type = T.self, tag: Tag? = nil, factory: (A, B, C, ...) throws -> T) -> Definition<T, (A, B, C, ...)> {
return register(scope: scope, type: type, tag: tag, factory: factory, numberOfArguments: ...) { container, tag in
try factory(container.resolve(tag: tag), ...)
}
}
```
Though before you do so you should probably review your design and try to reduce number of dependencies.
*/
public func register<T, U>(scope: ComponentScope, type: T.Type, tag: DependencyTagConvertible?, factory: @escaping (U) throws -> T, numberOfArguments: Int, autoWiringFactory: @escaping (DependencyContainer, Tag?) throws -> T) -> Definition<T, U> {
let definition = DefinitionBuilder<T, U> {
$0.scope = scope
$0.factory = factory
$0.numberOfArguments = numberOfArguments
$0.autoWiringFactory = autoWiringFactory
}.build()
register(definition, tag: tag)
return definition
}
// MARK: 1 Runtime Argument
/**
Register factory that accepts one runtime argumentof type `Arg1`. You can use up to six runtime arguments.
Register factory that accepts one runtime argument of type `A`. You can use up to six runtime arguments.
- note: You can have several factories with different number or types of arguments registered for same type,
optionally associated with some tags. When container resolves that type it matches the type,
__number__, __types__ and __order__ of runtime arguments and optional tag that you pass to `resolve(tag:withArguments:)` method.
__number__, __types__ and __order__ of runtime arguments and optional tag that you pass to `resolve(tag:arguments:)` method.
- parameters:
- tag: The arbitrary tag to associate this factory with. Pass `nil` to associate with any tag. Default value is `nil`.
- scope: The scope to use for this component. Default value is `.Prototype`.
- scope: The scope to use for this component. Default value is `Shared`.
- factory: The factory to register.
- seealso: `registerFactory(tag:scope:factory:)`
- seealso: `register(_:type:tag:factory:)`
*/
public func register<T, Arg1>(tag tag: DependencyTagConvertible? = nil, _ scope: ComponentScope = .Prototype, factory: (Arg1) throws -> T) -> DefinitionOf<T, (Arg1) throws -> T> {
return registerFactory(tag: tag, scope: scope, factory: factory, numberOfArguments: 1) { container, tag in try factory(try container.resolve(tag: tag)) }
@discardableResult public func register<T, A>(_ scope: ComponentScope = .shared, type: T.Type = T.self, tag: DependencyTagConvertible? = nil, factory: @escaping ((A)) throws -> T) -> Definition<T, A> {
return register(scope: scope, type: type, tag: tag, factory: factory, numberOfArguments: 1) { container, tag in try factory(container.resolve(tag: tag)) }
}
/**
Resolve a dependency using one runtime argument.
Resolve type `T` using one runtime argument.
- note: When resolving type container will first try to use definition that matches types of arguments that you pass to resolve method. If it fails or no such definition is found container will try to _auto-wire_ component. For that it will iterate through all the definitions registered for that type which factories accept any number of runtime arguments and are tagged with the same tag, passed to `resolve` method, or with no tag. Container will try to use these definitions to resolve a component one by one until one of them succeeds, starting with tagged definitions in order of decreasing their's factories number of arguments.
- note: When resolving a type container will first try to use definition
that exactly matches types of arguments that you pass to resolve method.
If it fails or no such definition is found container will try to _auto-wire_ component.
For that it will iterate through all the definitions registered for that type
which factories accept any number of runtime arguments and are tagged with the same tag,
passed to `resolve` method, or with no tag. Container will try to use these definitions
to resolve a component one by one until one of them succeeds, starting with tagged definitions
in order of decreasing their's factories number of arguments. If none of them succeeds it will
throw an error. If it finds two definitions with the same number of arguments - it will throw
an error.
- parameters:
- tag: The arbitrary tag to lookup registered definition.
@@ -59,71 +153,101 @@ extension DependencyContainer {
- returns: An instance of type `T`.
- seealso: `register(tag:_:factory:)`, `resolve(tag:builder:)`
- seealso: `register(_:type:tag:factory:)`, `resolve(tag:builder:)`
*/
public func resolve<T, Arg1>(tag tag: DependencyTagConvertible? = nil, withArguments arg1: Arg1) throws -> T {
return try resolve(tag: tag) { (factory: (Arg1) throws -> T) in try factory(arg1) }
}
// MARK: 2 Runtime Arguments
/// - seealso: `register(tag:scope:factory:)`
public func register<T, Arg1, Arg2>(tag tag: DependencyTagConvertible? = nil, _ scope: ComponentScope = .Prototype, factory: (Arg1, Arg2) throws -> T) -> DefinitionOf<T, (Arg1, Arg2) throws -> T> {
return registerFactory(tag: tag, scope: scope, factory: factory, numberOfArguments: 2) { container, tag in try factory(try container.resolve(tag: tag), try container.resolve(tag: tag)) }
}
/// - seealso: `resolve(tag:_:)`
public func resolve<T, Arg1, Arg2>(tag tag: DependencyTagConvertible? = nil, withArguments arg1: Arg1, _ arg2: Arg2) throws -> T {
return try resolve(tag: tag) { (factory: (Arg1, Arg2) throws -> T) in try factory(arg1, arg2) }
public func resolve<T, A>(tag: DependencyTagConvertible? = nil, arguments arg1: A) throws -> T {
return try resolve(tag: tag) { factory in try factory(arg1) }
}
///- seealso: `resolve(_:tag:)`, `resolve(tag:arguments:)`
public func resolve<A>(_ type: Any.Type, tag: DependencyTagConvertible? = nil, arguments arg1: A) throws -> Any {
return try resolve(type, tag: tag) { factory in try factory(arg1) }
}
// MARK: 2 Runtime Arguments
/// - seealso: `register(_:type:tag:factory:)`
@discardableResult public func register<T, A, B>(_ scope: ComponentScope = .shared, type: T.Type = T.self, tag: DependencyTagConvertible? = nil, factory: @escaping ((A, B)
) throws -> T) -> Definition<T, (A, B)> {
return register(scope: scope, type: type, tag: tag, factory: factory, numberOfArguments: 2) { container, tag in try factory((container.resolve(tag: tag), container.resolve(tag: tag))) }
}
/// - seealso: `resolve(tag:arguments:)`
public func resolve<T, A, B>(tag: DependencyTagConvertible? = nil, arguments arg1: A, _ arg2: B) throws -> T {
return try resolve(T.self, tag: tag) { factory in try factory((arg1, arg2)) } as! T
}
///- seealso: `resolve(_:tag:)`, `resolve(tag:arguments:)`
public func resolve<A, B>(_ type: Any.Type, tag: DependencyTagConvertible? = nil, arguments arg1: A, _ arg2: B) throws -> Any {
return try resolve(type, tag: tag) { factory in try factory((arg1, arg2)) }
}
// MARK: 3 Runtime Arguments
/// - seealso: `register(tag:scope:factory:)`
public func register<T, Arg1, Arg2, Arg3>(tag tag: DependencyTagConvertible? = nil, _ scope: ComponentScope = .Prototype, factory: (Arg1, Arg2, Arg3) throws -> T) -> DefinitionOf<T, (Arg1, Arg2, Arg3) throws -> T> {
return registerFactory(tag: tag, scope: scope, factory: factory, numberOfArguments: 3) { container, tag in try factory(try container.resolve(tag: tag), try container.resolve(), try container.resolve(tag: tag)) }
/// - seealso: `register(_:type:tag:factory:)`
@discardableResult public func register<T, A, B, C>(_ scope: ComponentScope = .shared, type: T.Type = T.self, tag: DependencyTagConvertible? = nil, factory: @escaping ((A, B, C)) throws -> T) -> Definition<T, (A, B, C)> {
return register(scope: scope, type: type, tag: tag, factory: factory, numberOfArguments: 3) { container, tag in try factory((container.resolve(tag: tag), container.resolve(tag: tag), container.resolve(tag: tag))) }
}
/// - seealso: `resolve(tag:arguments:)`
public func resolve<T, A, B, C>(tag: DependencyTagConvertible? = nil, arguments arg1: A, _ arg2: B, _ arg3: C) throws -> T {
return try resolve(T.self, tag: tag) { factory in try factory((arg1, arg2, arg3)) } as! T
}
/// - seealso: `resolve(tag:withArguments:)`
public func resolve<T, Arg1, Arg2, Arg3>(tag tag: DependencyTagConvertible? = nil, withArguments arg1: Arg1, _ arg2: Arg2, _ arg3: Arg3) throws -> T {
return try resolve(tag: tag) { (factory: (Arg1, Arg2, Arg3) throws -> T) in try factory(arg1, arg2, arg3) }
///- seealso: `resolve(_:tag:)`, `resolve(tag:arguments:)`
public func resolve<A, B, C>(_ type: Any.Type, tag: DependencyTagConvertible? = nil, arguments arg1: A, _ arg2: B, _ arg3: C) throws -> Any {
return try resolve(type, tag: tag) { factory in try factory((arg1, arg2, arg3)) }
}
// MARK: 4 Runtime Arguments
/// - seealso: `register(tag:scope:factory:)`
public func register<T, Arg1, Arg2, Arg3, Arg4>(tag tag: DependencyTagConvertible? = nil, _ scope: ComponentScope = .Prototype, factory: (Arg1, Arg2, Arg3, Arg4) throws -> T) -> DefinitionOf<T, (Arg1, Arg2, Arg3, Arg4) throws -> T> {
return registerFactory(tag: tag, scope: scope, factory: factory, numberOfArguments: 4) { container, tag in try factory(try container.resolve(tag: tag), try container.resolve(tag: tag), try container.resolve(tag: tag), try container.resolve(tag: tag)) }
/// - seealso: `register(_:type:tag:factory:)`
@discardableResult public func register<T, A, B, C, D>(_ scope: ComponentScope = .shared, type: T.Type = T.self, tag: DependencyTagConvertible? = nil, factory: @escaping ((A, B, C, D)) throws -> T) -> Definition<T, (A, B, C, D)> {
return register(scope: scope, type: type, tag: tag, factory: factory, numberOfArguments: 4) { container, tag in try factory((container.resolve(tag: tag), container.resolve(tag: tag), container.resolve(tag: tag), container.resolve(tag: tag))) }
}
/// - seealso: `resolve(tag:withArguments:)`
public func resolve<T, Arg1, Arg2, Arg3, Arg4>(tag tag: DependencyTagConvertible? = nil, withArguments arg1: Arg1, _ arg2: Arg2, _ arg3: Arg3, _ arg4: Arg4) throws -> T {
return try resolve(tag: tag) { (factory: (Arg1, Arg2, Arg3, Arg4) throws -> T) in try factory(arg1, arg2, arg3, arg4) }
/// - seealso: `resolve(tag:arguments:)`
public func resolve<T, A, B, C, D>(tag: DependencyTagConvertible? = nil, arguments arg1: A, _ arg2: B, _ arg3: C, _ arg4: D) throws -> T {
return try resolve(T.self, tag: tag) { factory in try factory((arg1, arg2, arg3, arg4)) } as! T
}
/// - seealso: `resolve(_:tag:)`, `resolve(tag:arguments:)`
public func resolve<A, B, C, D>(_ type: Any.Type, tag: DependencyTagConvertible? = nil, arguments arg1: A, _ arg2: B, _ arg3: C, _ arg4: D) throws -> Any {
return try resolve(type, tag: tag) { factory in try factory((arg1, arg2, arg3, arg4)) }
}
// MARK: 5 Runtime Arguments
/// - seealso: `register(tag:scope:factory:)`
public func register<T, Arg1, Arg2, Arg3, Arg4, Arg5>(tag tag: DependencyTagConvertible? = nil, _ scope: ComponentScope = .Prototype, factory: (Arg1, Arg2, Arg3, Arg4, Arg5) throws -> T) -> DefinitionOf<T, (Arg1, Arg2, Arg3, Arg4, Arg5) throws -> T> {
return registerFactory(tag: tag, scope: scope, factory: factory, numberOfArguments: 5) { container, tag in try factory(try container.resolve(tag: tag), try container.resolve(tag: tag), try container.resolve(tag: tag), try container.resolve(tag: tag), try container.resolve(tag: tag)) }
/// - seealso: `register(_:type:tag:factory:)`
@discardableResult public func register<T, A, B, C, D, E>(_ scope: ComponentScope = .shared, type: T.Type = T.self, tag: DependencyTagConvertible? = nil, factory: @escaping ((A, B, C, D, E)) throws -> T) -> Definition<T, (A, B, C, D, E)> {
return register(scope: scope, type: type, tag: tag, factory: factory, numberOfArguments: 5) { container, tag in try factory((container.resolve(tag: tag), container.resolve(tag: tag), container.resolve(tag: tag), container.resolve(tag: tag), container.resolve(tag: tag))) }
}
/// - seealso: `resolve(tag:withArguments:)`
public func resolve<T, Arg1, Arg2, Arg3, Arg4, Arg5>(tag tag: DependencyTagConvertible? = nil, withArguments arg1: Arg1, _ arg2: Arg2, _ arg3: Arg3, _ arg4: Arg4, _ arg5: Arg5) throws -> T {
return try resolve(tag: tag) { (factory: (Arg1, Arg2, Arg3, Arg4, Arg5) throws -> T) in try factory(arg1, arg2, arg3, arg4, arg5) }
/// - seealso: `resolve(tag:arguments:)`
public func resolve<T, A, B, C, D, E>(tag: DependencyTagConvertible? = nil, arguments arg1: A, _ arg2: B, _ arg3: C, _ arg4: D, _ arg5: E) throws -> T {
return try resolve(T.self, tag: tag) { factory in try factory((arg1, arg2, arg3, arg4, arg5)) } as! T
}
///- seealso: `resolve(_:tag:)`, `resolve(tag:arguments:)`
public func resolve<A, B, C, D, E>(_ type: Any.Type, tag: DependencyTagConvertible? = nil, arguments arg1: A, _ arg2: B, _ arg3: C, _ arg4: D, _ arg5: E) throws -> Any {
return try resolve(type, tag: tag) { factory in try factory((arg1, arg2, arg3, arg4, arg5)) }
}
// MARK: 6 Runtime Arguments
/// - seealso: `register(tag:scope:factory:)`
public func register<T, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6>(tag tag: DependencyTagConvertible? = nil, _ scope: ComponentScope = .Prototype, factory: (Arg1, Arg2, Arg3, Arg4, Arg5, Arg6) throws -> T) -> DefinitionOf<T, (Arg1, Arg2, Arg3, Arg4, Arg5, Arg6) throws -> T> {
return registerFactory(tag: tag, scope: scope, factory: factory, numberOfArguments: 6) { container, tag in try factory(try container.resolve(tag: tag), try container.resolve(tag: tag), try container.resolve(tag: tag), try container.resolve(tag: tag), try container.resolve(tag: tag), try container.resolve(tag: tag)) }
/// - seealso: `register(_:type:tag:factory:)`
@discardableResult public func register<T, A, B, C, D, E, F>(_ scope: ComponentScope = .shared, type: T.Type = T.self, tag: DependencyTagConvertible? = nil, factory: @escaping ((A, B, C, D, E, F)) throws -> T) -> Definition<T, (A, B, C, D, E, F)> {
return register(scope: scope, type: type, tag: tag, factory: factory, numberOfArguments: 6) { container, tag in try factory((container.resolve(tag: tag), container.resolve(tag: tag), container.resolve(tag: tag), container.resolve(tag: tag), container.resolve(tag: tag), container.resolve(tag: tag))) }
}
/// - seealso: `resolve(tag:withArguments:)`
public func resolve<T, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6>(tag tag: DependencyTagConvertible? = nil, withArguments arg1: Arg1, _ arg2: Arg2, _ arg3: Arg3, _ arg4: Arg4, _ arg5: Arg5, _ arg6: Arg6) throws -> T {
return try resolve(tag: tag) { (factory: (Arg1, Arg2, Arg3, Arg4, Arg5, Arg6) throws -> T) in try factory(arg1, arg2, arg3, arg4, arg5, arg6) }
/// - seealso: `resolve(tag:arguments:)`
public func resolve<T, A, B, C, D, E, F>(tag: DependencyTagConvertible? = nil, arguments arg1: A, _ arg2: B, _ arg3: C, _ arg4: D, _ arg5: E, _ arg6: F) throws -> T {
return try resolve(T.self, tag: tag) { factory in try factory((arg1, arg2, arg3, arg4, arg5, arg6)) } as! T
}
/// - seealso: `resolve(_:tag:)`, `resolve(tag:arguments:)`
public func resolve<A, B, C, D, E, F>(_ type: Any.Type, tag: DependencyTagConvertible? = nil, arguments arg1: A, _ arg2: B, _ arg3: C, _ arg4: D, _ arg5: E, _ arg6: F) throws -> Any {
return try resolve(type, tag: tag) { factory in try factory((arg1, arg2, arg3, arg4, arg5, arg6)) }
}
}
+245
View File
@@ -0,0 +1,245 @@
//
// DipUI
//
// Copyright (c) 2016 Ilya Puchka <ilyapuchka@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#if (canImport(UIKit) || canImport(AppKit) || canImport(WatchKit))
extension DependencyContainer {
///Containers that will be used to resolve dependencies of instances, created by stroyboards.
static public var uiContainers: [DependencyContainer] = {
#if os(watchOS)
swizzleAwakeWithContext
#endif
return []
}()
/**
Resolves dependencies of passed in instance.
Use this method to resolve dependencies of object created by storyboard.
The type of the instance should be registered in the container.
You should call this method only from implementation of `didInstantiateFromStoryboard(_:tag:)`
of `StoryboardInstantiatable` protocol if you override its default implementation.
This method will do the same as `resolve(tag:) as T`, but instead of creating
a new intance with a registered factory it will use passed in instance as a resolved instance.
- parameters:
- instance: The object which dependencies should be resolved
- tag: An optional tag used to register the type (`T`) in the container
**Example**:
```swift
class ViewController: UIViewController, ServiceDelegate, StoryboardInstantiatable {
var service: Service?
func didInstantiateFromStoryboard(_ container: DependencyContainer, tag: DependencyContainer.Tag?) throws {
try container.resolveDependencies(of: self as ServiceDelegate, tag: "vc")
}
}
class ServiceImp: Service {
weak var delegate: ServiceDelegate?
}
container.register(tag: "vc") { ViewController() }
.resolvingProperties { container, controller in
controller.service = try container.resolve() as Service
controller.service.delegate = controller
}
container.register { ServiceImp() as Service }
```
- seealso: `register(_:type:tag:factory:)`, `didInstantiateFromStoryboard(_:tag:)`
*/
public func resolveDependencies<T>(of instance: T, tag: Tag? = nil) throws {
_ = try resolve(tag: tag) { (_: () throws -> T) in instance }
}
/**
Register storyboard type `T` which has to conform to `StoryboardInstantiatable` and associate it with an optional tag.
- parameters:
- type: Storyboard type to register definition for.
- tag: The arbitrary tag to associate this factory with. Pass `nil` to associate with any tag. Default value is `nil`.
- returns: A registered definition.
- note: This method will register concrete types. If you need to register
as abstract types you should use standard `register` method from Dip.
You should cast the factory return type to the protocol you want to
register it for (unless you want to register concrete type) or
provide `type` parameter.
- seealso: `Definition`, `ComponentScope`, `DependencyTagConvertible`
**Example**:
```swift
// Register MyViewController
container.register(storyboardType: MyViewController.self)
// or
container.register(tag: "myVC") { MyViewController() as MyViewControllerType }
```
*/
public func register<T: NSObject>(storyboardType type: T.Type, tag: DependencyTagConvertible? = nil) -> Dip.Definition<T, ()> where T: StoryboardInstantiatable {
return register(.unique, type: type, tag: tag, factory: { T() })
}
}
#if os(watchOS)
public protocol StoryboardInstantiatableType {}
#else
public typealias StoryboardInstantiatableType = NSObjectProtocol
#endif
public protocol StoryboardInstantiatable: StoryboardInstantiatableType {
/**
This method will be called if you set a `dipTag` attirbute on the object in a storyboard
that conforms to `StoryboardInstantiatable` protocol.
- parameters:
- tag: The tag value, that was set on the object in a storyboard
- container: The `DependencyContainer` associated with storyboards
The type that implements `StoryboardInstantiatable` protocol should be registered in `UIStoryboard.container`.
Default implementation of that method calls `resolveDependenciesOf(_:tag:)`
and pass it `self` instance and the tag.
Usually you will not need to override the default implementation of this method
if you registered the type of the instance as a concrete type in the container.
Then you only need to add conformance to `StoryboardInstantiatable`.
You may want to override it if you want to add custom logic before/after resolving dependencies
or you want to resolve the instance as implementation of some protocol which it conforms to.
- warning: This method will be called after `init?(coder:)` but before `awakeFromNib` method of `NSObject`.
On watchOS this method will be called before `awakeWithContext(_:)`.
**Example**:
```swift
extension MyViewController: SomeProtocol { ... }
extension MyViewController: StoryboardInstantiatable {
func didInstantiateFromStoryboard(_ container: DependencyContainer, tag: DependencyContainer.Tag) throws {
//resolve dependencies of the instance as SomeProtocol type
try container.resolveDependencies(of: self as SomeProtocol, tag: tag)
//do some additional setup here
}
}
```
*/
func didInstantiateFromStoryboard(_ container: DependencyContainer, tag: DependencyContainer.Tag?) throws
}
extension StoryboardInstantiatable {
public func didInstantiateFromStoryboard(_ container: DependencyContainer, tag: DependencyContainer.Tag?) throws {
try container.resolveDependencies(of: self, tag: tag)
}
}
#if os(iOS) || os(tvOS) || os(OSX)
#if os(iOS) || os(tvOS)
import UIKit
#elseif os(OSX)
import AppKit
#endif
let DipTagAssociatedObjectKey = UnsafeMutablePointer<Int8>.allocate(capacity: 1)
extension NSObject {
///A string tag that will be used to resolve dependencies of this instance
///if it implements `StoryboardInstantiatable` protocol.
@objc private(set) public var dipTag: String? {
get {
return objc_getAssociatedObject(self, DipTagAssociatedObjectKey) as? String
}
set {
objc_setAssociatedObject(self, DipTagAssociatedObjectKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
guard let instantiatable = self as? StoryboardInstantiatable else { return }
let tag = dipTag.map(DependencyContainer.Tag.String)
for (index, container) in DependencyContainer.uiContainers.enumerated() {
do {
log("Trying to resolve \(type(of: self)) with UI container at index \(index)")
try instantiatable.didInstantiateFromStoryboard(container, tag: tag)
log("Resolved \(type(of: self))")
return
} catch { }
}
}
}
}
func log(_ message: Any) {
if Dip.LogLevel.Errors.rawValue <= Dip.logLevel.rawValue {
Dip.logger(logLevel, message)
}
}
#else
import WatchKit
let swizzleAwakeWithContext: Void = {
let originalSelector = #selector(WKInterfaceController.awake(withContext:))
let swizzledSelector = #selector(WKInterfaceController.dip_awake(withContext:))
guard let originalMethod = class_getInstanceMethod(WKInterfaceController.self, originalSelector),
let swizzledMethod = class_getInstanceMethod(WKInterfaceController.self, swizzledSelector) else { return }
let didAddMethod = class_addMethod(WKInterfaceController.self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(WKInterfaceController.self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}()
extension WKInterfaceController: StoryboardInstantiatableType {
@objc func dip_awake(withContext context: AnyObject?) {
defer { self.dip_awake(withContext: context) }
guard let instantiatable = self as? StoryboardInstantiatable else { return }
for container in DependencyContainer.uiContainers {
guard let _ = try? instantiatable.didInstantiateFromStoryboard(container, tag: nil) else { continue }
break
}
}
}
#endif
#endif
+143
View File
@@ -0,0 +1,143 @@
//
// Dip
//
// Copyright (c) 2015 Olivier Halligon <olivier@halligon.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
protocol TypeForwardingDefinition: DefinitionType {
var implementingTypes: [Any.Type] { get }
func doesImplements(type aType: Any.Type) -> Bool
}
extension Definition {
/**
Registers definition for passed type.
If instance created by factory of definition on which method is called
does not implement type passed in a `type` parameter,
container will throw `DipError.DefinitionNotFound` error when trying to resolve that type.
- parameters:
- type: Type to register definition for
- tag: Optional tag to associate definition with. Default is `nil`.
- returns: definition on which `implements` was called
*/
@discardableResult public func implements<F>(_ type: F.Type, tag: DependencyTagConvertible? = nil) -> Definition {
precondition(container != nil, "Definition should be registered in the container.")
container!.register(self, type: type, tag: tag)
return self
}
/**
Registers definition for passed type.
If instance created by factory of definition on which method is called
does not implement type passed in a `type` parameter,
container will throw `DipError.DefinitionNotFound` error when trying to resolve that type.
- parameters:
- type: Type to register definition for
- tag: Optional tag to associate definition with. Default is `nil`.
- resolvingProperties: Optional block to be called to resolve instance property dependencies
- returns: definition on which `implements` was called
*/
@discardableResult public func implements<F>(_ type: F.Type, tag: DependencyTagConvertible? = nil, resolvingProperties: @escaping (DependencyContainer, F) throws -> ()) -> Definition {
precondition(container != nil, "Definition should be registered in the container.")
let forwardDefinition = container!.register(self, type: type, tag: tag)
forwardDefinition.resolvingProperties(resolvingProperties)
return self
}
///Registers definition for types passed as parameters
@discardableResult public func implements<A, B>(_ a: A.Type, _ b: B.Type) -> Definition {
return implements(a).implements(b)
}
///Registers definition for types passed as parameters
@discardableResult public func implements<A, B, C>(_ a: A.Type, _ b: B.Type, _ c: C.Type) -> Definition {
return implements(a).implements(b).implements(c)
}
///Registers definition for types passed as parameters
@discardableResult public func implements<A, B, C, D>(_ a: A.Type, _ b: B.Type, _ c: C.Type, _ d: D.Type) -> Definition {
return implements(a).implements(b).implements(c).implements(d)
}
}
extension DependencyContainer {
func _register<T, U, F>(definition aDefinition: Definition<T, U>, type: F.Type, tag: DependencyTagConvertible? = nil) -> Definition<F, U> {
let definition = aDefinition
precondition(definition.container === self, "Definition should be registered in the container.")
let key = DefinitionKey(type: F.self, typeOfArguments: U.self)
let forwardDefinition = DefinitionBuilder<F, U> {
$0.scope = definition.scope
let factory = definition.factory
$0.factory = { [unowned self] in
let resolved = try factory($0)
if let resolved = resolved as? F {
return resolved
}
else {
throw DipError.invalidType(resolved: resolved, key: key.tagged(with: self.context.tag))
}
}
$0.numberOfArguments = definition.numberOfArguments
$0.autoWiringFactory = definition.autoWiringFactory.map({ factory in
{ [unowned self] in
let resolved = try factory($0, $1)
if let resolved = resolved as? F {
return resolved
}
else {
throw DipError.invalidType(resolved: resolved, key: key.tagged(with: self.context.tag))
}
}
})
$0.forwardsTo = definition
}.build()
register(forwardDefinition, tag: tag)
return forwardDefinition
}
/// Searches for definition that forwards requested type
func typeForwardingDefinition(forKey key: DefinitionKey) -> KeyDefinitionPair? {
var forwardingDefinitions = self.definitions.map({ (key: $0.0, definition: $0.1) })
forwardingDefinitions = filter(definitions: forwardingDefinitions, byKey: key, byTypeOfArguments: true)
forwardingDefinitions = order(definitions: forwardingDefinitions, byTag: key.tag)
//we need to carry on original tag
return forwardingDefinitions.first.map({ ($0.key.tagged(with: key.tag), $0.definition) })
}
}
+59 -2
View File
@@ -22,6 +22,63 @@
// THE SOFTWARE.
//
public enum LogLevel: Int {
case None
case Errors
case Verbose
}
public var logLevel: LogLevel = .Errors
public var logger: (LogLevel, Any) -> Void = { print($1) }
func log(level logLevel: LogLevel, _ message: Any) {
guard logLevel.rawValue <= Dip.logLevel.rawValue else { return }
logger(logLevel, message)
}
///Internal protocol used to unwrap optional values.
protocol BoxType {
var unboxed: Any? { get }
}
extension Optional: BoxType {
var unboxed: Any? {
return self ?? nil
}
}
class Box<T> {
var unboxed: T
init(_ value: T) {
self.unboxed = value
}
}
class NullableBox<T> {
var unboxed: T?
init(_ value: T?) {
self.unboxed = value
}
}
protocol WeakBoxType {
var unboxed: AnyObject? { get }
}
class WeakBox<T>: WeakBoxType {
weak var unboxed: AnyObject?
var value: T? {
return unboxed as? T
}
init(_ value: T?) {
weak var value: AnyObject? = value as AnyObject
self.unboxed = value
}
}
extension Dictionary {
subscript(key: Key?) -> Value? {
get {
@@ -41,7 +98,7 @@ extension Optional {
}
}
#if os(Linux)
#if !_runtime(_ObjC)
import Glibc
class RecursiveLock {
private var _lock = _initializeRecursiveMutex()
@@ -71,7 +128,7 @@ extension Optional {
extension pthread_mutex_t {
mutating func lock() {
pthread_mutex_trylock(&self)
pthread_mutex_lock(&self)
}
mutating func unlock() {
pthread_mutex_unlock(&self)
+505
View File
@@ -0,0 +1,505 @@
//
// Dip
//
// Copyright (c) 2015 Olivier Halligon <olivier@halligon.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import XCTest
@testable import Dip
private protocol Server: AnyObject {
var client: Client! {get}
var anotherClient: Client! {get set}
}
private protocol Client: AnyObject {
var server: Server? {get}
var anotherServer: Server! {get set}
}
#if swift(>=5.1)
private class ServerImp: Server {
@InjectedWeak(didInject: { _ in
AutoInjectionTests.clientDidInjectCalled = true
}) var client: Client!
@InjectedWeak(required: false) var _optionalProperty: AnyObject?
weak var anotherClient: Client!
}
private class ClientImp: Client {
@Injected(didInject: { _ in
AutoInjectionTests.serverDidInjectCalled = true
}) var server: Server
@Injected(required: false) var _optionalProperty: AnyObject?
@Injected(tag: "tagged") var taggedServer: Server
@Injected(tag: nil) var nilTaggedServer: Server
var anotherServer: Server!
}
#else
private class ServerImp: Server {
var _client = InjectedWeak<Client>() { _ in
AutoInjectionTests.clientDidInjectCalled = true
}
var client: Client! {
return _client.value
}
weak var anotherClient: Client!
var _optionalProperty = InjectedWeak<AnyObject>(required: false)
}
private class ClientImp: Client {
var _server = Injected<Server>() { _ in
AutoInjectionTests.serverDidInjectCalled = true
}
var server: Server? {
return _server.value
}
var anotherServer: Server!
var _optionalProperty = Injected<AnyObject>(required: false)
var taggedServer = Injected<Server>(tag: "tagged")
var nilTaggedServer = Injected<Server>(tag: nil)
}
#endif
#if swift(>=5.1)
private class Obj1 {
@InjectedWeak var obj2: Obj2?
@Injected var obj3: Obj3?
}
private class Obj2 {
@Injected var obj1: Obj1?
}
#else
private class Obj1 {
let obj2 = InjectedWeak<Obj2>()
let obj3 = Injected<Obj3>()
}
private class Obj2 {
let obj1 = Injected<Obj1>()
}
#endif
private class Obj3 {
weak var obj1: Obj1?
init(obj: Obj1) {
self.obj1 = obj
}
}
class AutoInjectionTests: XCTestCase {
static var serverDidInjectCalled: Bool = false
static var clientDidInjectCalled: Bool = false
let container = DependencyContainer()
override func setUp() {
container.reset()
}
func testThatItResolvesAutoInjectedDependencies() {
container.register { ServerImp() as Server }
container.register { ClientImp() as Client }
let client = try! container.resolve() as Client
let server = client.server
XCTAssertTrue(client === server?.client)
}
func testThatItResolvesInheritedDependencies() {
class ServerImp2: ServerImp {
#if swift(>=5.1)
@InjectedWeak(didInject: { _ in
XCTAssertTrue(AutoInjectionTests.serverDidInjectCalled, "Inherited properties should be resolved first")
}) var client2: Client?
#else
var _client2 = InjectedWeak<Client>() { _ in
XCTAssertTrue(AutoInjectionTests.serverDidInjectCalled, "Inherited properties should be resolved first")
}
var client2: Client? {
return _client2.value
}
#endif
}
container.register { ServerImp2() as Server }
container.register { ClientImp() as Client }
//when
let client = try! container.resolve() as Client
let server = client.server as? ServerImp2
XCTAssertTrue(client === server?.client)
XCTAssertTrue(client === server?.client2)
}
func testThatItCanSetInjectedProperty() {
container.register { ServerImp() as Server }
container.register { ClientImp() as Client }
let client = (try! container.resolve() as Client) as! ClientImp
let server = client.server as! ServerImp
let newServer = ServerImp()
let newClient = ClientImp()
#if swift(>=5.1)
client.server = newServer
server.client = newClient
#else
client._server = client._server.setValue(newServer)
server._client = server._client.setValue(newClient)
#endif
XCTAssertTrue(client.server === newServer)
XCTAssertTrue(server.client === newClient)
}
func testThatItThrowsErrorIfFailsToAutoInjectDependency() {
container.register { ClientImp() as Client }
XCTAssertThrowsError(try self.container.resolve() as Client)
}
func testThatItResolvesAutoInjectedSingletons() {
//given
container.register(.singleton) { ServerImp() as Server }
container.register(.singleton) { ClientImp() as Client }
//when
let sharedClient = try! container.resolve() as Client
let sharedServer = try! container.resolve() as Server
let client = try! container.resolve() as Client
let server = client.server
//then
XCTAssertTrue(client === sharedClient)
XCTAssertTrue(client === server?.client)
XCTAssertTrue(server === sharedServer)
}
func testThatItCallsResolveDependencyBlockWhenAutoInjecting() {
var serverBlockWasCalled = false
//given
container.register { ServerImp() as Server }
.resolvingProperties { (container, server) -> () in
serverBlockWasCalled = true
}
var clientBlockWasCalled = false
container.register { ClientImp() as Client }
.resolvingProperties { (container, client) -> () in
clientBlockWasCalled = true
}
//when
let _ = try! container.resolve() as Client
XCTAssertTrue(serverBlockWasCalled)
let _ = try! container.resolve() as Server
XCTAssertTrue(clientBlockWasCalled)
}
func testThatItReusesResolvedAutoInjectedInstances() {
//given
container.register { ServerImp() as Server }
.resolvingProperties { (container, server) -> () in
server.anotherClient = try! container.resolve() as Client
}
container.register { ClientImp() as Client }
.resolvingProperties { (container, client) -> () in
client.anotherServer = try! container.resolve() as Server
}
//when
let client = try! container.resolve() as Client
//then
let server = client.server
let anotherServer = client.anotherServer
XCTAssertTrue(server === anotherServer)
let oneClient = server!.client
let anotherClient = server!.anotherClient
XCTAssertTrue(oneClient === anotherClient)
XCTAssertTrue(client === anotherClient)
}
func testThatItReusesAutoInjectedInstancesOnNextResolveOrAutoInjection() {
//given
container.register { Obj1() }
container.register { Obj2() }
container.register { Obj3(obj: try self.container.resolve()) }
//when
let obj2 = try! container.resolve() as Obj2
//then
#if swift(>=5.1)
XCTAssertTrue(obj2 === obj2.obj1!.obj2!,
"Auto-injected instance should be reused on next auto-injection")
XCTAssertTrue(obj2.obj1! === obj2.obj1!.obj3!.obj1,
"Auto-injected instance should be reused on next resolve")
#else
XCTAssertTrue(obj2 === obj2.obj1.value!.obj2.value!,
"Auto-injected instance should be reused on next auto-injection")
XCTAssertTrue(obj2.obj1.value! === obj2.obj1.value!.obj3.value!.obj1,
"Auto-injected instance should be reused on next resolve")
#endif
}
func testThatThereIsNoRetainCycleBetweenAutoInjectedCircularDependencies() {
//given
container.register { ServerImp() as Server }
container.register { ClientImp() as Client }
//when
var client: Client? = try! container.resolve() as Client
//then
weak var weakServer: Server? = client?.server
weak var weakClient = client
XCTAssertNotNil(weakClient)
XCTAssertNotNil(weakServer)
client = nil
XCTAssertNil(weakClient)
XCTAssertNil(weakServer)
}
func testThatItCallsDidInjectOnAutoInjectedProperty() {
AutoInjectionTests.clientDidInjectCalled = false
AutoInjectionTests.serverDidInjectCalled = false
//given
container.register { ServerImp() as Server }
container.register { ClientImp() as Client }
//when
let _ = try! container.resolve() as Client
//then
XCTAssertTrue(AutoInjectionTests.clientDidInjectCalled)
XCTAssertTrue(AutoInjectionTests.serverDidInjectCalled)
}
func testThatNoErrorThrownWhenOptionalPropertiesAreNotAutoInjected() {
//given
container.register { ServerImp() as Server }
container.register { ClientImp() as Client }
XCTAssertNoThrow(
try container.resolve() as Client,
"Container should not throw error if failed to resolve optional auto-injected properties."
)
}
func testThatItResolvesTaggedAutoInjectedProperties() {
//given
container.register { ServerImp() as Server }
container.register(tag: "tagged") { ServerImp() as Server }
container.register { ClientImp() as Client }
//when
let client = try! container.resolve() as Client
//then
#if swift(>=5.1)
let taggedServer = (client as! ClientImp).taggedServer!
#else
let taggedServer = (client as! ClientImp).taggedServer.value!
#endif
let server = client.server!
//server and tagged server should be resolved as different instances
XCTAssertTrue(server !== taggedServer)
XCTAssertNotNil(server)
XCTAssertNotNil(taggedServer)
}
func testThatItPassesTagToAutoInjectedProperty() {
//given
container.register { ServerImp() as Server }
container.register(tag: "tagged") { ServerImp() as Server }
container.register { ClientImp() as Client }
//when
let client = try! container.resolve(tag: "tagged") as Client
//then
#if swift(>=5.1)
let taggedServer = (client as! ClientImp).taggedServer!
#else
let taggedServer = (client as! ClientImp).taggedServer.value!
#endif
let server = client.server!
//server and tagged server should be resolved as the same instance
XCTAssertTrue(server === taggedServer)
}
func testThatItDoesNotPassTagToAutoInjectedPropertyWithExplicitTag() {
//given
container.register { ServerImp() as Server }
container.register(tag: "tagged") { ServerImp() as Server }
container.register { ClientImp() as Client }
.resolvingProperties { (container, client) -> () in
client.anotherServer = try! container.resolve() as Server
}
//when
let client = try! container.resolve(tag: "otherTag") as Client
//then
#if swift(>=5.1)
let taggedServer = (client as! ClientImp).taggedServer!
let nilTaggedServer = (client as! ClientImp).nilTaggedServer!
#else
let taggedServer = (client as! ClientImp).taggedServer.value!
let nilTaggedServer = (client as! ClientImp).nilTaggedServer.value!
#endif
let server = client.server!
//server and tagged server should be resolved as different instances
XCTAssertTrue(server !== taggedServer)
XCTAssertTrue((client.anotherServer as! ServerImp) === nilTaggedServer)
XCTAssertNotNil(server)
XCTAssertNotNil(taggedServer)
XCTAssertNotNil(nilTaggedServer)
}
struct Foo
{
struct Bar
{
}
}
struct Baz
{
struct Bar
{
}
}
func testScopedTypes() {
let key1 = DefinitionKey(type: Baz.Bar.self, typeOfArguments: Void.self)
let key2 = DefinitionKey(type: Foo.Bar.self, typeOfArguments: Void.self)
XCTAssertNotEqual(key1, key2)
XCTAssertNotEqual(key1.hashValue, key2.hashValue)
container.register { Baz.Bar() }
XCTAssertNotNil(try? container.resolve() as Baz.Bar)
XCTAssertThrowsError(try container.resolve() as Foo.Bar)
container.register { Foo.Bar() }
XCTAssertNotNil(try? container.resolve() as Foo.Bar)
}
func testThatItAutoInjectsPropertyWithCollaboratingContainer() {
let collaborator = DependencyContainer()
collaborator.register { ServerImp() as Server }
container.register { ClientImp() as Client }
container.collaborate(with: collaborator)
collaborator.collaborate(with: container)
let client = try! container.resolve() as Client
let server = client.server
XCTAssertTrue(client === server?.client)
}
func testThatItDoesNotAutoInjectIfDisabledInDefinition() {
container.register { ServerImp() as Server }
container.register { ClientImp() as Client }
.autoInjectingProperties(false)
let client = try! container.resolve() as Client
let server = client.server
XCTAssertNil(server)
}
func testThatItDoesNotAutoInjectIfDisabledInContainer() {
let container = DependencyContainer(autoInjectProperties: false)
container.register { ServerImp() as Server }
container.register { ClientImp() as Client }
let client = try! container.resolve() as Client
let server = client.server
XCTAssertNil(server)
}
func testThatItAutoInjectsWhenOverriddenInDefinition() {
let container = DependencyContainer(autoInjectProperties: false)
container.register { ServerImp() as Server }
container.register { ClientImp() as Client }
.autoInjectingProperties(true)
let client = try! container.resolve() as Client
let server = client.server
XCTAssertNotNil(server)
XCTAssertNil(server?.client)
}
}
+463
View File
@@ -0,0 +1,463 @@
//
// Dip
//
// Copyright (c) 2015 Olivier Halligon <olivier@halligon.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import XCTest
@testable import Dip
private protocol Service: AnyObject { }
private class ServiceImp1: Service { }
private class ServiceImp2: Service { }
private class ServiceImp3 {}
private protocol AutoWiredClient: AnyObject {
var service1: Service! { get set }
var service2: Service! { get set }
}
private class AutoWiredClientImp: AutoWiredClient {
var service1: Service!
var service2: Service!
init(service1: Service?, service2: ServiceImp2) {
self.service1 = service1
self.service2 = service2
}
init() {}
}
class AutoWiringTests: XCTestCase {
let container = DependencyContainer()
override func setUp() {
container.reset()
}
func testThatItCanResolveWithAutoWiring() {
//given
container.register { ServiceImp1() as Service }
container.register { ServiceImp2() }
container.register { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
//when
let client = try! container.resolve() as AutoWiredClient
//then
let service1 = client.service1
XCTAssertTrue(service1 is ServiceImp1)
let service2 = client.service2
XCTAssertTrue(service2 is ServiceImp2)
//when
let anyClient = try! container.resolve(AutoWiredClient.self)
//then
XCTAssertTrue(anyClient is AutoWiredClientImp)
}
func testThatItUsesAutoWireFactoryWithMostNumberOfArguments() {
//given
//1 arg
container.register { AutoWiredClientImp(service1: $0, service2: try self.container.resolve()) as AutoWiredClient }
//1 arg
container.register { AutoWiredClientImp(service1: try self.container.resolve(), service2: $0) as AutoWiredClient }
//2 args
var factoryWithMostNumberOfArgumentsCalled = false
container.register { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
.resolvingProperties { _,_ in
factoryWithMostNumberOfArgumentsCalled = true
}
container.register { ServiceImp1() as Service }
container.register { ServiceImp2() }
//when
let _ = try! container.resolve() as AutoWiredClient
//then
XCTAssertTrue(factoryWithMostNumberOfArgumentsCalled)
}
func testThatItThrowsAmbiguityErrorWhenUsingAutoWire() {
//given
//1 arg
container.register { AutoWiredClientImp(service1: $0, service2: try self.container.resolve()) as AutoWiredClient }
//1 arg
container.register { AutoWiredClientImp(service1: try self.container.resolve(), service2: $0) as AutoWiredClient }
container.register { ServiceImp1() as Service }
container.register { ServiceImp2() }
//when
XCTAssertThrowsError(try self.container.resolve() as AutoWiredClient) { (error) in
guard case DipError.autoWiringFailed(_, DipError.ambiguousDefinitions) = error else {
XCTFail("Thrown unexpected error: \(error)")
return
}
}
}
func testThatItPrefersTaggedFactoryWithDifferentNumberOfArgumentsWhenUsingAutoWire() {
//given
//1 arg
container.register { AutoWiredClientImp(service1: $0, service2: try self.container.resolve()) as AutoWiredClient }
//1 arg
container.register { AutoWiredClientImp(service1: try self.container.resolve(), service2: $0) as AutoWiredClient }
//2 args
container.register { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
//1 arg tagged
var taggedFactoryWithMostNumberOfArgumentsCalled = false
container.register(tag: "tag") { AutoWiredClientImp(service1: $0, service2: try self.container.resolve()) as AutoWiredClient }
//2 arg tagged
container.register(tag: "tag") { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }.resolvingProperties { _,_ in
taggedFactoryWithMostNumberOfArgumentsCalled = true
}
container.register() { ServiceImp1() as Service }
container.register { ServiceImp2() }
//when
let _ = try! container.resolve(tag: "tag") as AutoWiredClient
//then
XCTAssertTrue(taggedFactoryWithMostNumberOfArgumentsCalled)
}
func testThatItPrefersTaggedFactoryWithDifferentTypesOfArgumentsWhenUsingAutoWire() {
//given
//1 arg
container.register { AutoWiredClientImp(service1: $0, service2: try self.container.resolve()) as AutoWiredClient }
//2 args
container.register { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
//1 arg tagged
var taggedFactoryCalled = false
container.register(tag: "tag") { AutoWiredClientImp(service1: try self.container.resolve(), service2: $0) as AutoWiredClient }.resolvingProperties { _,_ in
taggedFactoryCalled = true
}
container.register() { ServiceImp1() as Service }
container.register { ServiceImp2() }
//when
let _ = try! container.resolve(tag: "tag") as AutoWiredClient
//then
XCTAssertTrue(taggedFactoryCalled)
}
func testThatItFallbackToNotTaggedFactoryWhenUsingAutoWire() {
//given
//1 arg
var notTaggedFactoryWithMostNumberOfArgumentsCalled = false
container.register { AutoWiredClientImp(service1: $0, service2: try self.container.resolve()) as AutoWiredClient }.resolvingProperties { _,_ in
notTaggedFactoryWithMostNumberOfArgumentsCalled = true
}
//1 arg tagged
container.register(tag: "tag") { AutoWiredClientImp(service1: $0, service2: try self.container.resolve()) as AutoWiredClient }
container.register { ServiceImp1() as Service }
container.register { ServiceImp2() }
//when
let _ = try! container.resolve(tag: "other tag") as AutoWiredClient
//then
XCTAssertTrue(notTaggedFactoryWithMostNumberOfArgumentsCalled)
}
func testThatItDoesNotTryToUseAutoWiringWhenCallingResolveWithArguments() {
//given
container.register { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
container.register { ServiceImp1() as Service }
container.register { ServiceImp2() }
//when
let service = try! container.resolve() as Service
// then
XCTAssertThrowsError(
try self.container.resolve(arguments: service) as AutoWiredClient,
"Container should not use auto-wiring when resolving with runtime arguments"
)
}
func testThatItDoesNotUseAutoWiringWhenFailedToResolveLowLevelDependency() {
//given
container.register { AutoWiredClientImp() as AutoWiredClient }
.resolvingProperties { container, resolved in
resolved.service1 = try container.resolve() as Service
resolved.service2 = try container.resolve() as ServiceImp2
//simulate that something goes wrong on the way
throw DipError.definitionNotFound(key: DefinitionKey(type: ServiceImp1.self, typeOfArguments: Any.self))
}
container.register { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
.resolvingProperties { container, resolved in
//auto-wiring should be performed only when definition for type to resolve is not found
//but not for any other type along the way in the graph
XCTFail("Auto-wiring should not be performed if instance was actually resolved.")
}
container.register { ServiceImp1() as Service }
container.register { ServiceImp2() }
//then
XCTAssertThrowsError(
try self.container.resolve() as AutoWiredClient,
"Container should not use auto-wiring when definition for resolved type is registered."
)
}
func testThatItReusesInstancesResolvedWithAutoWiringWhenUsingAutoWiringAgain() {
//given
container.register { ServiceImp1() as Service }
container.register { ServiceImp2() }
var anotherInstance: AutoWiredClient?
container.register { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
.resolvingProperties { container, _ in
if anotherInstance == nil {
anotherInstance = try! container.resolve() as AutoWiredClient
}
}
//when
let resolved = try! container.resolve() as AutoWiredClient
//then
XCTAssertTrue(
(resolved as! AutoWiredClientImp) === (anotherInstance as! AutoWiredClientImp),
"when doing another auto-wiring during resolve we should reuse instance"
)
}
func testThatItReusesInstancesResolvedWithoutAutoWiringWhenUsingAutoWiringAgain() {
//given
container.register { ServiceImp1() as Service }
container.register { ServiceImp2() }
var anotherInstance: AutoWiredClient?
container.register { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
.resolvingProperties { container, _ in
if anotherInstance == nil {
anotherInstance = try! container.resolve() as AutoWiredClient
}
}
//when
let service1 = try! container.resolve() as Service?
let service2 = try! container.resolve() as ServiceImp2
let resolved = try! container.resolve(arguments: service1, service2) as AutoWiredClient
//then
XCTAssertTrue(
(resolved as! AutoWiredClientImp) === (anotherInstance as! AutoWiredClientImp),
"when doing another auto-wiring during resolve we should reuse instance"
)
}
func testThatItReusesInstancesResolvedWithAutoWiringWhenUsingAutoWiringAgainWithTheSameTag() {
//given
container.register { ServiceImp1() as Service }
container.register { ServiceImp2() }
var anotherInstance: AutoWiredClient?
container.register(tag: "tag") { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
.resolvingProperties { container, _ in
if anotherInstance == nil {
anotherInstance = try! container.resolve(tag: "tag") as AutoWiredClient
}
}
//when
let resolved = try! container.resolve(tag: "tag") as AutoWiredClient
//then
XCTAssertTrue(
(resolved as! AutoWiredClientImp) === (anotherInstance as! AutoWiredClientImp),
"when doing another auto-wiring during resolve we should reuse instance"
)
}
func testThatItDoesNotReuseInstancesResolvedWithAutoWiringWhenUsingAutoWiringAgainWithAnotherTag() {
//given
container.register { ServiceImp1() as Service }
container.register { ServiceImp2() }
var anotherInstance: AutoWiredClient?
container.register { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
.resolvingProperties { container, _ in
if anotherInstance == nil {
anotherInstance = try! container.resolve() as AutoWiredClient
}
}
//when
let resolved = try! container.resolve(tag: "tag") as AutoWiredClient
//then
XCTAssertTrue(
(resolved as! AutoWiredClientImp) !== (anotherInstance as! AutoWiredClientImp),
"when doing another auto-wiring during resolve we should reuse instance"
)
}
func testThatItUsesTagToResolveDependenciesWithAutoWiringWith1Argument() {
//given
container.register { ServiceImp1() as Service }
container.register(tag: "tag") { ServiceImp2() as Service }
container.register { (dep1: Service) -> ServiceImp3 in
XCTAssertTrue(dep1 is ServiceImp2)
return ServiceImp3()
}
//when
let _ = try! container.resolve(tag: "tag") as ServiceImp3
}
func testThatItUsesTagToResolveDependenciesWithAutoWiringWith2Arguments() {
//given
container.register { ServiceImp1() as Service }
container.register(tag: "tag") { ServiceImp2() as Service }
container.register { (dep1: Service, dep2: Service) -> ServiceImp3 in
XCTAssertTrue(dep1 is ServiceImp2)
XCTAssertTrue(dep2 is ServiceImp2)
return ServiceImp3()
}
//when
let _ = try! container.resolve(tag: "tag") as ServiceImp3
}
func testThatItUsesTagToResolveDependenciesWithAutoWiringWith3Arguments() {
//given
container.register { ServiceImp1() as Service }
container.register(tag: "tag") { ServiceImp2() as Service }
container.register { (dep1: Service, dep2: Service, dep3: Service) -> ServiceImp3 in
XCTAssertTrue(dep1 is ServiceImp2)
XCTAssertTrue(dep2 is ServiceImp2)
XCTAssertTrue(dep3 is ServiceImp2)
return ServiceImp3()
}
//when
let _ = try! container.resolve(tag: "tag") as ServiceImp3
}
func testThatItUsesTagToResolveDependenciesWithAutoWiringWith4Arguments() {
//given
container.register { ServiceImp1() as Service }
container.register(tag: "tag") { ServiceImp2() as Service }
container.register { (dep1: Service, dep2: Service, dep3: Service, dep4: Service) -> ServiceImp3 in
XCTAssertTrue(dep1 is ServiceImp2)
XCTAssertTrue(dep2 is ServiceImp2)
XCTAssertTrue(dep3 is ServiceImp2)
XCTAssertTrue(dep4 is ServiceImp2)
return ServiceImp3()
}
//when
let _ = try! container.resolve(tag: "tag") as ServiceImp3
}
func testThatItUsesTagToResolveDependenciesWithAutoWiringWith5Arguments() {
//given
container.register { ServiceImp1() as Service }
container.register(tag: "tag") { ServiceImp2() as Service }
container.register { (dep1: Service, dep2: Service, dep3: Service, dep4: Service, dep5: Service) -> ServiceImp3 in
XCTAssertTrue(dep1 is ServiceImp2)
XCTAssertTrue(dep2 is ServiceImp2)
XCTAssertTrue(dep3 is ServiceImp2)
XCTAssertTrue(dep4 is ServiceImp2)
XCTAssertTrue(dep5 is ServiceImp2)
return ServiceImp3()
}
//when
let _ = try! container.resolve(tag: "tag") as ServiceImp3
}
func testThatItUsesTagToResolveDependenciesWithAutoWiringWith6Arguments() {
//given
container.register { ServiceImp1() as Service }
container.register(tag: "tag") { ServiceImp2() as Service }
container.register { (dep1: Service, dep2: Service, dep3: Service, dep4: Service, dep5: Service, dep6: Service) -> ServiceImp3 in
XCTAssertTrue(dep1 is ServiceImp2)
XCTAssertTrue(dep2 is ServiceImp2)
XCTAssertTrue(dep3 is ServiceImp2)
XCTAssertTrue(dep4 is ServiceImp2)
XCTAssertTrue(dep5 is ServiceImp2)
XCTAssertTrue(dep6 is ServiceImp2)
return ServiceImp3()
}
//when
let _ = try! container.resolve(tag: "tag") as ServiceImp3
}
func testThatItCanAutoWireOptional() {
//given
container.register { ServiceImp1() as Service }
container.register { ServiceImp2() }
container.register { AutoWiredClientImp(service1: $0, service2: $1) as AutoWiredClient }
var resolved: AutoWiredClient?
//when
XCTAssertNoThrow(resolved = try self.container.resolve() as AutoWiredClient?)
XCTAssertNotNil(resolved)
//when
XCTAssertNoThrow(resolved = try self.container.resolve(tag: "tag") as AutoWiredClient?)
XCTAssertNotNil(resolved)
}
}
+353
View File
@@ -0,0 +1,353 @@
//
// Dip
//
// Copyright (c) 2015 Olivier Halligon <olivier@halligon.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import XCTest
@testable import Dip
private protocol Service: AnyObject {}
private class ServiceImp1: Service {}
private class ServiceImp2: Service {}
private class Server {
weak var client: Client?
init() {}
}
private class Client {
var server: Server
init(server: Server) {
self.server = server
}
}
class ComponentScopeTests: XCTestCase {
let container = DependencyContainer()
override func setUp() {
container.reset()
}
func testThatSharedIsDefaultScope() {
let def = container.register { ServiceImp1() as Service }
XCTAssertEqual(def.scope, ComponentScope.shared)
}
func testThatScopeCanBeChanged() {
let def = container.register(.singleton) { ServiceImp1() as Service }
XCTAssertEqual(def.scope, ComponentScope.singleton)
}
func testThatItResolvesTypeAsNewInstanceForUniqueScope() {
//given
container.register { ServiceImp1() as Service }
//when
let service1 = try! container.resolve() as Service
let service2 = try! container.resolve() as Service
//then
XCTAssertFalse(service1 === service2)
}
func testThatItReusesInstanceForSingletonScope() {
func test(_ scope: ComponentScope, line: UInt = #line) {
//given
container.register(scope) { ServiceImp1() as Service }
//when
let service1 = try! container.resolve() as Service
let service2 = try! container.resolve() as Service
//then
XCTAssertTrue(service1 === service2, line: line)
}
test(.singleton)
test(.weakSingleton)
test(.eagerSingleton)
}
func testThatSingletonIsNotReusedAcrossContainers() {
func test(_ scope: ComponentScope, line: UInt = #line) {
//given
let def = container.register(scope) { ServiceImp1() as Service }
let secondContainer = DependencyContainer()
secondContainer.register(def, tag: nil)
//when
let service1 = try! container.resolve() as Service
let service2 = try! secondContainer.resolve() as Service
//then
XCTAssertTrue(service1 !== service2, "Singleton instances should not be reused across containers", line: line)
}
test(.singleton)
test(.weakSingleton)
test(.eagerSingleton)
}
func testThatSingletonIsReleasedWhenDefinitionIsRemoved() {
func test(_ scope: ComponentScope, line: UInt = #line) {
//given
let def = container.register(scope) { ServiceImp1() as Service }
let service1 = try! container.resolve() as Service
//when
container.remove(def, tag: nil)
container.register(def, tag: nil)
//then
let service2 = try! container.resolve() as Service
XCTAssertTrue(service1 !== service2, "Singleton instances should be released when definition is removed from the container", line: line)
}
test(.singleton)
test(.weakSingleton)
test(.eagerSingleton)
}
func testThatSingletonIsReleasedWhenDefinitionIsOverridden() {
func test(_ scope: ComponentScope, line: UInt = #line) {
//given
let def = container.register(scope) { ServiceImp1() as Service }
let service1 = try! container.resolve() as Service
//when
container.register(def, tag: nil)
//then
let service2 = try! container.resolve() as Service
XCTAssertTrue(service1 !== service2, "Singleton instances should be released when definition is overridden", line: line)
}
test(.singleton)
test(.weakSingleton)
test(.eagerSingleton)
}
func testThatSingletonIsReleasedWhenContainerIsReset() {
func test(_ scope: ComponentScope, line: UInt = #line) {
//given
let def = container.register(scope) { ServiceImp1() as Service }
let service1 = try! container.resolve() as Service
//when
container.reset()
container.register(def, tag: nil)
//then
let service2 = try! container.resolve() as Service
XCTAssertTrue(service1 !== service2, "Singleton instances should be released when container is reset", line: line)
}
test(.singleton)
test(.weakSingleton)
test(.eagerSingleton)
}
func testThatItReusesInstanceInSharedScopeDuringResolve() {
//given
container.register { Client(server: try self.container.resolve()) as Client }
container.register { Server() as Server }
.resolvingProperties { container, server in
server.client = try container.resolve() as Client
}
//when
let client = try! container.resolve() as Client
//then
let server = client.server
XCTAssertTrue(server.client === client)
}
func testThatItDoesNotReuseInstanceInSharedScopeInNextResolve() {
//given
container.register { Client(server: try self.container.resolve()) as Client }
container.register { Server() as Server }
.resolvingProperties { container, server in
server.client = try container.resolve() as Client
}
//when
let client = try! container.resolve() as Client
let server = client.server
let anotherClient = try! container.resolve() as Client
let anotherServer = anotherClient.server
//then
XCTAssertFalse(server === anotherServer)
XCTAssertFalse(client === anotherClient)
}
func testThatItDoesNotReuseInstanceInSharedScopeResolvedForNilTagWhenResolvingForAnotherTag() {
//given
var service2: Service?
container.register { ServiceImp1() as Service }
.resolvingProperties { (c, _) in
//when service1 is resolved using this definition due to fallback to nil tag
service2 = try c.resolve(tag: "service") as Service
//then we don't want every next resolve of service for other tags to reuse it
XCTAssertTrue(service2 is ServiceImp2)
}
container.register(tag: "service") { ServiceImp2() as Service}
//when
let service1 = try! container.resolve(tag: "tag") as Service
//then
XCTAssertTrue(service1 is ServiceImp1)
}
func testThatItReusesInstanceInSharedScopeResolvedForNilTag() {
//given
var service2: Service?
container.register { ServiceImp1() as Service }
.resolvingProperties { (c, service1) in
guard service2 == nil else { return }
//when service1 is resolved using this definition due to fallback to nil tag
//and service is resolved again with another (existing) tag
service2 = try c.resolve(tag: "tag") as Service
//than we don't want every next resolve of service to reuse it
XCTAssertTrue(service1 as! ServiceImp1 === service1)
}
//when
let service1 = try! container.resolve(tag: "tag") as Service
//then
XCTAssertTrue(service1 is ServiceImp1)
}
func testThatOnlyEagerSingletonIsCreatedWhenContainerIsBootsrapped() {
//given
var eagerSingletonResolved = false
container.register(.eagerSingleton, tag: "eager") { ServiceImp1() as Service }
.resolvingProperties { container, service in eagerSingletonResolved = true }
container.register(.singleton, tag: "singleton") { ServiceImp1() as Service }
.resolvingProperties { container, service in XCTFail() }
container.register(.unique, tag: "prototype") { ServiceImp1() as Service }
.resolvingProperties { container, service in XCTFail() }
container.register(.shared, tag: "graph") { ServiceImp1() as Service }
.resolvingProperties { container, service in XCTFail() }
//when
try! container.bootstrap()
XCTAssertTrue(eagerSingletonResolved)
}
func testThatContainerCanBeBootstrappedAgainAfterReset() {
try! container.bootstrap()
XCTAssertTrue(container.bootstrapped)
container.reset()
XCTAssertFalse(container.bootstrapped)
}
func testThatItReusesResolvedInstanceWhenResolvingOptional() {
var otherService: Service!
var anyOtherService: Any!
container.register { ServiceImp1() as Service }
.resolvingProperties { container, service in
otherService = try! container.resolve() as Service?
anyOtherService = try! container.resolve((Service?).self)
}
let service = try! container.resolve() as Service
XCTAssertTrue(otherService as! ServiceImp1 === service as! ServiceImp1)
XCTAssertTrue(anyOtherService as! ServiceImp1 === service as! ServiceImp1)
}
func testThatItHoldsWeakReferenceToWeakSingletonInstance() {
//given
container.register(.weakSingleton) { ServiceImp1() as Service }
var strongSingleton: Service? = try! container.resolve() as Service
weak var weakSingleton = try! container.resolve() as Service
//then
XCTAssertTrue(weakSingleton === strongSingleton)
//when
strongSingleton = nil
//then
XCTAssertNil(weakSingleton)
}
func testThatItResolvesWeakSingletonAgainAfterItWasReleased() {
Dip.logLevel = .Verbose
//given
let service = container.register(.weakSingleton) { ServiceImp1() }
container.register(service, type: Service.self)
//when
//resolve and release right away
_ = try? container.resolve() as ServiceImp1
//then
XCTAssertNoThrow(
try container.resolve() as Service,
"Weak singleton should be resolved again."
)
}
func testThatCollaboratingContainersReuseSingletonsResolvedByAnotherContainer() {
func test(_ scope: ComponentScope, line: UInt = #line) {
let container1 = DependencyContainer()
container1.register(scope) { ServiceImp1() as Service }
let container2 = DependencyContainer()
container1.collaborate(with: container2)
container2.collaborate(with: container1)
try! container1.bootstrap()
try! container2.bootstrap()
let service1 = try! container1.resolve() as Service
let service2 = try! container2.resolve() as Service
XCTAssertTrue(service1 === service2, "\(scope) should be reused when first resolved from another container", line: line)
}
test(.singleton)
test(.weakSingleton)
test(.eagerSingleton)
}
}
+247
View File
@@ -0,0 +1,247 @@
//
// Dip
//
// Copyright (c) 2015 Olivier Halligon <olivier@halligon.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import XCTest
@testable import Dip
private protocol Service {}
private class ServiceImp1: Service {
let injected = Injected<ServiceImp2>()
let injectedWeak = InjectedWeak<ServiceImp2>()
let taggedInjected = Injected<ServiceImp2>(tag: "injectedTag")
let taggedInjectedWeak = InjectedWeak<ServiceImp2>(tag: "injectedTag")
let injectedNilTag = Injected<ServiceImp2>(tag: nil)
}
private class ServiceImp2: Service {}
class ContextTests: XCTestCase {
let container = DependencyContainer()
override func setUp() {
container.reset()
container.register { ServiceImp2() }
}
func testThatContextStoresCurrentlyResolvedType() {
container.register { () -> Service in
XCTAssertTrue(self.container.context.resolvingType == Service.self)
let _ = try self.container.resolve() as ServiceImp1
return ServiceImp1() as Service
}.resolvingProperties { _,_ in
XCTAssertTrue(self.container.context.resolvingType == Service.self)
let _ = try self.container.resolve() as ServiceImp1
}
container.register { () -> ServiceImp1 in
XCTAssertTrue(self.container.context.resolvingType == ServiceImp1.self)
return ServiceImp1()
}.resolvingProperties { _,_ in
XCTAssertTrue(self.container.context.resolvingType == ServiceImp1.self)
}
let _ = try! container.resolve() as Service
}
func testThatContextStoresInjectedInType() {
container.register { () -> Service in
XCTAssertNil(self.container.context.injectedInType)
let _ = try self.container.resolve() as ServiceImp1
return ServiceImp1() as Service
}.resolvingProperties { _,_ in
XCTAssertNil(self.container.context.injectedInType)
let _ = try self.container.resolve() as ServiceImp1
}
container.register { () -> ServiceImp1 in
XCTAssertTrue(self.container.context.injectedInType == Service.self)
return ServiceImp1()
}.resolvingProperties { _,_ in
XCTAssertTrue(self.container.context.injectedInType == Service.self)
}
let _ = try! container.resolve() as Service
}
func testThatContextStoresTheTagPassedToResolve() {
container.register { () -> Service in
XCTAssertNotNil(self.container.context.tag)
XCTAssertTrue(DependencyContainer.Tag.String("tag") ~= self.container.context.tag!)
let _ = try self.container.resolve(tag: "otherTag") as ServiceImp1
return ServiceImp1() as Service
}.resolvingProperties { _,_ in
XCTAssertNotNil(self.container.context.tag)
XCTAssertTrue(DependencyContainer.Tag.String("tag") ~= self.container.context.tag!)
let _ = try self.container.resolve(tag: "otherTag") as ServiceImp1
}
container.register { () -> ServiceImp1 in
XCTAssertNotNil(self.container.context.tag)
XCTAssertTrue(DependencyContainer.Tag.String("otherTag") ~= self.container.context.tag!)
return ServiceImp1()
}.resolvingProperties { _,_ in
XCTAssertNotNil(self.container.context.tag)
XCTAssertTrue(DependencyContainer.Tag.String("otherTag") ~= self.container.context.tag!)
}
let _ = try! container.resolve(tag: "tag") as Service
}
func testThatContextStoresTheTagPassedToResolveWhenAutoInjecting() {
container.register { ServiceImp1() as Service }
container.register { ServiceImp1() }
container.register { () -> ServiceImp2 in
if self.container.context.injectedInProperty == "injectedNilTag" {
XCTAssertNil(self.container.context.tag)
}
else {
XCTAssertNotNil(self.container.context.tag)
XCTAssertTrue(DependencyContainer.Tag.String("injectedTag") ~= self.container.context.tag!)
}
return ServiceImp2()
}.resolvingProperties { _,_ in
if self.container.context.injectedInProperty == "injectedNilTag" {
XCTAssertNil(self.container.context.tag)
}
else {
XCTAssertNotNil(self.container.context.tag)
XCTAssertTrue(DependencyContainer.Tag.String("injectedTag") ~= self.container.context.tag!)
}
}
container.register(tag: "tag") { () -> ServiceImp2 in
XCTAssertNotNil(self.container.context.tag)
XCTAssertTrue(DependencyContainer.Tag.String("tag") ~= self.container.context.tag!)
return ServiceImp2()
}.resolvingProperties { _,_ in
XCTAssertNotNil(self.container.context.tag)
XCTAssertTrue(DependencyContainer.Tag.String("tag") ~= self.container.context.tag!)
}
let _ = try! container.resolve(tag: "tag") as Service
}
func testThatContextStoresTheTagPassedToResolveWhenAutoWiring() {
container.register { (_: ServiceImp1) -> Service in
return ServiceImp1() as Service
}.resolvingProperties { _,_ in
}
container.register { () -> ServiceImp1 in
XCTAssertNotNil(self.container.context.tag)
XCTAssertTrue(DependencyContainer.Tag.String("tag") ~= self.container.context.tag!)
return ServiceImp1()
}.resolvingProperties { _,_ in
XCTAssertNotNil(self.container.context.tag)
XCTAssertTrue(DependencyContainer.Tag.String("tag") ~= self.container.context.tag!)
}
let _ = try! container.resolve(tag: "tag") as Service
}
func testThatContextDoesNotOverrideNilTagPassedToResolve() {
container.register { () -> Service in
XCTAssertNotNil(self.container.context.tag)
XCTAssertTrue(DependencyContainer.Tag.String("tag") ~= self.container.context.tag!)
let _ = try self.container.resolve() as ServiceImp1
return ServiceImp1() as Service
}.resolvingProperties { _,_ in
XCTAssertNotNil(self.container.context.tag)
XCTAssertTrue(DependencyContainer.Tag.String("tag") ~= self.container.context.tag!)
let _ = try self.container.resolve() as ServiceImp1
}
container.register { () -> ServiceImp1 in
XCTAssertNil(self.container.context.tag)
return ServiceImp1()
}.resolvingProperties { _,_ in
XCTAssertNil(self.container.context.tag)
}
let _ = try! container.resolve(tag: "tag") as Service
}
func testThatContextStoresNameOfAutoInjectedProperty() {
container.register { ServiceImp1() as Service }
container.register { ServiceImp1() }
let names = ["injected", "injectedWeak", "taggedInjected", "taggedInjectedWeak", "injectedNilTag"]
container.register { () -> ServiceImp2 in
XCTAssertNotNil(self.container.context.injectedInProperty)
XCTAssertTrue(names.contains(self.container.context.injectedInProperty!))
return ServiceImp2()
}.resolvingProperties { _,_ in
XCTAssertNotNil(self.container.context.injectedInProperty)
XCTAssertTrue(names.contains(self.container.context.injectedInProperty!))
}
let _ = try! container.resolve() as Service
}
func testThatItDoesNotSetInjectedInTypeWhenResolvingWithCollaboration() {
let collaborator = DependencyContainer()
collaborator.register { () -> ServiceImp1 in
unowned let collaborator = collaborator
XCTAssertNil(collaborator.context.injectedInType)
return ServiceImp1()
}.resolvingProperties { collaborator, _ in
XCTAssertNil(collaborator.context.injectedInType)
}
container.collaborate(with: collaborator)
collaborator.collaborate(with: container)
let _ = try! container.resolve() as ServiceImp1
}
func testThatContextIsPreservedWhenResolvingWithCollaboration() {
let collaborator = DependencyContainer()
container.register { () -> Service in
XCTAssertTrue(self.container.context.resolvingType == Service.self)
let _ = try self.container.resolve() as ServiceImp1
return ServiceImp1() as Service
}.resolvingProperties { _,_ in
XCTAssertTrue(self.container.context.resolvingType == Service.self)
let _ = try self.container.resolve() as ServiceImp1
}
collaborator.register { () -> ServiceImp1 in
XCTAssertTrue(collaborator.context.resolvingType == ServiceImp1.self)
return ServiceImp1()
}.resolvingProperties { _,_ in
XCTAssertTrue(collaborator.context.resolvingType == ServiceImp1.self)
}
container.collaborate(with: collaborator)
collaborator.collaborate(with: container)
let _ = try! container.resolve() as Service
collaborator.reset()
}
}
@@ -36,46 +36,33 @@ class DefinitionTests: XCTestCase {
let tag1 = DependencyContainer.Tag.String("tag1")
let tag2 = DependencyContainer.Tag.String("tag2")
#if os(Linux)
var allTests: [(String, () throws -> Void)] {
return [
("testThatDefinitionKeyIsEqualBy_Type_Factory_Tag", testThatDefinitionKeyIsEqualBy_Type_Factory_Tag),
("testThatDefinitionKeysWithDifferentTypesAreNotEqual", testThatDefinitionKeysWithDifferentTypesAreNotEqual),
("testThatDefinitionKeysWithDifferentFactoriesAreNotEqual", testThatDefinitionKeysWithDifferentFactoriesAreNotEqual),
("testThatDefinitionKeysWithDifferentTagsAreNotEqual", testThatDefinitionKeysWithDifferentTagsAreNotEqual),
("testThatResolveDependenciesCallsResolveDependenciesBlock", testThatResolveDependenciesCallsResolveDependenciesBlock),
("testThatResolveDependenciesBlockIsNotCalledWhenPassedWrongInstance", testThatResolveDependenciesBlockIsNotCalledWhenPassedWrongInstance)
]
}
#endif
func testThatDefinitionKeyIsEqualBy_Type_Factory_Tag() {
let equalKey1 = DefinitionKey(protocolType: Service.self, factoryType: F1.self, associatedTag: tag1)
let equalKey2 = DefinitionKey(protocolType: Service.self, factoryType: F1.self, associatedTag: tag1)
let equalKey1 = DefinitionKey(type: Service.self, typeOfArguments: F1.self, tag: tag1)
let equalKey2 = DefinitionKey(type: Service.self, typeOfArguments: F1.self, tag: tag1)
XCTAssertEqual(equalKey1, equalKey2)
XCTAssertEqual(equalKey1.hashValue, equalKey2.hashValue)
}
func testThatDefinitionKeysWithDifferentTypesAreNotEqual() {
let keyWithDifferentType1 = DefinitionKey(protocolType: Service.self, factoryType: F1.self, associatedTag: nil)
let keyWithDifferentType2 = DefinitionKey(protocolType: AnyObject.self, factoryType: F1.self, associatedTag: nil)
let keyWithDifferentType1 = DefinitionKey(type: Service.self, typeOfArguments: F1.self, tag: nil)
let keyWithDifferentType2 = DefinitionKey(type: AnyObject.self, typeOfArguments: F1.self, tag: nil)
XCTAssertNotEqual(keyWithDifferentType1, keyWithDifferentType2)
XCTAssertNotEqual(keyWithDifferentType1.hashValue, keyWithDifferentType2.hashValue)
}
func testThatDefinitionKeysWithDifferentFactoriesAreNotEqual() {
let keyWithDifferentFactory1 = DefinitionKey(protocolType: Service.self, factoryType: F1.self, associatedTag: nil)
let keyWithDifferentFactory2 = DefinitionKey(protocolType: Service.self, factoryType: F2.self, associatedTag: nil)
let keyWithDifferentFactory1 = DefinitionKey(type: Service.self, typeOfArguments: F1.self, tag: nil)
let keyWithDifferentFactory2 = DefinitionKey(type: Service.self, typeOfArguments: F2.self, tag: nil)
XCTAssertNotEqual(keyWithDifferentFactory1, keyWithDifferentFactory2)
XCTAssertNotEqual(keyWithDifferentFactory1.hashValue, keyWithDifferentFactory2.hashValue)
}
func testThatDefinitionKeysWithDifferentTagsAreNotEqual() {
let keyWithDifferentTag1 = DefinitionKey(protocolType: Service.self, factoryType: F1.self, associatedTag: tag1)
let keyWithDifferentTag2 = DefinitionKey(protocolType: Service.self, factoryType: F1.self, associatedTag: tag2)
let keyWithDifferentTag1 = DefinitionKey(type: Service.self, typeOfArguments: F1.self, tag: tag1)
let keyWithDifferentTag2 = DefinitionKey(type: Service.self, typeOfArguments: F1.self, tag: tag2)
XCTAssertNotEqual(keyWithDifferentTag1, keyWithDifferentTag2)
XCTAssertNotEqual(keyWithDifferentTag1.hashValue, keyWithDifferentTag2.hashValue)
@@ -85,12 +72,13 @@ class DefinitionTests: XCTestCase {
var blockCalled = false
//given
let def = DefinitionOf<Service, () -> Service>(scope: .Prototype) { ServiceImp() as Service }.resolveDependencies { container, service in
blockCalled = true
let def = Definition<Service, ()>(scope: .unique) { ServiceImp() as Service }
.resolvingProperties { container, service in
blockCalled = true
}
//when
try! def.resolveDependenciesOf(ServiceImp(), withContainer: DependencyContainer())
try! def.resolveProperties(of: ServiceImp(), container: DependencyContainer())
//then
XCTAssertTrue(blockCalled)
@@ -100,15 +88,23 @@ class DefinitionTests: XCTestCase {
var blockCalled = false
//given
let def = DefinitionOf<Service, () -> Service>(scope: .Prototype) { ServiceImp() as Service }.resolveDependencies { container, service in
blockCalled = true
let def = Definition<Service, ()>(scope: .unique) { ServiceImp() as Service }
.resolvingProperties { container, service in
blockCalled = true
}
//when
try! def.resolveDependenciesOf(String(), withContainer: DependencyContainer())
try! def.resolveProperties(of: String(), container: DependencyContainer())
//then
XCTAssertFalse(blockCalled)
}
func testThatItRegistersOptionalTypesAsForwardedTypes() {
let def = Definition<Service, ()>(scope: .unique) { ServiceImp() as Service }
XCTAssertTrue(def.implementingTypes.contains(where: { $0 == Service?.self }))
}
}
+880
View File
@@ -0,0 +1,880 @@
//
// Dip
//
// Copyright (c) 2015 Olivier Halligon <olivier@halligon.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import XCTest
@testable import Dip
private protocol Service: AnyObject { }
private class ServiceImp1: Service { }
private class ServiceImp2: Service { }
private protocol Server: AnyObject {
var client: Client! { get }
}
private protocol Client: AnyObject {
var server: Server! { get }
}
class ResolvableService: Service, Resolvable {
var didResolveDependenciesCalled = false
func didResolveDependencies() {
XCTAssertFalse(didResolveDependenciesCalled, "didResolveDependencies should be called only once per instance")
didResolveDependenciesCalled = true
}
}
class DipTests: XCTestCase {
let container = DependencyContainer()
override func setUp() {
container.reset()
}
func testThatCreatingContainerWithConfigBlockDoesNotCreateRetainCycle() {
var container: DependencyContainer! = DependencyContainer() { container in
//compiler crashes if you try to capture container in capture list
//so instead we capture it in a variable
unowned let container = container
container.register { ServiceImp1() }
container.register { (_: ServiceImp1)->Service in
//referencing container in factory
let _ = container
return ServiceImp1() as Service
}.resolvingProperties { container, _ in
//when container is passed as argument there will be no retain cycle
let _ = container
}
}
let _ = try! container.resolve() as Service
weak var weakContainer = container
container = nil
XCTAssertNil(weakContainer)
}
func testThatItResolvesInstanceRegisteredWithoutTag() {
//given
container.register { ServiceImp1() as Service }
//when
let serviceInstance = try! container.resolve() as Service
//then
XCTAssertTrue(serviceInstance is ServiceImp1)
//and when
let anyService = try! container.resolve(Service.self)
//then
XCTAssertTrue(anyService is ServiceImp1)
//and when
let optService = try! container.resolve((Service?).self)
//then
XCTAssertTrue(optService is ServiceImp1)
}
func testThatItResolvesInstanceRegisteredWithTag() {
//given
container.register(tag: "service") { ServiceImp1() as Service }
//when
let serviceInstance = try! container.resolve(tag: "service") as Service
//then
XCTAssertTrue(serviceInstance is ServiceImp1)
//and when
let anyService = try! container.resolve(Service.self, tag: "service")
//then
XCTAssertTrue(anyService is ServiceImp1)
//and when
let optService = try! container.resolve((Service?).self, tag: "service")
//then
XCTAssertTrue(optService is ServiceImp1)
}
func testThatItResolvesDifferentInstancesRegisteredForDifferentTags() {
//given
container.register(tag: "service1") { ServiceImp1() as Service }
container.register(tag: "service2") { ServiceImp2() as Service }
//when
let service1Instance = try! container.resolve(tag: "service1") as Service
let service2Instance = try! container.resolve(tag: "service2") as Service
//then
XCTAssertTrue(service1Instance is ServiceImp1)
XCTAssertTrue(service2Instance is ServiceImp2)
//and when
let anyService1 = try! container.resolve(Service.self, tag: "service1")
let anyService2 = try! container.resolve(Service.self, tag: "service2")
//then
XCTAssertTrue(anyService1 is ServiceImp1)
XCTAssertTrue(anyService2 is ServiceImp2)
//and when
let optService1 = try! container.resolve((Service?).self, tag: "service1")
let optService2 = try! container.resolve((Service?).self, tag: "service2")
//then
XCTAssertTrue(optService1 is ServiceImp1)
XCTAssertTrue(optService2 is ServiceImp2)
}
func testThatNewRegistrationOverridesPreviousRegistration() {
//given
container.register { ServiceImp1() as Service }
let service1 = try! container.resolve() as Service
//when
container.register { ServiceImp2() as Service }
let service2 = try! container.resolve() as Service
//then
XCTAssertTrue(service1 is ServiceImp1)
XCTAssertTrue(service2 is ServiceImp2)
}
func testThatItCallsResolveDependenciesOnDefinition() {
//given
var resolveDependenciesCalled = false
container.register { ServiceImp1() as Service }.resolvingProperties { (c, s) in
resolveDependenciesCalled = true
}
//when
let _ = try! container.resolve() as Service
//then
XCTAssertTrue(resolveDependenciesCalled)
resolveDependenciesCalled = false
//and when
let _ = try! container.resolve(Service.self)
//then
XCTAssertTrue(resolveDependenciesCalled)
resolveDependenciesCalled = false
//and when
let _ = try! container.resolve((Service?).self)
//then
XCTAssertTrue(resolveDependenciesCalled)
resolveDependenciesCalled = false
}
func testThatItThrowsErrorIfCanNotFindDefinitionForType() {
//given
container.register { ServiceImp1() as ServiceImp1 }
//when
XCTAssertThrowsError(try self.container.resolve() as Service) { error in
guard case let DipError.definitionNotFound(key) = error else {
XCTFail("Thrown unexpected error: \(error)")
return
}
//then
let expectedKey = DefinitionKey(type: Service.self, typeOfArguments: Void.self, tag: nil)
XCTAssertEqual(key, expectedKey)
}
//and when
XCTAssertThrowsError(try self.container.resolve(Service.self)) { error in
guard case let DipError.definitionNotFound(key) = error else {
XCTFail("Thrown unexpected error: \(error)")
return
}
//then
let expectedKey = DefinitionKey(type: Service.self, typeOfArguments: Void.self, tag: nil)
XCTAssertEqual(key, expectedKey)
}
}
func testThatItThrowsErrorIfCanNotFindDefinitionForTag() {
//given
container.register(tag: "some tag") { ServiceImp1() as Service }
//when
XCTAssertThrowsError(try self.container.resolve(tag: "other tag") as Service) { error in
guard case let DipError.definitionNotFound(key) = error else {
XCTFail("Thrown unexpected error: \(error)")
return
}
//then
let expectedKey = DefinitionKey(type: Service.self, typeOfArguments: Void.self, tag: "other tag")
XCTAssertEqual(key, expectedKey)
}
//and when
XCTAssertThrowsError(try self.container.resolve(Service.self, tag: "other tag")) { error in
guard case let DipError.definitionNotFound(key) = error else {
XCTFail("Thrown unexpected error: \(error)")
return
}
//then
let expectedKey = DefinitionKey(type: Service.self, typeOfArguments: Void.self, tag: "other tag")
XCTAssertEqual(key, expectedKey)
}
}
func testThatItThrowsErrorIfCanNotFindDefinitionForFactoryWithArguments() {
//given
container.register { ServiceImp1() as Service }
//when
XCTAssertThrowsError(try self.container.resolve(arguments: "some string") as Service) { error in
guard case let DipError.definitionNotFound(key) = error else {
XCTFail("Thrown unexpected error: \(error)")
return
}
//then
let expectedKey = DefinitionKey(type: Service.self, typeOfArguments: String.self, tag: nil)
XCTAssertEqual(key, expectedKey)
}
//and when
XCTAssertThrowsError(try self.container.resolve(Service.self, arguments: "some string")) { error in
guard case let DipError.definitionNotFound(key) = error else {
XCTFail("Thrown unexpected error: \(error)")
return
}
//then
let expectedKey = DefinitionKey(type: Service.self, typeOfArguments: String.self, tag: nil)
XCTAssertEqual(key, expectedKey)
}
}
func testThatItThrowsErrorIfConstructorThrows() {
//given
let failedKey = DefinitionKey(type: Any.self, typeOfArguments: Any.self)
let expectedError = DipError.definitionNotFound(key: failedKey)
container.register { () throws -> Service in throw expectedError }
//when
XCTAssertThrowsError(try self.container.resolve() as Service) { error in
guard case let DipError.definitionNotFound(key) = error, key == failedKey else {
XCTFail("Thrown unexpected error: \(error)")
return
}
}
//and when
XCTAssertThrowsError(try self.container.resolve(Service.self)) { error in
guard case let DipError.definitionNotFound(key) = error, key == failedKey else {
XCTFail("Thrown unexpected error: \(error)")
return
}
}
}
func testThatItThrowsErrorIfFailsToResolveDependency() {
//given
let failedKey = DefinitionKey(type: Any.self, typeOfArguments: Any.self)
let expectedError = DipError.definitionNotFound(key: failedKey)
container.register { ServiceImp1() as Service }
.resolvingProperties { container, service in
//simulate throwing error when resolving dependency
throw expectedError
}
//when
XCTAssertThrowsError(try self.container.resolve() as Service) { error in
guard case let DipError.definitionNotFound(key) = error, key == failedKey else {
XCTFail("Thrown unexpected error: \(error)")
return
}
}
//and when
XCTAssertThrowsError(try self.container.resolve(Service.self)) { error in
guard case let DipError.definitionNotFound(key) = error, key == failedKey else {
XCTFail("Thrown unexpected error: \(error)")
return
}
}
}
func testThatItCallsDidResolveDependenciesOnResolvableIntance() {
//given
container.register { ResolvableService() as Service }
.resolvingProperties { _, service in
XCTAssertFalse((service as! ResolvableService).didResolveDependenciesCalled, "didResolveDependencies should not be called yet")
return
}
container.register(tag: "graph") { ResolvableService() as Service }
.resolvingProperties { _, service in
XCTAssertFalse((service as! ResolvableService).didResolveDependenciesCalled, "didResolveDependencies should not be called yet")
return
}
container.register(.singleton, tag: "singleton") { ResolvableService() as Service }
.resolvingProperties { _, service in
XCTAssertFalse((service as! ResolvableService).didResolveDependenciesCalled, "didResolveDependencies should not be called yet")
return
}
//when
let service = try! container.resolve() as Service
//then
XCTAssertTrue((service as! ResolvableService).didResolveDependenciesCalled)
//and when
let graphService = try! container.resolve(tag: "graph") as Service
//then
XCTAssertTrue((graphService as! ResolvableService).didResolveDependenciesCalled)
//and when
let singletonService = try! container.resolve(tag: "singleton") as Service
let _ = try! container.resolve(tag: "singleton") as Service
//then
XCTAssertTrue((singletonService as! ResolvableService).didResolveDependenciesCalled)
}
func testThatItCallsDidResolveDependenciesInReverseOrder() {
class ResolvableService: Service, Resolvable {
static var resolved: [Service] = []
func didResolveDependencies() {
ResolvableService.resolved.append(self)
}
}
//given
var resolveDependenciesCalled = false
var service2: Service!
container.register { ResolvableService() as Service }
.resolvingProperties { _, service in
if !resolveDependenciesCalled {
resolveDependenciesCalled = true
service2 = try self.container.resolve() as Service
}
return
}
//when
let service1 = try! container.resolve() as Service
//then
XCTAssertTrue(ResolvableService.resolved.first === service2)
XCTAssertTrue(ResolvableService.resolved.last === service1)
}
func testItCallsResolveDependenciesOnResolableInstance() {
class Class: Resolvable {
var resolveDependenciesCalled = false
func resolveDependencies(_ container: DependencyContainer) {
resolveDependenciesCalled = true
}
}
class SubClass: Class {
override func resolveDependencies(_ container: DependencyContainer) {
super.resolveDependencies(container)
}
}
container.register { Class() }
.resolvingProperties { _, instance in
XCTAssertTrue(instance.resolveDependenciesCalled)
}
container.register { SubClass() }
.resolvingProperties { _, instance in
XCTAssertTrue(instance.resolveDependenciesCalled)
}
let _ = try! container.resolve() as Class
let _ = try! container.resolve() as SubClass
}
func testThatItResolvesCircularDependencies() {
class ResolvableServer: Server, Resolvable {
weak var client: Client!
weak var secondClient: Client!
init(client: Client) {
self.client = client
}
var didResolveDependenciesCalled = false
func didResolveDependencies() {
XCTAssertFalse(didResolveDependenciesCalled, "didResolveDependencies should be called only once per instance")
didResolveDependenciesCalled = true
XCTAssertNotNil(self.client)
XCTAssertNotNil(self.secondClient)
XCTAssertNotNil(self.client?.server)
XCTAssertNotNil(self.secondClient)
XCTAssertNotNil(self.secondClient?.server)
}
}
class ResolvableClient: Client, Resolvable {
var server: Server!
var secondServer: Server!
var didResolveDependenciesCalled = false
func didResolveDependencies() {
XCTAssertFalse(didResolveDependenciesCalled, "didResolveDependencies should be called only once per instance")
didResolveDependenciesCalled = true
XCTAssertNotNil(self.server)
XCTAssertNotNil(self.secondServer)
XCTAssertNotNil(self.server?.client)
XCTAssertNotNil(self.secondServer?.client)
}
}
//given
container.register { try ResolvableServer(client: self.container.resolve()) as Server }
.resolvingProperty(\ResolvableServer.secondClient, as: Client.self)
container.register { ResolvableClient() as Client }
.resolvingProperty(\ResolvableClient.server, factory: { try $0.resolve() })
.resolvingProperty(\ResolvableClient.secondServer)
//when
let client = (try! container.resolve() as Client) as! ResolvableClient
let server = client.server as! ResolvableServer
let secondServer = client.secondServer as! ResolvableServer
let secondClient = server.secondClient as! ResolvableClient
//then
XCTAssertTrue(client === server.client)
XCTAssertTrue(client === server.secondClient)
XCTAssertTrue(client === secondServer.client)
XCTAssertTrue(client === secondServer.secondClient)
XCTAssertTrue(client === secondClient)
XCTAssertTrue(server === secondServer)
XCTAssertTrue(client.didResolveDependenciesCalled)
XCTAssertTrue(server.didResolveDependenciesCalled)
}
func testThatItValidatesConfiguration() {
//given
var createdService1 = false
var createdService2 = false
var createdService3 = false
var createdService = false
container.register { ServiceImp1() }
.resolvingProperties { container, _ in
if container.context.resolvingType == ServiceImp1.self {
createdService1 = true
}
if container.context.resolvingType == Service.self {
createdService = true
}
}.implements(Service.self)
container.register(tag: "tag") { ServiceImp2() as Service }
.resolvingProperties { _,_ in
createdService2 = true
}
container.register { (arg: String) in ServiceImp1() }
.resolvingProperties { _,_ in
createdService3 = true
}
//then
XCTAssertNoThrow(try self.container.validate("arg"))
XCTAssertTrue(createdService1)
XCTAssertTrue(createdService2)
XCTAssertTrue(createdService3)
XCTAssertTrue(createdService)
}
func testThatItPicksRuntimeArgumentsWhenValidatingConfiguration() {
//given
let expectedIntArgument = 1
let expectedStringArgument = "a"
container.register { (a: Int) -> Service in
XCTAssertEqual(a, expectedIntArgument)
return ServiceImp1() as Service
}
container.register { (a: Int, b: String) -> Service in
XCTAssertEqual(a, expectedIntArgument)
XCTAssertEqual(b, expectedStringArgument)
return ServiceImp1() as Service
}
//then
XCTAssertNoThrow(
try self.container.validate(
"1",
expectedIntArgument,
"x",
(expectedStringArgument, expectedIntArgument),
(expectedIntArgument, expectedStringArgument)
)
)
}
func testThatItFailsValidationIfNoMatchingArgumentsFound() {
//given
container.register { (a: Int) -> Service in ServiceImp1() as Service }
//then
XCTAssertThrowsError(try self.container.validate()) { error in
guard error is DipError else {
XCTFail("Thrown unexpected error: \(error)")
return
}
}
XCTAssertThrowsError(try self.container.validate("1")) { error in
guard error is DipError else {
XCTFail("Thrown unexpected error: \(error)")
return
}
}
}
func testThatItFailsValidationOnlyForDipErrors() {
//given
enum SomeError: Error { case error }
container.register { () -> Service in
throw SomeError.error
}
//then
XCTAssertNoThrow(try self.container.validate())
//given
let key = DefinitionKey(type: Service.self, typeOfArguments: Void.self, tag: nil)
container.register { () -> Service in
throw DipError.definitionNotFound(key: key)
}
//then
XCTAssertThrowsError(try self.container.validate()) { error in
guard case let DipError.definitionNotFound(_key) = error, _key == key else {
XCTFail("Thrown unexpected error: \(error)")
return
}
}
}
}
extension DipTests {
func testThatItCanResolveUsingContainersCollaboration() {
//given
let collaborator = DependencyContainer()
collaborator.register { ResolvableService() as Service }
container.register { "something" }
//when
container.collaborate(with: collaborator)
//then
XCTAssertNoThrow(try self.container.resolve() as Service)
XCTAssertNoThrow(try self.container.resolve(Service.self))
XCTAssertNoThrow(try collaborator.resolve() as String)
XCTAssertNoThrow(try collaborator.resolve(String.self))
}
func testThatCollaboratingWithSelfIsIgnored() {
let collaborator = DependencyContainer()
collaborator.collaborate(with: collaborator)
XCTAssertTrue(collaborator._collaborators.isEmpty, "Container should not collaborate with itself")
}
func testThatCollaboratingContainersAreWeakReferences() {
//given
var collaborator: DependencyContainer? = DependencyContainer()
weak var weakCollaborator = collaborator
//when
container.collaborate(with: collaborator!)
collaborator = nil
//then
XCTAssertNil(weakCollaborator)
}
func testThatCollaboratingContainersReuseInstancesResolvedByAnotherContainer() {
//given
class ServerImp: Server {
weak var client: Client!
init(client: Client) { self.client = client }
}
class ClientImp: Client {
var server: Server!
var anotherServer: Server!
}
let serverContainer = DependencyContainer()
serverContainer.register { ServerImp(client: $0) as Server }
let clientContainer = DependencyContainer()
clientContainer.register { ClientImp() as Client }
.resolvingProperties { container, client in
let client = client as! ClientImp
client.server = try container.resolve() as Server
client.anotherServer = try container.resolve() as Server
}
//when
serverContainer.collaborate(with: clientContainer)
clientContainer.collaborate(with: serverContainer)
var client = try? clientContainer.resolve() as Client
//then
XCTAssertNotNil(client)
XCTAssertTrue(client === client?.server?.client)
XCTAssertTrue(client === (client as? ClientImp)?.anotherServer?.client)
XCTAssertTrue(client?.server === (client as? ClientImp)?.anotherServer)
client = try? serverContainer.resolve() as Client
//then
XCTAssertNotNil(client)
XCTAssertTrue(client === client?.server?.client)
XCTAssertTrue(client === (client as? ClientImp)?.anotherServer?.client)
XCTAssertTrue(client?.server === (client as? ClientImp)?.anotherServer)
}
func testThatCollaborationReferencesAreRecursivelyUpdate() {
let container = DependencyContainer()
container.register(.singleton){ ResolvableService() as Service }
//when
let collaborator1 = DependencyContainer()
let collaborator2 = DependencyContainer()
let collaborator3 = DependencyContainer()
let collaborator4 = DependencyContainer()
collaborator1.collaborate(with: container)
XCTAssertTrue(collaborator1.resolvedInstances.sharedSingletonsBox === container.resolvedInstances.sharedSingletonsBox)
collaborator2.collaborate(with: container)
XCTAssertTrue(collaborator2.resolvedInstances.sharedSingletonsBox === container.resolvedInstances.sharedSingletonsBox)
collaborator3.collaborate(with: collaborator1)
XCTAssertTrue(collaborator3.resolvedInstances.sharedSingletonsBox === container.resolvedInstances.sharedSingletonsBox)
collaborator4.collaborate(with: collaborator2)
XCTAssertTrue(collaborator4.resolvedInstances.sharedSingletonsBox === container.resolvedInstances.sharedSingletonsBox)
let service1 = try! collaborator1.resolve() as Service
let service2 = try! collaborator2.resolve() as Service
let service3 = try! collaborator3.resolve() as Service
let service4 = try! collaborator4.resolve() as Service
let serviceRoot = try! container.resolve() as Service
XCTAssertTrue(service1 === service2)
XCTAssertTrue(service1 === service3)
XCTAssertTrue(service1 === service4)
XCTAssertTrue(service1 === serviceRoot)
XCTAssertTrue(service2 === serviceRoot)
XCTAssertTrue(service3 === serviceRoot)
XCTAssertTrue(service4 === serviceRoot)
}
class RootService {}
class ServiceClient {
let name: String
let service: RootService
init(name: String, service: RootService) {
self.name = name
self.service = service
}
}
func testThatContainersShareTheirSingletonsOnlyWithCollaborators() {
let container = DependencyContainer()
container.register(.singleton) { RootService() }
let collaborator1 = DependencyContainer()
collaborator1.register(.singleton) {
ServiceClient(name: "1", service: $0)
}
let collaborator2 = DependencyContainer()
collaborator2.register(.singleton) {
ServiceClient(name: "2", service: $0)
}
collaborator1.collaborate(with: container)
collaborator2.collaborate(with: container)
let client2 = try! collaborator2.resolve() as ServiceClient
let client1 = try! collaborator1.resolve() as ServiceClient
XCTAssertEqual(client1.name, "1")
XCTAssertEqual(client2.name, "2")
XCTAssertTrue(client1.service === client2.service)
}
func testThatContainerAutowireBeforeCollaboration() {
let container = DependencyContainer()
container.register(.singleton) { RootService() }
let collaborator1 = DependencyContainer()
collaborator1.register(.singleton) {
ServiceClient(name: "1", service: $0)
}
let collaborator2 = DependencyContainer()
collaborator2.register(.singleton) {
ServiceClient(name: "2", service: $0)
}
collaborator1.collaborate(with: container, collaborator2)
collaborator2.collaborate(with: container, collaborator1)
let client2 = try! collaborator2.resolve() as ServiceClient
let client1 = try! collaborator1.resolve() as ServiceClient
XCTAssertEqual(client1.name, "1")
XCTAssertEqual(client2.name, "2")
XCTAssertTrue(client1.service === client2.service)
}
}
class Manager {}
class AnotherManager {}
class Object {
let manager: Manager?
init(with container: DependencyContainer) {
self.manager = try? container.resolve()
}
}
class Owner {
var manager: Manager?
}
extension DipTests {
func testThatItCanHandleSeparateContainersAndTheirCollaboration() {
let container = self.container
let anotherContainer = DependencyContainer()
anotherContainer.register { Object(with: anotherContainer) }
container.collaborate(with: anotherContainer)
container
.register { Owner() }
.resolvingProperties { $1.manager = try $0.resolve() }
container.register(.singleton) { AnotherManager() }
container.register(.singleton) { Manager() }
let manager: Manager? = try? container.resolve()
let another: AnotherManager? = try? container.resolve()
var owner: Owner? = try? container.resolve(arguments: 1, "")
let object: Object? = try? container.resolve()
owner = try? container.resolve()
let nonNilValues: [Any?] = [another, manager, owner, object, object?.manager]
nonNilValues.forEach { XCTAssertNotNil($0) }
XCTAssertTrue(
owner?.manager
.flatMap { value in
manager.flatMap { $0 === value }
}
?? false
)
}
}
extension DipTests {
// https://bugs.swift.org/browse/SR-8878
func test_weak_mirror_regression() {
class A {
static var released = false
deinit {
A.released = true
}
}
class B {
static var released = false
weak var a: A?
init(a: A) {
self.a = a
}
deinit {
B.released = true
}
}
let container = DependencyContainer()
let tag = "my_tag"
container.register(.unique, tag: tag, factory: B.init(a:))
do {
let a0 = A()
let _: B = try container.resolve(tag: tag, arguments: a0)
XCTAssertTrue(B.released)
// Due to regression in swift 4.2 Mirror retains weak children
// https://bugs.swift.org/browse/SR-8878
XCTAssertFalse(A.released)
} catch {
}
}
}
+252
View File
@@ -0,0 +1,252 @@
//
// DipUI
//
// Copyright (c) 2016 Ilya Puchka <ilyapuchka@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#if (canImport(UIKit) || canImport(AppKit)) && !SWIFT_PACKAGE
import XCTest
@testable import Dip
#if canImport(UIKit)
import UIKit
typealias Storyboard = UIStoryboard
typealias ViewController = UIViewController
typealias StoryboardName = String
extension UIStoryboard {
@nonobjc
@discardableResult func instantiateViewControllerWithIdentifier(_ identifier: String) -> UIViewController {
return instantiateViewController(withIdentifier: identifier)
}
}
#else
import AppKit
typealias Storyboard = NSStoryboard
typealias ViewController = NSViewController
typealias StoryboardName = NSStoryboard.Name
extension NSStoryboard {
@discardableResult func instantiateViewControllerWithIdentifier(_ identifier: String) -> NSViewController {
return instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(identifier)) as! NSViewController
}
}
#endif
#if os(iOS)
let storyboardName: StoryboardName = "UIStoryboard"
#elseif os(tvOS)
let storyboardName: StoryboardName = "TVStoryboard"
#else
let storyboardName: StoryboardName = StoryboardName("NSStoryboard")
#endif
class DipViewController: ViewController, StoryboardInstantiatable {}
class NilTagViewController: ViewController, StoryboardInstantiatable {}
class DipUITests: XCTestCase {
let storyboard: Storyboard = {
let bundle = Bundle(for: DipUITests.self)
return Storyboard(name: storyboardName, bundle: bundle)
}()
func testThatViewControllerHasDipTagProperty() {
let viewController = storyboard.instantiateViewControllerWithIdentifier("DipViewController")
XCTAssertEqual(viewController.dipTag, "vc")
}
func testThatItDoesNotResolveIfContainerIsNotSet() {
let container = DependencyContainer()
container.register(tag: "vc") { ViewController() }
.resolvingProperties { _, _ in
XCTFail("Should not resolve when container is not set.")
}
storyboard.instantiateViewControllerWithIdentifier("DipViewController")
}
func testThatItDoesNotResolveIfTagIsNotSet() {
let container = DependencyContainer()
container.register(tag: "vc") { ViewController() }
.resolvingProperties { _, _ in
XCTFail("Should not resolve when container is not set.")
}
DependencyContainer.uiContainers = [container]
storyboard.instantiateViewControllerWithIdentifier("ViewController")
}
func testThatItResolvesIfContainerAndStringTagAreSet() {
var resolved = false
let container = DependencyContainer()
container.register(storyboardType: DipViewController.self, tag: "vc")
.resolvingProperties { _, _ in
resolved = true
}
DependencyContainer.uiContainers = [container]
storyboard.instantiateViewControllerWithIdentifier("DipViewController")
XCTAssertTrue(resolved, "Should resolve when container and tag are set.")
}
func testThatItResolvesIfContainerAndNilTagAreSet() {
var resolved = false
let container = DependencyContainer()
container.register(storyboardType: NilTagViewController.self)
.resolvingProperties { _, _ in
resolved = true
}
DependencyContainer.uiContainers = [container]
storyboard.instantiateViewControllerWithIdentifier("NilTagViewController")
XCTAssertTrue(resolved, "Should resolve when container and nil tag are set.")
}
func testThatItDoesNotResolveIfTagDoesNotMatch() {
let container = DependencyContainer()
container.register(storyboardType: DipViewController.self, tag: "wrong tag")
.resolvingProperties { _, _ in
XCTFail("Should not resolve when container is not set.")
}
DependencyContainer.uiContainers = [container]
storyboard.instantiateViewControllerWithIdentifier("DipViewController")
}
func testThatItResolvesWithDefinitionWithNoTag() {
var resolved = false
let container = DependencyContainer()
container.register(storyboardType: DipViewController.self)
.resolvingProperties { _, _ in
resolved = true
}
DependencyContainer.uiContainers = [container]
storyboard.instantiateViewControllerWithIdentifier("DipViewController")
XCTAssertTrue(resolved, "Should fallback to definition with no tag.")
}
func testThatItIteratesUIContainers() {
var resolved = false
let container1 = DependencyContainer()
let container2 = DependencyContainer()
container2.register(storyboardType: DipViewController.self, tag: "vc")
.resolvingProperties { container, _ in
XCTAssertTrue(container === container2)
resolved = true
}
DependencyContainer.uiContainers = [container1, container2]
storyboard.instantiateViewControllerWithIdentifier("DipViewController")
XCTAssertTrue(resolved, "Should resolve using second container")
}
}
protocol SomeService: AnyObject {
var delegate: SomeServiceDelegate? { get set }
}
protocol SomeServiceDelegate: AnyObject { }
class SomeServiceImp: SomeService {
weak var delegate: SomeServiceDelegate?
init(delegate: SomeServiceDelegate) {
self.delegate = delegate
}
init(){}
}
protocol OtherService: AnyObject {
var delegate: OtherServiceDelegate? { get set }
}
protocol OtherServiceDelegate: AnyObject {}
class OtherServiceImp: OtherService {
weak var delegate: OtherServiceDelegate?
init(delegate: OtherServiceDelegate){
self.delegate = delegate
}
init(){}
}
protocol SomeScreen: AnyObject {
var someService: SomeService? { get set }
var otherService: OtherService? { get set }
}
class ViewControllerImp: SomeScreen, SomeServiceDelegate, OtherServiceDelegate {
var someService: SomeService?
var otherService: OtherService?
init(){}
}
extension DipUITests {
func testThatItDoesNotCreateNewInstanceWhenResolvingDependenciesOfExternalInstance() {
let container = DependencyContainer()
//given
var factoryCalled = false
container.register(.shared) { () -> SomeScreen in
factoryCalled = true
return ViewControllerImp() as SomeScreen
}
//when
let screen = ViewControllerImp()
try! container.resolveDependencies(of: screen as SomeScreen)
//then
XCTAssertFalse(factoryCalled, "Container should not create new instance when resolving dependencies of external instance.")
}
func testThatItResolvesInstanceThatImplementsSeveralProtocols() {
let container = DependencyContainer()
//given
container.register(.shared) { ViewControllerImp() as SomeScreen }
.resolvingProperties { container, resolved in
//manually provide resolved instance for the delegate properties
resolved.someService = try container.resolve() as SomeService
resolved.someService?.delegate = resolved as? SomeServiceDelegate
resolved.otherService = try container.resolve(arguments: resolved as! OtherServiceDelegate) as OtherService
}
container.register(.shared) { SomeServiceImp() as SomeService }
container.register(.shared) { OtherServiceImp(delegate: $0) as OtherService }
//when
let screen = try! container.resolve() as SomeScreen
//then
XCTAssertNotNil(screen.someService)
XCTAssertNotNil(screen.otherService)
XCTAssertTrue(screen.someService?.delegate === screen)
XCTAssertTrue(screen.otherService?.delegate === screen)
}
}
#endif
@@ -51,31 +51,9 @@ class RuntimeArgumentsTests: XCTestCase {
let container = DependencyContainer()
#if os(Linux)
var allTests: [(String, () throws -> Void)] {
return [
("testThatItResolvesInstanceWithOneArgument", testThatItResolvesInstanceWithOneArgument),
("testThatItResolvesInstanceWithTwoArguments", testThatItResolvesInstanceWithTwoArguments),
("testThatItResolvesInstanceWithThreeArguments", testThatItResolvesInstanceWithThreeArguments),
("testThatItResolvesInstanceWithFourArguments", testThatItResolvesInstanceWithFourArguments),
("testThatItResolvesInstanceWithFiveArguments", testThatItResolvesInstanceWithFiveArguments),
("testThatItResolvesInstanceWithSixArguments", testThatItResolvesInstanceWithSixArguments),
("testThatItRegistersDifferentFactoriesForDifferentNumberOfArguments", testThatItRegistersDifferentFactoriesForDifferentNumberOfArguments),
("testThatItRegistersDifferentFactoriesForDifferentTypesOfArguments", testThatItRegistersDifferentFactoriesForDifferentTypesOfArguments),
("testThatItRegistersDifferentFactoriesForDifferentOrderOfArguments", testThatItRegistersDifferentFactoriesForDifferentOrderOfArguments),
("testThatNewRegistrationWithSameArgumentsOverridesPreviousRegistration", testThatNewRegistrationWithSameArgumentsOverridesPreviousRegistration),
("testThatDifferentFactoriesRegisteredIfArgumentIsOptional", testThatDifferentFactoriesRegisteredIfArgumentIsOptional)
]
}
func setUp() {
container.reset()
}
#else
override func setUp() {
container.reset()
}
#endif
func testThatItResolvesInstanceWithOneArgument() {
//given
@@ -86,10 +64,16 @@ class RuntimeArgumentsTests: XCTestCase {
})
//when
let service = try! container.resolve(withArguments: arg1) as Service
let service = try! container.resolve(arguments: arg1) as Service
//then
XCTAssertTrue(service is ServiceImp1)
//when
let anyService = try! container.resolve(Service.self, arguments: arg1)
//then
XCTAssertTrue(anyService is ServiceImp1)
}
func testThatItResolvesInstanceWithTwoArguments() {
@@ -102,10 +86,16 @@ class RuntimeArgumentsTests: XCTestCase {
}
//when
let service = try! container.resolve(withArguments: arg1, arg2) as Service
let service = try! container.resolve(arguments: arg1, arg2) as Service
//then
XCTAssertTrue(service is ServiceImp1)
//when
let anyService = try! container.resolve(Service.self, arguments: arg1, arg2)
//then
XCTAssertTrue(anyService is ServiceImp1)
}
func testThatItResolvesInstanceWithThreeArguments() {
@@ -118,10 +108,16 @@ class RuntimeArgumentsTests: XCTestCase {
}
//when
let service = try! container.resolve(withArguments: arg1, arg2, arg3) as Service
let service = try! container.resolve(arguments: arg1, arg2, arg3) as Service
//then
XCTAssertTrue(service is ServiceImp1)
//when
let anyService = try! container.resolve(Service.self, arguments: arg1, arg2, arg3)
//then
XCTAssertTrue(anyService is ServiceImp1)
}
func testThatItResolvesInstanceWithFourArguments() {
@@ -135,10 +131,16 @@ class RuntimeArgumentsTests: XCTestCase {
}
//when
let service = try! container.resolve(withArguments: arg1, arg2, arg3, arg4) as Service
let service = try! container.resolve(arguments: arg1, arg2, arg3, arg4) as Service
//then
XCTAssertTrue(service is ServiceImp1)
//when
let anyService = try! container.resolve(Service.self, arguments: arg1, arg2, arg3, arg4)
//then
XCTAssertTrue(anyService is ServiceImp1)
}
func testThatItResolvesInstanceWithFiveArguments() {
@@ -153,10 +155,16 @@ class RuntimeArgumentsTests: XCTestCase {
}
//when
let service = try! container.resolve(withArguments: arg1, arg2, arg3, arg4, arg5) as Service
let service = try! container.resolve(arguments: arg1, arg2, arg3, arg4, arg5) as Service
//then
XCTAssertTrue(service is ServiceImp1)
//when
let anyService = try! container.resolve(Service.self, arguments: arg1, arg2, arg3, arg4, arg5)
//then
XCTAssertTrue(anyService is ServiceImp1)
}
func testThatItResolvesInstanceWithSixArguments() {
@@ -172,10 +180,16 @@ class RuntimeArgumentsTests: XCTestCase {
}
//when
let service = try! container.resolve(withArguments: arg1, arg2, arg3, arg4, arg5, arg6) as Service
let service = try! container.resolve(arguments: arg1, arg2, arg3, arg4, arg5, arg6) as Service
//then
XCTAssertTrue(service is ServiceImp1)
//when
let anyService = try! container.resolve(Service.self, arguments: arg1, arg2, arg3, arg4, arg5, arg6)
//then
XCTAssertTrue(anyService is ServiceImp1)
}
func testThatItRegistersDifferentFactoriesForDifferentNumberOfArguments() {
@@ -185,8 +199,8 @@ class RuntimeArgumentsTests: XCTestCase {
container.register { (a1: Int, a2: Int) in ServiceImp2() as Service }
//when
let service1 = try! container.resolve(withArguments: arg1) as Service
let service2 = try! container.resolve(withArguments: arg1, arg2) as Service
let service1 = try! container.resolve(arguments: arg1) as Service
let service2 = try! container.resolve(arguments: arg1, arg2) as Service
//then
XCTAssertTrue(service1 is ServiceImp1)
@@ -200,8 +214,8 @@ class RuntimeArgumentsTests: XCTestCase {
container.register(factory: { (a1: String) in ServiceImp2() as Service })
//when
let service1 = try! container.resolve(withArguments: arg1) as Service
let service2 = try! container.resolve(withArguments: arg2) as Service
let service1 = try! container.resolve(arguments: arg1) as Service
let service2 = try! container.resolve(arguments: arg2) as Service
//then
XCTAssertTrue(service1 is ServiceImp1)
@@ -215,8 +229,8 @@ class RuntimeArgumentsTests: XCTestCase {
container.register { (a1: String, a2: Int) in ServiceImp2() as Service }
//when
let service1 = try! container.resolve(withArguments: arg1, arg2) as Service
let service2 = try! container.resolve(withArguments: arg2, arg1) as Service
let service1 = try! container.resolve(arguments: arg1, arg2) as Service
let service2 = try! container.resolve(arguments: arg2, arg1) as Service
//then
XCTAssertTrue(service1 is ServiceImp1)
@@ -227,11 +241,11 @@ class RuntimeArgumentsTests: XCTestCase {
//given
let arg1 = 1, arg2 = 2
container.register { (a1: Int, a2: Int) in ServiceImp1() as Service }
let service1 = try! container.resolve(withArguments: arg1, arg2) as Service
let service1 = try! container.resolve(arguments: arg1, arg2) as Service
//when
container.register { (a1: Int, a2: Int) in ServiceImp2() as Service }
let service2 = try! container.resolve(withArguments: arg1, arg2) as Service
let service2 = try! container.resolve(arguments: arg1, arg2) as Service
//then
XCTAssertTrue(service1 is ServiceImp1)
@@ -240,20 +254,28 @@ class RuntimeArgumentsTests: XCTestCase {
func testThatDifferentFactoriesRegisteredIfArgumentIsOptional() {
//given
let name1 = "1", name2 = "2", name3 = "3"
let name1 = "1", name2 = "2"
container.register { (port: Int, url: String) in ServiceImp(name: name1, baseURL: url, port: port) as Service }
container.register { (port: Int, url: String?) in ServiceImp(name: name2, baseURL: url!, port: port) as Service }
container.register { (port: Int, url: String!) in ServiceImp(name: name3, baseURL: url, port: port) as Service }
//when
let service1 = try! container.resolve(withArguments: 80, "http://example.com") as Service
let service2 = try! container.resolve(withArguments: 80, "http://example.com" as String?) as Service
let service3 = try! container.resolve(withArguments: 80, "http://example.com" as String!) as Service
let service1 = try! container.resolve(arguments: 80, "http://example.com") as Service
let service2 = try! container.resolve(arguments: 80, "http://example.com" as String?) as Service
//then
XCTAssertEqual(service1.name, name1)
XCTAssertEqual(service2.name, name2)
XCTAssertEqual(service3.name, name3)
//Due to incomplete implementation of SE-0054 (bug: https://bugs.swift.org/browse/SR-2143)
//registering definition with T? and T! arguments types will produce two different definitions
//but when argument of T! will be passed to `resolve` method it will be transformed to T?
//and wrong definition will be used
//When fixed using T? and T! should not register two different definitions
// let name3 = "3"
// container.register { (port: Int, url: String!) in ServiceImp(name: name3, baseURL: url, port: port) as Service }
// let service3 = try! container.resolve(arguments: 80, "http://example.com" as String!) as Service
// XCTAssertEqual(service3.name, name3)
}
}
@@ -22,14 +22,15 @@
// THE SOFTWARE.
//
#if canImport(ObjectiveC)
import XCTest
@testable import Dip
private protocol Server: class {
var client: Client? { get set }
private protocol Server: AnyObject {
var client: Client! { get set }
}
private protocol Client: class {
private protocol Client: AnyObject {
var server: Server { get }
}
@@ -45,11 +46,11 @@ private func ==<T: ClientImp>(lhs: T, rhs: T) -> Bool {
}
private class ServerImp: Server, Hashable {
weak var client: Client?
weak var client: Client!
init() {}
var hashValue: Int {
return unsafeAddressOf(self).hashValue
func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self))
}
}
@@ -62,37 +63,21 @@ private var resolvedClients = Array<ClientImp>()
private var container: DependencyContainer!
#if os(Linux)
import Glibc
private var lock: pthread_spinlock_t = 0
private let resolveClientSync: () -> Client? = {
var clientPointer: UnsafeMutablePointer<Void> = nil
clientPointer = dispatch_sync { _ in
let resolved = try! container.resolve() as Client
return UnsafeMutablePointer(Unmanaged.passUnretained(resolved as! ClientImp).toOpaque())
}
return Unmanaged<ClientImp>.fromOpaque(COpaquePointer(clientPointer)).takeUnretainedValue()
}
#else
let queue = NSOperationQueue()
let lock = NSRecursiveLock()
let queue = OperationQueue()
let lock = RecursiveLock()
private let resolveClientSync: () -> Client? = {
var client: Client?
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
DispatchQueue.global(qos: .default).sync() {
client = try! container.resolve() as Client
}
return client
}
#endif
let resolveServerAsync = {
let service = try! container.resolve() as Server
let server = try! container.resolve() as Server
lock.lock()
resolvedServers.insert(service as! ServerImp)
resolvedServers.insert(server as! ServerImp)
lock.unlock()
}
@@ -105,29 +90,8 @@ let resolveClientAsync = {
class ThreadSafetyTests: XCTestCase {
#if os(Linux)
init() {
pthread_spin_init(&lock, 0)
}
var allTests: [(String, () throws -> Void)] {
return [
("testSingletonThreadSafety", testSingletonThreadSafety),
("testFactoryThreadSafety", testFactoryThreadSafety),
("testCircularReferenceThreadSafety", testCircularReferenceThreadSafety)
]
}
func setUp() {
container = DependencyContainer()
}
func tearDown() {
resolvedServers.removeAll()
resolvedClients.removeAll()
}
#else
override func setUp() {
Dip.logLevel = .Verbose
container = DependencyContainer()
}
@@ -135,27 +99,15 @@ class ThreadSafetyTests: XCTestCase {
resolvedServers.removeAll()
resolvedClients.removeAll()
}
#endif
func testSingletonThreadSafety() {
container.register(.Singleton) { ServerImp() as Server }
container.register(.singleton) { ServerImp() as Server }
for _ in 0..<100 {
#if os(Linux)
dispatch_async({ _ in
resolveServerAsync()
return nil
})
#else
queue.addOperationWithBlock(resolveServerAsync)
#endif
queue.addOperation(resolveServerAsync)
}
#if os(Linux)
sleep(1)
#else
queue.waitUntilAllOperationsAreFinished()
#endif
XCTAssertEqual(resolvedServers.count, 1, "Should create only one instance")
}
@@ -165,52 +117,30 @@ class ThreadSafetyTests: XCTestCase {
container.register { ServerImp() as Server }
for _ in 0..<100 {
#if os(Linux)
dispatch_async({ _ in
resolveServerAsync()
return nil
})
#else
queue.addOperationWithBlock(resolveServerAsync)
#endif
queue.addOperation(resolveServerAsync)
}
#if os(Linux)
sleep(1)
#else
queue.waitUntilAllOperationsAreFinished()
#endif
XCTAssertEqual(resolvedServers.count, 100, "All instances should be different")
}
func testCircularReferenceThreadSafety() {
container.register(.ObjectGraph) {
container.register {
ClientImp(server: try container.resolve()) as Client
}
container.register(.ObjectGraph) { ServerImp() as Server }
.resolveDependencies { container, server in
container.register { ServerImp() as Server }
.resolvingProperties { container, server in
server.client = resolveClientSync()
}
for _ in 0..<100 {
#if os(Linux)
dispatch_async({ _ in
resolveClientAsync()
return nil
})
#else
queue.addOperationWithBlock(resolveClientAsync)
#endif
queue.addOperation(resolveClientAsync)
}
#if os(Linux)
sleep(1)
#else
queue.waitUntilAllOperationsAreFinished()
#endif
XCTAssertEqual(resolvedClients.count, 100, "Instances should be not reused in different object graphs")
for client in resolvedClients {
@@ -221,5 +151,4 @@ class ThreadSafetyTests: XCTestCase {
}
}
#endif

Some files were not shown because too many files have changed in this diff Show More