Created
March 27, 2024 06:32
-
-
Save hishd/9e64afb70cb095167785dac91c0a4d75 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// DependencyInjector.swift | |
// SkyWizard | |
// | |
// Created by Hishara Dilshan on 2023-10-10. | |
// | |
import Foundation | |
protocol Injectable {} | |
@propertyWrapper | |
/// Usage: @Inject var propertyName: Type (that conforms to Injectable) | |
struct Inject<T: Injectable> { | |
var wrappedValue: T | |
init() { | |
self.wrappedValue = Resolver.shared.resolve() | |
} | |
} | |
class Resolver { | |
static let shared = Resolver() | |
private var storage: [String: Injectable] = [:] | |
private init() {} | |
func add<T: Injectable>(injectable: T) { | |
let key = String(reflecting: injectable) | |
storage[key] = injectable | |
} | |
func resolve<T: Injectable>() -> T { | |
let key = String(reflecting: T.self) | |
guard let injectable = storage[key] as? T else { | |
fatalError("Injectable not found on storage") | |
} | |
return injectable | |
} | |
} | |
class DependencyManager { | |
//let dependency: Type (conforms to Injectable) | |
init() { | |
//Resolver.shared.add(dependency) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment