Cyclic dependencies

29 views
Skip to first unread message

Viacheslav Vdovenko

unread,
Aug 28, 2015, 4:55:47 AM8/28/15
to Transfuse
Hi All,

I'm testing the possibility of cyclic dependencies for transfuse.
Unfortunately my project has a lot of them. And it works quite well with dagger v1.
I've created a set of classes and looks like app not able to resolve that. It hangs on starting.
My classes:

public class Example {
}

@Singleton
public class MainObject {

    public final Example mExample;
    public final Provider<ComplexObject> mComplexObjectProvider;

    @Inject
    public MainObject(Example example, Provider<ComplexObject> complexObjectProvider) {
        mExample = example;
        mComplexObjectProvider = complexObjectProvider;
    }

    @Override
    public String toString() {
        return super.toString() + ", mExample: " + mExample + ", mComplexObject: " + mComplexObjectProvider.get();
    }
}

@Singleton
public class ComplexObject {

    public final Provider<MainObject> mMainObjectProvider;

    @Inject
    public ComplexObject(Provider<MainObject> mainObjectProvider) {
        mMainObjectProvider = mainObjectProvider;
    }

    @Override
    public String toString() {
        return super.toString() + ", mMainObject: " + mMainObjectProvider.get();
    }
}

And finally in activity I do print all injected members:
@org.androidtransfuse.annotations.Activity
@Layout(R.layout.activity_main)
public class Main {

    @Inject
    Example mExample;

    @Inject
    MainObject mMainObject;

    @Inject
    ComplexObject mComplexObject;

    @OnCreate
    public void doSomethingElse(){
        Log.d("MainActivity", "mExample: " + mExample);
        Log.d("MainActivity", "mMainObject: " + mMainObject);
        Log.d("MainActivity", "mComplexObject: " + mComplexObject);
    }
}

But in log cat I could observe only log line from mExample

Am I doing something wrong?

Regards,

John Ericksen

unread,
Aug 28, 2015, 3:03:55 PM8/28/15
to Transfuse
Hi Viacheslav,

From my understanding of Dagger, it purposefully does not allow for cyclic dependencies.  I see you've broken the cyclic dependencies in your example with Providers, which should work just fine in both Dagger and Transfuse.  Transfuse specifically breaks cyclic dependencies using a virtual proxy which requires you to inject the object via the interface.  There is the future possibility to break cyclic dependencies by field or method injection as well.

Looking through your example are you sure it's not logging the last two log debug statements?

John

John Ericksen

unread,
Aug 28, 2015, 7:26:04 PM8/28/15
to Transfuse
I was curious so I stood up your example.  I'm getting a StackOverflowError exception from the toString() because of the association between ComplexObject referencing MainObject referencing back ComplexObject.

Viacheslav Vdovenko

unread,
Sep 16, 2015, 11:26:20 AM9/16/15
to Transfuse
Hi John,
Unfortunately yes there are no log out from last two lines.
I'm looking forward to switch a quite complex project from dagger 1.x. It's not possible to break anyhow cyclic dependency in dagger 2.x.

Sorry for delay in answer, haven't got notification update.
Regards,

Viacheslav Vdovenko

unread,
Sep 16, 2015, 11:28:52 AM9/16/15
to Transfuse
Not only me looking for cyclic dependency.
That is discussion thread for dagger 2.x:
https://groups.google.com/forum/#!topic/dagger-discuss/KRDGFD3rUGQ

John Ericksen

unread,
Sep 16, 2015, 12:54:06 PM9/16/15
to Transfuse
Viacheslav,

If you'd like I could push up a working version of your example with a full example of Transfuse supporting cyclic dependencies.  Would that help?

John

Viacheslav Vdovenko

unread,
Sep 17, 2015, 3:52:14 AM9/17/15
to Transfuse
I do believe that will help :)

John Ericksen

unread,
Sep 17, 2015, 9:12:39 AM9/17/15
to Transfuse
Viacheslav,


I broke the cyclic dependencies both by using the Provider (Not really a cycle) and an interface, which Transfuse generates a proxy for.   Let me know if you have any questions.

John

Viacheslav Vdovenko

unread,
Sep 21, 2015, 3:39:03 AM9/21/15
to Transfuse
Thanks John for the quick replay,
I'll have a look...
Reply all
Reply to author
Forward
0 new messages