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.

No comments: