MEF and Prism exploration, MEF module loading

About a month ago I posted some ideas around MEF and Prism together. As part of the post, I presented 2 high-level scenarios for MEF/Prism integration (and there are likely more)

  • Use Prism with MEF as an add-on for extensibility. That is make modules extensible through MEF.  You can think of this as the “Two worlds in harmony” solution. Your core architecture rests on Prism’s modularity / and an IoC Container and sits side by side with MEF.
  • Use MEF primarily for modularity, and pull in Prism bits for UI composition.  In this model, your core architecture sits on top of MEF, with MEF replacing the services of your IoC.
  • Recently Jeremy Likeness of Wintillect started exploring the first approach. He’s started a series of blog posts on his explorations down that path. If you check the series you’ll see how he is introducing a new type of module called a PartModule that then uses MEF. The approach he takes is nice because it uses all of the existing Prism module loading infrastructure including Unity, and then adds on that when the module is received, it is registered through MEF. He also leverages MEF’s recomposition capabilities such that when new things are received, they automatically show up as recomposable parts are recomposed.

    Jeremy has 3 posts in the series so far, the content is nice and it includes all the code.

  • PRISM, MEF, and MVVM Part 1 of 3: Unity Glue
  • PRISM, MEF, and MVVM Part 2 of 3: Making PRISM MEF Friendly
  • PRISM, MEF, and MVVM Part 3 of 3: Dynamic MEF Modules in PRISM
  • This is just one of what I am sure will be many explorations to come!

    This entry was posted in MEF, prism, silverlight. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
    • Fallon Massey

      Hi Glenn, that’s exactly what I needed.


    • Glenn Block

      Hi Fallon

      It sounds like what you are asking is how to expose existing singletons through MEF such as the Application class, though I am not sure

      There’s several ways to do that, the most common is to use MEF’s property export support. Instead of a type that has an export, you create a class and drop an export on the property. Then in the getter you implement the logic to retrieve the instance. MEF calls your getter and exports it.

      For example:

      public class ApplicationPart {

      public Application ApplicationExport {
      get{return Application.Current;}

      Importers of Application will now get Application.Current as long as ApplicationPart is in the catalog.

      Does this work for you?

    • Fallon Massey

      Hi Glenn, I have a question. There is an instance of a type that exists, and there can only be one, and I don’t instantiate it, I just get it.

      In Prism, I can add that instance to the container, so my question is this. Is that possible in MEF, and if so, how do you accomplish it.