AliSoftware / Dip

Simple Swift Dependency container. Use protocols to resolve your dependencies and avoid singletons / sharedInstances!
MIT License
977 stars 72 forks source link

No injections for circular dependencies with type forwarding #132

Closed azatZ closed 8 years ago

azatZ commented 8 years ago

I worked on Dip version for swift 2.3, but after migrating to Swift 3 I have to move to Dip v5.0.2 and my VIPER module assemblies stop working. Circular dependencies doesn't injecting. This is one example of the module assembly.

import Dip

protocol OnboardModuleFactory {
    func OnboardModule() throws -> (OnboardModuleInput, OnboardViewInput)
}

class OnboardModuleAssembly: OnboardModuleFactory {

    let moduleContainer = DependencyContainer() { container in

        let presenterDefinition = container.register(.shared) { OnboardPresenter() }
            .resolvingProperties { container, presenter in

                presenter.router = try container.resolve()
                presenter.view = try container.resolve()
        }

        let routerDefinition = container.register(.shared) { OnboardRouter()}
        let viewDefinintion = container.register(.shared) { OnboardViewController()}
        .resolvingProperties { container, view in
            view.output = try container.resolve()
        }

        // type forwarding
        container.register(presenterDefinition, type: OnboardViewOutput.self)
        container.register(presenterDefinition, type: OnboardModuleInput.self)
        container.register(viewDefinintion, type: OnboardViewInput.self)
        container.register(viewDefinintion, type: OnboardViewController.self)
        container.register(routerDefinition, type: OnboardRouterInput.self)
    }

    func onboardModule() throws -> (OnboardModuleInput, OnboardViewInput) {
        let view = try moduleContainer.resolve() as OnboardViewController

        // view output is nil
        let presenter = view.output as! OnboardPresenter
        return (presenter, view)
    }
}
azatZ commented 8 years ago

Problem is solved. I registered definition for OnboardViewController.self twice and it stop working.