How to resolve a circular dependency while still using Dagger2?

后端 未结 3 1485
猫巷女王i
猫巷女王i 2021-02-12 15:29

I have two classes, Foo and Bar, which depend on each other, as well as various other classes. I am using Dagger-2 for dependency injection, b

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-12 16:07

    After an excessive amount of thought and talks with coworkers, we ended up doing the following:

    class Foo extends FooWithoutDep {
        @Inject Foo(Bar bar, OtherDep1 dep1, OtherDep2 dep2) {
            super(dep1, dep2);
            setBarDep(bar);
        }
    }
    
    class FooWithoutDep {
        //Field declarations elided
        @Inject FooWithoutDep(OtherDep1 dep1, OtherDep2 dep2) {
            //Normal constructor stuff
        }
        void setBarDep(Bar bar) { this.bar = bar; }
    
        //The rest of the actual logic
    }
    
    class Bar {
        //Field declarations elided
        @Inject Bar(FooWithoutDep foo, OtherDep3 dep3) {
            this.foo = foo;
            this.foo.setBarDep(this);
            this.dep3 = dep3;
        }
    
        //Code that uses Foo and the other dependencies
    }
    

    Explaining this -- we moved the actual logic of Foo into a parent class (FooWithoutDep), that took the circular dependency as a settable field rather than a constructor parameter. Then the original class just contained a constructor that took the circular dependency and called the setter. The other class, Bar, depended on the parent (FooWithoutDep), and called the setter explicitly, passing itself (this). This enables all the existing references to the class to remain the same, while still using Dagger to inject all the dependencies.

    This seemed confusing enough to be worth writing up here.

提交回复
热议问题