Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Dip/Dip.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
0945269C1BEA1CFF0034E72A /* Dip.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 094526911BEA1CFF0034E72A /* Dip.framework */; };
094526A11BEA1CFF0034E72A /* DipTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 094526A01BEA1CFF0034E72A /* DipTests.swift */; };
094526AC1BEA1D200034E72A /* Dip.swift in Sources */ = {isa = PBXBuildFile; fileRef = 094526AB1BEA1D200034E72A /* Dip.swift */; };
094526B41BEA51540034E72A /* RuntimeArguments.swift in Sources */ = {isa = PBXBuildFile; fileRef = 094526B31BEA51540034E72A /* RuntimeArguments.swift */; };
094526B61BEA520B0034E72A /* Definition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 094526B51BEA520B0034E72A /* Definition.swift */; };
094526B81BEA536A0034E72A /* RuntimeArgumentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 094526B71BEA536A0034E72A /* RuntimeArgumentsTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -35,6 +38,9 @@
094526B01BEA1E1C0034E72A /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
094526B11BEA1E1C0034E72A /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; name = CHANGELOG.md; path = ../CHANGELOG.md; sourceTree = "<group>"; };
094526B21BEA1E1C0034E72A /* LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
094526B31BEA51540034E72A /* RuntimeArguments.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RuntimeArguments.swift; sourceTree = "<group>"; };
094526B51BEA520B0034E72A /* Definition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Definition.swift; sourceTree = "<group>"; };
094526B71BEA536A0034E72A /* RuntimeArgumentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RuntimeArgumentsTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -80,6 +86,8 @@
children = (
094526941BEA1CFF0034E72A /* Dip.h */,
094526AB1BEA1D200034E72A /* Dip.swift */,
094526B31BEA51540034E72A /* RuntimeArguments.swift */,
094526B51BEA520B0034E72A /* Definition.swift */,
094526961BEA1CFF0034E72A /* Info.plist */,
);
path = Dip;
Expand All @@ -89,6 +97,7 @@
isa = PBXGroup;
children = (
094526A01BEA1CFF0034E72A /* DipTests.swift */,
094526B71BEA536A0034E72A /* RuntimeArgumentsTests.swift */,
094526A21BEA1CFF0034E72A /* Info.plist */,
);
path = DipTests;
Expand Down Expand Up @@ -214,6 +223,8 @@
buildActionMask = 2147483647;
files = (
094526AC1BEA1D200034E72A /* Dip.swift in Sources */,
094526B61BEA520B0034E72A /* Definition.swift in Sources */,
094526B41BEA51540034E72A /* RuntimeArguments.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -222,6 +233,7 @@
buildActionMask = 2147483647;
files = (
094526A11BEA1CFF0034E72A /* DipTests.swift in Sources */,
094526B81BEA536A0034E72A /* RuntimeArgumentsTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
66 changes: 66 additions & 0 deletions Dip/Dip/Definition.swift
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
Copy link
Owner

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 factory closure in the DefinitionKey. 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 factory which expect an actual closure, while in DefinitionKey that'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 used F.self there.

Simply renaming this to factoryType or factorySignature or whatnot would make things clearer I think.

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 {}
Loading