Wednesday, December 22, 2010

Configuring an MEF Container for Silverlight Prism4 applications

Prism supports modular application development by allowing you create a collection of modules that contains related components or features including user interface and business logic that can be developed independently and later integrated into the application. This feature of Prism makes is easier for develop, test, deploy and extend your application.
The module loading process in Prism includes the following:
1.       Registering/discovering modules. The modules to be loaded at run-time for a particular application are defined in a Module catalog. The catalog contains information about the modules to be loaded, their location, and the order in which they are to be loaded.
2.       Loading modules. The assemblies that contain the modules are loaded into memory. This phase may require the module to be downloaded from the web or otherwise retrieved from some remote location or local directory.
3.       Initializing modules. The modules are then initialized. This means creating instances of the module class and calling the Initialize method on them via the IModule interface.
In this post I’ll show how to create a module catalog by registering modules in XAML and the load them using an MefBootstrapper implementation in Silverlight.
·         In you prism application’s shell project add a reference to Microsoft.Practices.Prism.MefExtensions
·         Now change your Bootstrapper implementation to inherit the MefBootstrapper class
·         Create a new .xaml page and name it ModuleCatalog.xaml. This is the file which is used to specify the modules to be loaded. You need to override the CreateModuleCatalog method to return the ModuleCatalog.xamluri.
·         Override the  ConfigureAggregateCatalog method and add the assembly of the Shell project to the AssemblyCatalog collection .
The final implementation looks like
public class Bootstrapper : MefBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        return this.Container.GetExportedValue<Shell>();    

    }

    protected override void InitializeShell()
    {
        base.InitializeShell();

        Application.Current.RootVisual = (UIElement)this.Shell;
    }

    protected override IModuleCatalog CreateModuleCatalog()
    {
        return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri("/BlogsPrajeesh.PrismSamples.Shell;component/ModuleCatalog.xaml", UriKind.Relative));

    }

    protected override void ConfigureAggregateCatalog()
    {
        base.ConfigureAggregateCatalog();
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
    }
}

·         Create a new Silverlight application project. This will serve as a module in your application. Add references of the following assemblies to the project
§  Microsoft.Practices.Prism.MefExtensions
§  Microsoft.Practices.Prism
·         Create a new class that inherits the IModule interface. This is the entry point for your module.
[ModuleExport("BlogsPrajeesh.PrismSamples.Configuration", typeof(ConfiguraitonModule))]
public class ConfiguraitonModule : IModule
{
    public ConfiguraitonModule()
    {

    }
    public void Initialize()
    {
        //Initialization code goes here
    }
}

·         Make sure that the Copy Local property for the Prism assemblies in the module is set to False.
·         Next we can add the module to the catalog file.
<Modularity:ModuleCatalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                          xmlns:sys="clr-namespace:System;assembly=mscorlib"
                          xmlns:Modularity="clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism">
    <Modularity:ModuleInfo Ref="BlogsPrajeesh.PrismSamples.Configuration.xap" ModuleName="BlogsPrajeesh.PrismSamples.Configuration" ModuleType="BlogsPrajeesh.PrismSamples.Configuration.ConfiguraitonModule, BlogsPrajeesh.PrismSamples.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />   
Modularity:ModuleCatalog>



1 comment:

Anonymous said...

I couldn't get this to work. How are you loading the content? I am using IRegionNavigationContentLoader.LoadContent