From fb5de75e619b6527427b82a543e6a110c9b117d3 Mon Sep 17 00:00:00 2001 From: Nick Cipollo Date: Mon, 4 Dec 2023 11:27:28 -0500 Subject: [PATCH] Small improvements and updates: - Add final to classes which should not be overridden. - Better error loggin when dependency is missing - Tweaks to comments --- Sources/WhoopDIKit/DependencyDefinition.swift | 4 ++-- Sources/WhoopDIKit/ModuleList.swift | 2 +- Sources/WhoopDIKit/ServiceDictionary.swift | 2 +- Sources/WhoopDIKit/WhoopDI.swift | 8 ++++++-- Sources/WhoopDIKit/WhoopDIValidator.swift | 2 +- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Sources/WhoopDIKit/DependencyDefinition.swift b/Sources/WhoopDIKit/DependencyDefinition.swift index a7c6cbf..c908003 100644 --- a/Sources/WhoopDIKit/DependencyDefinition.swift +++ b/Sources/WhoopDIKit/DependencyDefinition.swift @@ -15,7 +15,7 @@ fileprivate extension DependencyDefinition { } /// Provides the definition of an object factory. A fresh version of this dependency will be provide each time one is requested. -class FactoryDefinition: DependencyDefinition { +final class FactoryDefinition: DependencyDefinition { private let factory: (Any?) throws -> Any let serviceKey: ServiceKey @@ -30,7 +30,7 @@ class FactoryDefinition: DependencyDefinition { } /// Provides the definition of a singleton object (i.e an object we will only create once per graph). -class SingletonDefinition: DependencyDefinition { +final class SingletonDefinition: DependencyDefinition { private let factory: (Any?) throws -> Any private let lock = NSLock() let serviceKey: ServiceKey diff --git a/Sources/WhoopDIKit/ModuleList.swift b/Sources/WhoopDIKit/ModuleList.swift index 6b2e444..1e238f6 100644 --- a/Sources/WhoopDIKit/ModuleList.swift +++ b/Sources/WhoopDIKit/ModuleList.swift @@ -2,6 +2,6 @@ public protocol ModuleList { var modules: [DependencyModule] { get } } -public class EmptyModuleList: ModuleList { +public final class EmptyModuleList: ModuleList { public var modules: [DependencyModule] { [] } } diff --git a/Sources/WhoopDIKit/ServiceDictionary.swift b/Sources/WhoopDIKit/ServiceDictionary.swift index 3410940..9d55e67 100644 --- a/Sources/WhoopDIKit/ServiceDictionary.swift +++ b/Sources/WhoopDIKit/ServiceDictionary.swift @@ -1,4 +1,4 @@ -public class ServiceDictionary { +public final class ServiceDictionary { private var valuesByType: [AnyHashable: Value] convenience public init() { diff --git a/Sources/WhoopDIKit/WhoopDI.swift b/Sources/WhoopDIKit/WhoopDI.swift index 0e41273..d564b55 100644 --- a/Sources/WhoopDIKit/WhoopDI.swift +++ b/Sources/WhoopDIKit/WhoopDI.swift @@ -1,4 +1,5 @@ -public class WhoopDI: DependencyRegister { +import Foundation +public final class WhoopDI: DependencyRegister { private static let serviceDict = ServiceDictionary() private static var localServiceDict: ServiceDictionary? = nil @@ -19,6 +20,8 @@ public class WhoopDI: DependencyRegister { do { return try get(name, params) } catch { + print("Inject failed with stack trace:") + Thread.callStackSymbols.forEach { print($0) } fatalError("WhoopDI inject failed with error: \(error)") } } @@ -52,7 +55,8 @@ public class WhoopDI: DependencyRegister { fatalError("Nesting WhoopDI.inject with local definitions is not currently supported") } // We need to maintain a reference to the local service dictionary because transient dependencies may also - // need to references dependencies from it. + // need to reference dependencies from it. + // ---- // This is a little dangerous since we are mutating a static variable but it should be fine as long as you // don't use `inject { }` within the scope of another `inject { }`. let serviceDict = ServiceDictionary() diff --git a/Sources/WhoopDIKit/WhoopDIValidator.swift b/Sources/WhoopDIKit/WhoopDIValidator.swift index f3c80ad..3abce74 100644 --- a/Sources/WhoopDIKit/WhoopDIValidator.swift +++ b/Sources/WhoopDIKit/WhoopDIValidator.swift @@ -1,5 +1,5 @@ /// Provides verification that the object graph is complete. This is intended to be called from within a test. -public class WhoopDIValidator { +public final class WhoopDIValidator { private let paramsDict = ServiceDictionary() public init() { }