Closed murad1981 closed 3 years ago
You can resolve anything inside of a registration. That's how constructor injection works. In your example, however....
extension Resolver: ResolverRegistering {
public static func registerAllServices() {
register { AppConfig() }
register(name: "host") { resolve(AppConfig.self).host }
}
}
No need for the String.self, as Resolver will infer the return type from the factory result.
I would probably go the extra mile here with name spaces...
extension Resolver.Name {
static let host = Self("host")
}
extension Resolver: ResolverRegistering {
public static func registerAllServices() {
register { AppConfig() }
register(name: .host) { resolve(AppConfig.self).host }
}
}
struct SomeModel {
@Injected(name: .host) var host: String
}
Name spaces help ensure consistency and minimizes the chance for errors, like attempting to do...
struct SomeModel {
@Injected(name: "Host") var host: String
}
@hmlongco Thank you for the explanation, I liked the idea of name spaces too ...
However, if the above setup uses an enum for the host instead of String
the app will crash with the error:
Fatal error: RESOLVER: 'String:host' not resolved. To disambiguate optionals use resolver.optional()
enum BaseUrl: String {
case staging = "staging.com"
case production = "prod.com"
}
struct AppConfig {
var host: BaseUrl = .staging
}
extension Resolver.Name {
static let host = Self("host")
}
extension Resolver: ResolverRegistering {
public static func registerAllServices() {
register {
AppConfig()
}
register(name: .host) {
resolve(AppConfig.self).host
}
}
}
I struggled to solve the error but to no avail unfortunately ..
the following is a ContentView
in case you want to test it:
struct ContentView: View {
@Injected(name: .host) var host: String
var body: some View {
Text("Hello, world!")
.padding()
.onTapGesture {
print("host = \(host)")
}
}
}
Ummm.... isn't the type now BaseUrl and not String?
@Injected(name: .host) var host: BaseUrl
That should work, though I've never tried injecting an enumeration before.
yeah totally ... sorry .. it was a typo though ...
Is there a way to read a value from a registered service inside
registerAllServices
, in the following example: