-
Notifications
You must be signed in to change notification settings - Fork 72
Added support for up to six runtime arguments #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9e528da
added support to register components with runtime arguments
ilyapuchka fbd4cb8
removed registration with factory that accepts tag
ilyapuchka ecac5ce
made public methods to register/resolve instances with arbitrary numb…
ilyapuchka 85807fe
fixed sample app
ilyapuchka 23924ce
fixed project file
6faf080
renamed factory to factoryType in DefinitionKey
e97e397
added test to demonstrate difference with optional runtime arguments
04aa2e1
`tag` argument is named in all methods
b5fca0a
updated changelog and readme
ae04c11
fixed test for optional runtime arguments
ilyapuchka f1c7c52
updated changelog and readme
2ea5e17
fixed documentation
ilyapuchka 89acac2
Moved Podspec Metadata Group outside the project
AliSoftware b9bf2da
Documentation nitpickings
AliSoftware 72663de
Code Style nitpicking: use 2-spaces indents (see also #9)
AliSoftware File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // | ||
| // Definition.swift | ||
| // Dip | ||
| // | ||
| // Created by Ilya Puchka on 04.11.15. | ||
| // Copyright © 2015 AliSoftware. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| ///Internal representation of a key used to associate definitons and factories by tag, type and factory. | ||
| struct DefinitionKey : Hashable, Equatable, CustomDebugStringConvertible { | ||
| var protocolType: Any.Type | ||
| var factory: Any.Type | ||
| var associatedTag: DependencyContainer.Tag? | ||
|
|
||
| var hashValue: Int { | ||
| return "\(protocolType)-\(factory)-\(associatedTag)".hashValue | ||
| } | ||
|
|
||
| var debugDescription: String { | ||
| return "type: \(protocolType), factory: \(factory), tag: \(associatedTag)" | ||
| } | ||
| } | ||
|
|
||
| func ==(lhs: DefinitionKey, rhs: DefinitionKey) -> Bool { | ||
| return | ||
| lhs.protocolType == rhs.protocolType && | ||
| lhs.factory == rhs.factory && | ||
| lhs.associatedTag == rhs.associatedTag | ||
| } | ||
|
|
||
| ///Describes the lifecycle of instances created by container. | ||
| public enum ComponentScope { | ||
| /// Indicates that new instance of the component will be always created. | ||
| case Prototype | ||
| /// Indicates that resolved component should be retained by container and always reused. | ||
| case Singleton | ||
| } | ||
|
|
||
| ///Definition of type T describes how instances of this type should be created when they are resolved by container. | ||
| public final class DefinitionOf<T>: Definition { | ||
| let factory: Any | ||
| let scope: ComponentScope | ||
|
|
||
| init(factory: Any, scope: ComponentScope = .Prototype) { | ||
| self.factory = factory | ||
| self.scope = scope | ||
| } | ||
|
|
||
| var resolvedInstance: T? { | ||
| get { | ||
| guard scope == .Singleton else { return nil } | ||
| return _resolvedInstance | ||
| } | ||
| set { | ||
| guard scope == .Singleton else { return } | ||
| _resolvedInstance = newValue | ||
| } | ||
| } | ||
|
|
||
| private var _resolvedInstance: T? | ||
| } | ||
|
|
||
| ///Dummy protocol to store definitions for different types in collection | ||
| protocol Definition {} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was confused at first to see that we stored the
factoryclosure in theDefinitionKey. I now understand that in fact you store the factory signature / Type in there, to store the expected runtime argument types.So here we probably need to find a different name for this property, to make it less confusing with the other places when we use the term
factorywhich expect an actual closure, while inDefinitionKeythat's the closure type, not the closure itself.I only understood that when I saw your
let key = DefinitionKey(protocolType: T.self, factory: F.self, associatedTag: tag)here, seeing that you usedF.selfthere.Simply renaming this to
factoryTypeorfactorySignatureor whatnot would make things clearer I think.