Build enterprise applications with WPF, WCF, EF and Prism. Tutorial 09.

Saturday, February 13, 2010 10:13 AM | Leave a reply »

Today we will see the modularity concept of Prism. Probably the best part of it, as it will allow us to build a real plug-and-play application where the main Shell won’t know anything about the modules.

Discover and load the module.

What does it mean? As we saw in the previous article, we have declared a reference in our Shell for the hello world module. In this way we force the Shell to be compiled including in the shell project a reference to the Hello World module. Fine, but what happen if we want to replace this module with a new version? We need to change the reference in the project and recompile it … Pretty ugly for a modular application doesn’t it?

The basic concept in Prism is that we can discover on fly the available modules and load them … This is the workflow process of discovering and loading a module in Prism.

 prism module

There are different ways to load a module. As we saw in the previous article, the easiest way is to add a direct reference to the module in the Shell and load it in the bootstrapper:

  1: ModuleCatalog catalog = new ModuleCatalog()
  2:    .AddModule(typeof(HelloWorldModule.HelloWorldModule));

The second way is to add the module reference in the app.config and REMOVE the code in the bootstrapper. In this way we won’t need to recompile the module each time we will run the application in Visual Studio and we won’t need a direct reference to it. Let’s see the code below. I have a added an app.config file in my shell project:

  1: <?xml version="1.0" encoding="utf-8" ?>
  2: <configuration>
  3:   <configSections>
  4:     <section name="modules"
  5:              type="Microsoft.Practices.Composite.Modularity.ModulesConfigurationSection, Microsoft.Practices.Composite"/>
  6:   </configSections>
  7:   <modules>
  8:     <module 
  9:       assemblyFile="Modules/HelloWorldModule.dll" 
 10:       moduleType="HelloWorldModule.HelloWorldModule, HelloWorldModule" 
 11:       moduleName="HelloWorldModule"/>
 12:   </modules>
 13: </configuration>

I am using the composite section in the app.config file to load a specific module. Now, in order to make this ‘runnable’ we need to:

  • remove the reference in the Shell project
  • clean-up the code in the bootstrapper that loads the module

The shell project will use this code now to load the Module catalog:

  1: ModuleCatalog catalog = new ConfigurationModuleCatalog();

As you can see we are telling to Prism to look in the app.config and load the modules available there. In order to make the module available in the modules folder inside the bin folder of the shell you can play with MSBuild or a post-event action in VS. I copied this code from MSDN and added to the post event build action in the HelloWorldModule project properties:

  1: xcopy "$(TargetDir)*.*" 
  2: "$(SolutionDir)Shell\bin\$(ConfigurationName)\Modules\" /Y

Pretty cool! We can now work with our modules without breaking any change in the Shell project, and trust me, when you work on a team, this happens every day …

Additional configuration options.

Of course the game is not done yet. We have a lot of options that we can configure in the app config in order to load modules on fly and to add dependencies from other modules. For example, let’s say that Outlook can’t open a view if the ribbon is not loaded, but the ribbon is on another module … Wink

Load on demand

We can specify, for example, that we want to load on demand (by request) a specific module that we don’t know. Pretty cool as this is how should work a real modular application. In order to do this we don’t need anything more that this tag in the app.config:

  1: <module 
  2:    assemblyFile="Modules/HelloWorld.dll" 
  3:    moduleType="HelloWorld, HelloWorld" 
  4:    moduleName="HelloWorld" 
  5:    InitializeMode="OnDemand"/>

Now that we setup the module to load “onDemand” the module won’t be load in memory until we will instruct Prism to do that. This is a real plug and play concept, because without any dependencies we can load on demand part of our UI during the program execution. In Windows Form this can’t simply be done!

  1: moduleManager.LoadModule("HelloWorld");

There you go, in a simple call from any view we can ask for a specific module. The initialize method of the module will be call and our UI will receive the ‘default’ view of that module, injected in the shell or in another module … there are no limitations at all.

Now we are ready to build the first cool module of our app, the main Ribbon bar.

Tags:


Comments

  1. Gravatar Mullany says:

    Re : # re: Build enterprise applications with WPF, WCF, EF and Prism. Tutorial 09.

    I get an unrecognized attribute when I try to use InitializeMode="OnDemand". It looks like an error in the documentation. Have you tried this? Change it to startupLoaded="false" works just fine or am I missing something? Thanks, great articles Raffaeu!
  2. Gravatar raffaeu says:

    Re : # re: Build enterprise applications with WPF, WCF, EF and Prism. Tutorial 09.

    Hi, I am sorry for that. What version of Prism are you using? I am working with the version 2.0 and everything seems to be ok on my side.
  3. Gravatar Kurko says:

    Re : # Integration Office 2007 WPF Ribbon into CWPF

    When do you explain integration Office 2007 WPF Ribbon into CWPF? Can you explain it, please?
  4. Gravatar raffaeu says:

    Re : # re: Build enterprise applications with WPF, WCF, EF and Prism. Tutorial 09.

    Yes I will do it right in the next article, just wait a little bit as I am busy on a project.
  5. Gravatar roncka says:

    Re : # re: Build enterprise applications with WPF, WCF, EF and Prism. Tutorial 09.

    I get the following error:

    Type name 'App' does not exist in the type 'Shell.Shell'
    D:\Silverlight 4\prismtutorial-41511\prismtutorial-41511\Shell\obj\Debug\App.g.cs 47 39 Shell

    Can you help me?
    Thanks
  6. Re : # re: Build enterprise applications with WPF, WCF, EF and Prism. Tutorial 09.

    @roncka
    This is generated code, when you remove the 'Shell.' it will build.

    This realy is a great tutorial, thanks a lot!
    Hope to read more soon.
  7. Gravatar raffaeu says:

    Re : # re: Build enterprise applications with WPF, WCF, EF and Prism. Tutorial 09.

    First of all I don't think you are using the correct trunk because my project doesn't have a SIlverlight 4 folder.
    THen, this happen because VS add this in the Shell: Shell.Shell. Try to remove Shell namespace and the project will compile properly.
    I am going to change this in the next release.
  8. Gravatar Ashok says:

    Re : # re: Build enterprise applications with WPF, WCF, EF and Prism. Tutorial 09.

    Hi, all the posts of Prims et.al. are not showing the images. Looks like the image links are broken.
  9. Re : # re: Build enterprise applications with WPF, WCF, EF and Prism. Tutorial 09.

    Is PRISM really as much as a behemoth as people make it out to be? I'm a bit scared of it.
  10. Gravatar anonymous says:

    Re : # re: Build enterprise applications with WPF, WCF, EF and Prism. Tutorial 09.

    Images are not loading...
  11. Gravatar learner says:

    Re : # re: Build enterprise applications with WPF, WCF, EF and Prism. Tutorial 09.

    Hi Raffaele thx for such a nice series creating the whole app instead of bits and peices, i been following it and its been a gr8 learning exp. Wondering if there are any plans for further series?
  12. Gravatar raffaeu says:

    Re : # re: Build enterprise applications with WPF, WCF, EF and Prism. Tutorial 09.

    Absolutely they are!
    In march I will be on Amazon with two books that will include all the tutorials you found on my Blog, then I will be back on blogging about WPF, MVVM and PRISM V4.
    I am planning to write a new series as soon as I am done with the two books (March/April 2011)
    Thanks.
Comments have been closed on this topic.