Nested import

Jan 18, 2012 at 1:00 AM
Edited Jan 18, 2012 at 1:01 AM
[Export(typeof(IA))]
public class A : IA {}


// In a different assembly

[Export(typeof(IB))]
public class B : IB, IPartImportsSatisfiedNotification
{
     [ImportMany(typeof(IA))]
     public IEnumerable<IA> As { get; set; }

     public void OnImportsSatisfied()
     {
           // put this object in "ready" state
     }
}


// In yet another assembly

[Export(typeof(IC))]
public class C : IC, IPartImportsSatisfiedNotification
{
     [ImportMany(typeof(IB))]
     public IEnumerable<IB> Bs { get; set; }

     public void OnImportsSatisfied()
     {
           // put this object in "ready" state
     }
}


// in main process... 

var c = new C();

I'm assuming that each of the imported objects at each level are imported, assuming all is well (the proper assemblies are available and the deployment catalogs are implemented correctly)


Is this correct? What I'm asking is whether or not you need to do all imports "flatly" and then assemble them using injection, orwhether MEF imports can nest.


If they can and do nest, how deep can it go (on what does that depend) and what are the typical "gotchas" in these scenarios (perhaps some inner nested imports won't finish before outer imports?)

Thanks,

Chad

Jan 18, 2012 at 2:09 AM

Follow up:

It appears nesting like that works, and the IPartImportsSatisfiedNotification method (in the host) seems to fire when the outermost imports are complete.

Maybe this doesn't work when using Lazy<> in conjunction with ImportMany and IPartImportsSatisfiedNotification.... for example, C lazily imports many B, which lazily imports many A.   B and C both implement IPartImportsSatisfiedNotification... 

...if host process creates a C, I'm wondering if all "ImportsSatisfied" methods get called in the correct order (innermost to outermost)

I just don't want to build an architecture that leans heavily on that behavior if it doesn't work.

 

 

Thanks again,

Chad

Jan 18, 2012 at 3:16 PM

Hi Chad,

I don’t believe that ordering is guaranteed for calls to OnImportsSatisfied(). Because MEF supports graphs with cycles in them, sometimes there is no clear ‘innermost’ or ‘outermost’ part. So, I would not take a dependency on this behavior.

Regards,

Nick

From: chadderack [email removed]
Sent: Tuesday, January 17, 2012 7:10 PM
To: Nicholas Blumhardt
Subject: Re: Nested import [MEF:286491]

From: chadderack

Follow up:

It appears nesting like that works, and the IPartImportsSatisfiedNotification method (in the host) seems to fire when the outermost imports are complete.

Maybe this doesn't work when using Lazy<> in conjunction with ImportMany and IPartImportsSatisfiedNotification.... for example, C lazily imports many B, which lazily imports many A. B and C both implement IPartImportsSatisfiedNotification...

...if host process creates a C, I'm wondering if all "ImportsSatisfied" methods get called in the correct order (innermost to outermost)

I just don't want to build an architecture that leans heavily on that behavior if it doesn't work.

Thanks again,

Chad