Skip to content
This repository has been archived by the owner on Oct 13, 2022. It is now read-only.

When should a missing @Inject dep crash? #8

Open
sureshjoshi opened this issue Feb 20, 2020 · 1 comment
Open

When should a missing @Inject dep crash? #8

sureshjoshi opened this issue Feb 20, 2020 · 1 comment

Comments

@sureshjoshi
Copy link

Using a creation pattern of:

private static let dependencies = Dependencies {
        Module { AccountStore.shared as AccountDataSource }
        Module { RealmStore.shared as RealmStore }
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        Self.dependencies.build()
        ...

I expected that as soon as a view model with an @Inject that is not in the dependencies would crash. In reality, it crashes on usage of that Inject (which was pretty surprising).

final class BrokenViewModel {
    @Inject var accountStore: AccountDataStore
    @Inject var realmStore: RealmStore
    @Inject var otherStore: OtherStore
    
    init() {
        // Use accountStore and realmStore - no problemo
    }
    
    func foo() {
        otherStore.bar() // CRASH
    }

I would have assumed on instantiation of BrokenViewModel that it would crash because the Injection couldn't be resolved?

@basememara
Copy link
Owner

Right, it would crash on first use of the properties. I've been thinking of having a build process you'd put in a phase that would run the entire container to check and give an Xcode error if one of the dependencies are missing.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants