hmlongco / Factory

A new approach to Container-Based Dependency Injection for Swift and SwiftUI.
MIT License
1.78k stars 113 forks source link

modular approach and scopes #214

Closed MartinP7r closed 1 month ago

MartinP7r commented 1 month ago

Hi 👋🏼

When using the modular approach you outlined in your documentation, how would one set a scope when actually registering the dependency inside the main app module?

extension Container: AutoRegistering {
    func autoRegister {
        accountLoader.register { AccountLoader() }
        ...
    }
}

I only found a way to use a default scope in that case, no per-dependency way of doing it.

extension Container: AutoRegistering {
    func autoRegister() {
        manager.defaultScope = .cached
        ...
    }
}
hmlongco commented 1 month ago

Try...

extension Container: AutoRegistering {
    func autoRegister {
        accountLoader
             .register { AccountLoader() }
             .scope(.cached)
        ...
    }
}
MartinP7r commented 1 month ago

thank you for the hint!