21
votes

What I'm using:

  • Visual Studio 2010
  • Microsoft .NET Framework 4
  • Prism v4

What I am trying to figure out is how to get started with Prism + MEF while maintaining the MVVM pattern. When I go into the Prism Quickstarts, it has a Prism + MEF, but the comments in the project specifically state that the Quickstart example does not implement MVVM. I'm not really sure what to mix/match so that my shell itself follows MVVM (and regions).

Basically, I want to use MEF to be able to load Assemblies (Modules) at run-time. And, I want to setup regions in my Shell and have the Shell use MVVM (so I can databind things to the shell). Every example online is either Prism, Prism + MVVM, Prism + Unity, Silverlight examples, Prism + MEF, etc. But I can not find any WPF Prism + MEF + MVVM examples or information. I really have no idea how to setup my bootstrapping and such to get going.

Once that part is done, I'm sure I'll figure out how to load other controls using MVVM into my shell. Any help would be great, especially resources that deal directly with this situation as apposed to something similar (i.e. Prism + Unity and without MEF). Thanks!

2
I think you'll find all you need here to get you started: Developer's Guide to Microsoft PrismMacNET
Did you ever find an example of MEF+Prism+MVVM? I can't find one either and an example is worth a thousand help file links.JohnC
@JohnC: Well, the Developer's Guide to Prism was helpful, but no, I did not find any concrete examples of Prism + MEF + MVVM. Though, looking at each component individually things made more sense. Prism is a framework that offers utilities and classes that help you write a MVVM application. MEF is a plug-in framework. The reason why Prism and MEF can intertwine is because Prism is extendable. I haven't figured out how to handle Regions (using MEF), but I have found out that the AggregateCatalog is the heart and soul of Prism + MEF.michael
@JohnC: MVVM is a design style, and it naturally goes with Prism. The thing is Prism gives quite a few tools to help handle special MVVM situations (for example, it provides DelagateCommand and has samples on how to create a CompositeCommand). It offers Regions (which I still haven't figured out completely). It offers classes such as the NotificationObject, etc. I did find ways to use MEF to import my ViewModel into my view and export my View into my Bootstrapper. There are quite a few sources out there, just not one BIG example, instead lots of small examples that I pieced together.michael

2 Answers

1
votes

I have never used Prism+MEF myself, but in your question you mention you want to be able to load modules at runtime (with MEF). This is something you don't need to have MEF for, because Prism is quite good at doing that itself. The setup is pretty simple:

First, create a Prism module by implementing Modularity.IModule. It only requires one method: Initialize(). Here you can do any setup needed for your module. I generally also extend the constructor to inject any other interfaces I might need (using Unity).

Then, create a ModuleCatalog to specify the details of the module you created:

<Modularity:ModuleCatalog 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:Modularity="clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism.Composition">
    <Modularity:ModuleInfo Ref="Your.ModuleProject.dll" 
        ModuleType="Your.ModuleProject.Module, Your.ModuleProject" 
        ModuleName="Module1" 
        InitializationMode="OnDemand" />
</Modularity>

The InitializationMode is what you want to set if you need runtime loading. The catalog can be loaded in the Prim bootstrapper:

catalog = Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri("Modules.xaml", UriKind.RelativeOrAbsolute));

Then all you need to do to load up your module, is get a reference to IModuleManager (Dependency Injection, yay!) and load up the module:

if (loadModule1)
    var myModule = moduleManager.LoadModule("Module1");

Now the module is known to Prism. Keep in mind that unloading is not supported by Prism.

1
votes

Everything you asked seems to be present on the samples that come installed with Prism, you just looked at the wrong ones.

Check out the StockTrader RI folder, that is described here.

It has a fairly complete sample for real-life applications, with some of complex scenarios and is implemented with Prism (obviously), MVVM and MEF.

Edit: Even though the link I provided is for Prism 5, the sample was also present on Prism 4.1. In that version, the documentation was not available online (at least as far as I remember) , but was instead offered on a .chm file installed with the Prism source code + samples. Don't know about v4.0, though.