square / Cleanse

Lightweight Swift Dependency Injection Framework
Other
1.79k stars 90 forks source link

Dependencies between Modules #37

Closed Lukas-Stuehrk closed 7 years ago

Lukas-Stuehrk commented 7 years ago

When you have two modules and both modules depend on another module, Cleanse will crash because the bindings for the third module are already bound.

I am using the version of the swift-3 branch which uses the static configuration of #27.

Minimal example:

class Foobar: RootComponent {

    public static func configure<B : Binder>(binder: B) {

        binder.install(module: Module1.self)
        binder.install(module: Module2.self)
        binder
            .bind(UIWindow.self)
            .to {
                return UIWindow()
            }
    }

    typealias Root = UIWindow

}

struct Module1: Module {
    public static func configure<B : Binder>(binder: B) {
        binder.install(module: NetworkingModule.self)
    }

}

struct Module2: Module {
    /// This is where configuring providers occurs.
    public static func configure<B : Binder>(binder: B) {
        binder.install(module: NetworkingModule.self)
    }
}

struct NetworkingModule: Module {
    /// This is where configuring providers occurs.
    public static func configure<B : Binder>(binder: B) {
        binder.bind(URLSession.self).to(value: URLSession.shared)
    }
}

This will fail with the following error:

fatal error: Already bound at NSURLSession: file /tmp/CleanseTests/Pods/Cleanse/Cleanse/Graph.swift, line 114

Is there another way of using a module setup where the dependencies are like in my example? In my opinion it is a very common structure for applications where you have small, interchangeable components. As far as I know this is possible with dagger2 and the @Module(includes=) annotation.

mikelikespie commented 7 years ago

Thanks for the report! This is something I'd like to resolve soon. Since the "static-configure" branch its actually a lot easier now since one doesn't have to instantiate a module. I'll look into this soon I think

mikelikespie commented 7 years ago

@Lukas-Stuehrk Added a fix 5989ef8cbab005cf00ec67370ad02ed0e490b87a as part of this pr https://github.com/square/Cleanse/pull/39

mikelikespie commented 7 years ago

(need to port it to swift 3 branch though still)

mikelikespie commented 7 years ago

ok, its on the swift-3 branch now