Circular Dependency MEF

Suppose we have two components that depend on each other (indirectly, via interfaces):

public interface IAlice { ... }
public interface IBob { ... }

[Export(typeof(IAlice)), PartCreationPolicy(CreationPolicy.NonShared)]
class Alice : IAlice
{
    [Import]
    private IBob Bob { get; set; }
    ...
}

// could be defined in some assembly developed by another team
[Export(typeof(IBob)), PartCreationPolicy(CreationPolicy.NonShared)]
class Bob : IBob
{
    [Import]
    private IAlice Alice { get; set; }
    ...
}

      

This causes the composition to fail: the composition failed because it did not complete within 100 reps. This is most likely caused by a loop in the dependency graph of the part that is marked with a non-shared policy.

I know I can import via a Lazy declaration, but this way the import will be created on demand, while I need my Alice links Bob and Bob to reference the same Alice instance. I expected the CompositionContainer to allow such loops within a single composition operation, but apparently this is not the case.

I cannot declare the parts as shared, since I do not need a single Alice and Bob per container. I just need such dependency plots to be created as a whole when done in a single composition operation. Is there a workaround?

UPD. Added some clarification.

+3


source to share


1 answer


Why doesn't Alice import the IBob via ctor injection and then explicitly set the Alice property for Bob (i.e. Bob shouldn't use property injection for Alice)?



+3


source







All Articles