Add DIContainer singleton with key path registration

This commit is contained in:
Brendan Szymanski 2026-06-10 17:04:11 -04:00
parent cc5c836f04
commit e829b8141b

View file

@ -0,0 +1,34 @@
import Foundation
@MainActor
public final class DIContainer {
public static let shared = DIContainer()
public private(set) var values = InjectionValues()
private init() {}
public func register<T>(
_ keyPath: WritableKeyPath<InjectionValues, T?>,
value: T
) {
values[keyPath: keyPath] = value
}
public func resolve<T>(
_ keyPath: KeyPath<InjectionValues, T?>
) -> T {
guard let value = values[keyPath: keyPath] else {
fatalError(
"DIContainer: No value registered for \(keyPath). "
+ "Call DIContainer.shared.register(\\.key, value:) during app startup."
)
}
return value
}
public func reset() {
values = InjectionValues()
}
}