Policy Injection (PIAB) integration with Prism MEF

Topics: Prism v4 - Silverlight 4, Prism v4 - WPF 4
Aug 31, 2010 at 2:30 PM

Hi all,

I want to achieve some exception policies assembly (which are Prism modules) wrapping.

Is it possible to integrate PIAB with MEF (Prism)? without using some kind of factory?

 

Sep 13, 2010 at 5:34 PM

Hello,

Policy Injection Application Block (PIAB) doesn't have any MEF export attributes on it.  There are several easy (although tedious) techniques to get them into the MEF composition container.

1.  For each PIAB class, derive a class and apply the [Export] attribute, passing typeof either the interface to export or the PIAB concrete type name.  Prism v4 uses this technique in the Prism.MefExtensions component.  Once you have all the type exports in a component, then during bootstrapping add that component via AssemblyCatalog to the MefBootstrapper's AggregateCatalog.

2. If #1 doesn't work because the PIAB class is sealed.  You can use a similar technique, but instead of deriving, you export the PIAB class as a property.  Glenn Block has a blog post about these MEF "buddy classes".  In the get accessor, you simply return a singleton or new up another instance.  You have to make sure to compose the buddy class into the container so that it's exports are available.

3. During the bootstrapping process, you can use the composition container and call ComposeExportedValue repeatedly for each type in PIAB you want in the container.  This isn't recommended though as it is harder to debug and static analysis tools won't be able to see this code.

4. As a last resort, you could apply the MEF export attribute to each of the PIAB classes and recompile the application block.

Gvie #1 a try and let me know what issues you run into. 

Geoff Cox
Southworks

Sep 15, 2010 at 6:52 AM

Thank you so much

but as i learned MEF and PIAB are not meant to go hand by hand, Therefor i dropped the issue. Nevertheless, your solutions are very elegant and cover all possible situations IMHO and im sure this could be a great benefit since this is certainly the way to go.

 

one thing i still dont understand. where do you apply the PolicyInjection.Wrap<>() method on the import/export Prism modules instantiation (without using somekind of factory)?

Thank you.

Sep 15, 2010 at 3:43 PM

You're welcome :-)

MEF provides some limited support for generics and you may indeed require a factory or other intermediate class in some cases.  The [InheritedExport] can work well in some cases to allow the concrete types to export the correct interface.

Geoff Cox
Southworks