Showing posts with label Prism. Show all posts
Showing posts with label Prism. Show all posts

Tuesday, September 22, 2009

The need for ModuleBase class in Prism

When I started a sample application in Prism, I came to some situations where I need ModuleManager and all.After a small google I came to know that if we specify some arguments in the constructor of Modules we could get lot of useful objects such as ModuleManager,RegionManager etc...We can hold them for future uses.

Instead of having properties in each module to hold these objects, it is always better to have an abstract base class which holds all these objects.Then I did a trial an error method to find out what are all the objects which we can get and it came around 4 objects.

  • IModuleManager
  • IRegionManager
  • IUnityContainer
  • IRegionViewRegistry

And the base class implementation is as follows.
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
 
namespace Prism.Core
{
    public abstract class ModuleBase : IModule
    {
        IModuleManager _modulemanager;
 
        public IModuleManager ModuleManager
        {
            get { return _modulemanager; }
            set { _modulemanager = value; }
        }
        IRegionViewRegistry _regionViewRegistry;
 
        public IRegionViewRegistry RegionViewRegistry
        {
            get { return _regionViewRegistry; }
            set { _regionViewRegistry = value; }
        }
 
        IRegionManager _regionManager;
        public IRegionManager RegionManager
        {
            get { return _regionManager; }
            set { _regionManager = value; }
        }
 
        IUnityContainer _container;
        public IUnityContainer Container
        {
            get { return _container; }
            set { _container = value; }
        }
 
        public ModuleBase(IModuleManager moduleManager, 
            IRegionManager regionManager,
            IUnityContainer container, 
            IRegionViewRegistry reg)
        {
            ModuleManager = moduleManager;
            RegionManager = regionManager;
            Container = container;
            this.RegionViewRegistry = reg;
        }
 
        #region IModule Members
 
        public abstract void Initialize();
 
        #endregion
    }
}
The same thing is applicable in the case of Views too.

Wednesday, September 2, 2009

Specify Modules in XAML

When I first started PRISM my thought was how to specify the modules through a xaml file or an xml fille.The existing code is written as like something hard coded in the Bootstraper.cs.

protected override Microsoft.Practices.Composite.Modularity.IModuleCatalog GetModuleCatalog()
{
    var catalog = new ModuleCatalog();
    catalog.AddModule(typeof(MarkModule));
    catalog.AddModule(typeof(StudentsListModule));
    return catalog;
}

We can easily setup an xml file and create module objects using reflection.But I was sure, there will be an implementation of same somewhere in the framework.

After spending some time, I got a new way to implement my scenario.Why don't I have a new xaml file derived from ModuleCatalog and make the entries in xaml ? Yes that did the trick.

I added a new file which derives from ModuleCatelog and added my module entries.The xaml looks as below.

<modularity:ModuleCatalog 
    x:Class="Students.StudentsModuleCatalog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:modularity="clr-namespace:Microsoft.Practices.Composite.Modularity;assembly=Microsoft.Practices.Composite">
 
    <modularity:ModuleInfo 
        ModuleName="StudentsListModule"
        ModuleType="Students.StudentsListModule, Students, Version=1.0.0.0" />
    <modularity:ModuleInfo 
        ModuleName="MarKModule"
        ModuleType="Students.MarkModule, Students, Version=1.0.0.0" />
 
</modularity:ModuleCatalog>

Make sure that the code behind class should inherit from ModuleCatalog.

Now you can Rewrite the Bootstraper.GetModuleCatalog() as follows

protected override Microsoft.Practices.Composite.Modularity.IModuleCatalog GetModuleCatalog()
{
    return  new StudentsModuleCatalog();
}
This makes the code more neat and we can easily add or remove modules.

Saturday, August 8, 2009

Some useful links to Prism

What is Prism

According to my understanding about prism ,it is nothing but a framework which implements a modularized environment for different application blocks or UI components. We can develop modules separately and plug into the different regions in UI.

Samples
Hope I can add my own articles when I the project starts.