Skip to content

Commit 3ccbff4

Browse files
committed
Merge pull request #8 from AliSoftware/feature/runtime-args
Added support for up to six runtime arguments
2 parents ec3d979 + 72663de commit 3ccbff4

File tree

19 files changed

+781
-315
lines changed

19 files changed

+781
-315
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## Develop
4+
* Added support for factories with up to six runtime arguments
5+
[#8](https://github.com/AliSoftware/Dip/pull/8), [@ilyapuchka](https://github.com/ilyapuchka)
6+
* Parameter `tag` is now named in all register/resolve methods
7+
38
## 2.0.0
49

510
* Moved from generic _tag_ parameter on container to `Tag` enum with `String` and `Int` cases

Dip/Dip.xcodeproj/project.pbxproj

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
0945269C1BEA1CFF0034E72A /* Dip.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 094526911BEA1CFF0034E72A /* Dip.framework */; };
1212
094526A11BEA1CFF0034E72A /* DipTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 094526A01BEA1CFF0034E72A /* DipTests.swift */; };
1313
094526AC1BEA1D200034E72A /* Dip.swift in Sources */ = {isa = PBXBuildFile; fileRef = 094526AB1BEA1D200034E72A /* Dip.swift */; };
14+
094526B41BEA51540034E72A /* RuntimeArguments.swift in Sources */ = {isa = PBXBuildFile; fileRef = 094526B31BEA51540034E72A /* RuntimeArguments.swift */; };
15+
094526B61BEA520B0034E72A /* Definition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 094526B51BEA520B0034E72A /* Definition.swift */; };
16+
094526B81BEA536A0034E72A /* RuntimeArgumentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 094526B71BEA536A0034E72A /* RuntimeArgumentsTests.swift */; };
17+
09969C551BEB7C0A00F93C70 /* Dip.podspec in Resources */ = {isa = PBXBuildFile; fileRef = 09969C541BEB7C0A00F93C70 /* Dip.podspec */; };
1418
/* End PBXBuildFile section */
1519

1620
/* Begin PBXContainerItemProxy section */
@@ -31,10 +35,13 @@
3135
094526A01BEA1CFF0034E72A /* DipTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DipTests.swift; sourceTree = "<group>"; };
3236
094526A21BEA1CFF0034E72A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3337
094526AB1BEA1D200034E72A /* Dip.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Dip.swift; sourceTree = "<group>"; };
34-
094526AE1BEA1D5E0034E72A /* Dip.podspec */ = {isa = PBXFileReference; lastKnownFileType = text; name = Dip.podspec; path = /Users/ilya/Documents/Developer/iPhoneProjects/Dip/Dip.podspec; sourceTree = "<absolute>"; };
3538
094526B01BEA1E1C0034E72A /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
3639
094526B11BEA1E1C0034E72A /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; name = CHANGELOG.md; path = ../CHANGELOG.md; sourceTree = "<group>"; };
3740
094526B21BEA1E1C0034E72A /* LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
41+
094526B31BEA51540034E72A /* RuntimeArguments.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = RuntimeArguments.swift; sourceTree = "<group>"; tabWidth = 2; };
42+
094526B51BEA520B0034E72A /* Definition.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = Definition.swift; sourceTree = "<group>"; tabWidth = 2; };
43+
094526B71BEA536A0034E72A /* RuntimeArgumentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RuntimeArgumentsTests.swift; sourceTree = "<group>"; };
44+
09969C541BEB7C0A00F93C70 /* Dip.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = Dip.podspec; path = ../Dip.podspec; sourceTree = "<group>"; };
3845
/* End PBXFileReference section */
3946

4047
/* Begin PBXFrameworksBuildPhase section */
@@ -64,7 +71,9 @@
6471
0945269F1BEA1CFF0034E72A /* DipTests */,
6572
094526921BEA1CFF0034E72A /* Products */,
6673
);
74+
indentWidth = 2;
6775
sourceTree = "<group>";
76+
tabWidth = 2;
6877
};
6978
094526921BEA1CFF0034E72A /* Products */ = {
7079
isa = PBXGroup;
@@ -80,6 +89,8 @@
8089
children = (
8190
094526941BEA1CFF0034E72A /* Dip.h */,
8291
094526AB1BEA1D200034E72A /* Dip.swift */,
92+
094526B31BEA51540034E72A /* RuntimeArguments.swift */,
93+
094526B51BEA520B0034E72A /* Definition.swift */,
8394
094526961BEA1CFF0034E72A /* Info.plist */,
8495
);
8596
path = Dip;
@@ -89,6 +100,7 @@
89100
isa = PBXGroup;
90101
children = (
91102
094526A01BEA1CFF0034E72A /* DipTests.swift */,
103+
094526B71BEA536A0034E72A /* RuntimeArgumentsTests.swift */,
92104
094526A21BEA1CFF0034E72A /* Info.plist */,
93105
);
94106
path = DipTests;
@@ -97,7 +109,7 @@
97109
094526AF1BEA1E0B0034E72A /* Podspec Metadata */ = {
98110
isa = PBXGroup;
99111
children = (
100-
094526AE1BEA1D5E0034E72A /* Dip.podspec */,
112+
09969C541BEB7C0A00F93C70 /* Dip.podspec */,
101113
094526B01BEA1E1C0034E72A /* README.md */,
102114
094526B11BEA1E1C0034E72A /* CHANGELOG.md */,
103115
094526B21BEA1E1C0034E72A /* LICENSE */,
@@ -196,6 +208,7 @@
196208
isa = PBXResourcesBuildPhase;
197209
buildActionMask = 2147483647;
198210
files = (
211+
09969C551BEB7C0A00F93C70 /* Dip.podspec in Resources */,
199212
);
200213
runOnlyForDeploymentPostprocessing = 0;
201214
};
@@ -214,6 +227,8 @@
214227
buildActionMask = 2147483647;
215228
files = (
216229
094526AC1BEA1D200034E72A /* Dip.swift in Sources */,
230+
094526B61BEA520B0034E72A /* Definition.swift in Sources */,
231+
094526B41BEA51540034E72A /* RuntimeArguments.swift in Sources */,
217232
);
218233
runOnlyForDeploymentPostprocessing = 0;
219234
};
@@ -222,6 +237,7 @@
222237
buildActionMask = 2147483647;
223238
files = (
224239
094526A11BEA1CFF0034E72A /* DipTests.swift in Sources */,
240+
094526B81BEA536A0034E72A /* RuntimeArgumentsTests.swift in Sources */,
225241
);
226242
runOnlyForDeploymentPostprocessing = 0;
227243
};

Dip/Dip/Definition.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// Definition.swift
3+
// Dip
4+
//
5+
// Created by Ilya Puchka on 04.11.15.
6+
// Copyright © 2015 AliSoftware. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
///Internal representation of a key used to associate definitons and factories by tag, type and factory.
12+
struct DefinitionKey : Hashable, Equatable, CustomDebugStringConvertible {
13+
var protocolType: Any.Type
14+
var factoryType: Any.Type
15+
var associatedTag: DependencyContainer.Tag?
16+
17+
var hashValue: Int {
18+
return "\(protocolType)-\(factoryType)-\(associatedTag)".hashValue
19+
}
20+
21+
var debugDescription: String {
22+
return "type: \(protocolType), factory: \(factoryType), tag: \(associatedTag)"
23+
}
24+
}
25+
26+
func ==(lhs: DefinitionKey, rhs: DefinitionKey) -> Bool {
27+
return
28+
lhs.protocolType == rhs.protocolType &&
29+
lhs.factoryType == rhs.factoryType &&
30+
lhs.associatedTag == rhs.associatedTag
31+
}
32+
33+
///Describes the lifecycle of instances created by container.
34+
public enum ComponentScope {
35+
/// Indicates that a new instance of the component will be created each time it's resolved.
36+
case Prototype
37+
/// Indicates that resolved component should be retained by container and always reused.
38+
case Singleton
39+
}
40+
41+
///Definition of type T describes how instances of this type should be created when they are resolved by container.
42+
public final class DefinitionOf<T>: Definition {
43+
let factory: Any
44+
let scope: ComponentScope
45+
46+
init(factory: Any, scope: ComponentScope = .Prototype) {
47+
self.factory = factory
48+
self.scope = scope
49+
}
50+
51+
var resolvedInstance: T? {
52+
get {
53+
guard scope == .Singleton else { return nil }
54+
return _resolvedInstance
55+
}
56+
set {
57+
guard scope == .Singleton else { return }
58+
_resolvedInstance = newValue
59+
}
60+
}
61+
62+
private var _resolvedInstance: T?
63+
}
64+
65+
///Dummy protocol to store definitions for different types in collection
66+
protocol Definition {}

0 commit comments

Comments
 (0)