backgroundTop
backgroundTop
Front Page
GreenBackgroundTop

Modularity in Prism

Modularity - Testing, Module Catalog and Unity in Prism

Modularity allows applications to be broken down into discrete pieces. This allows for easier testing, better maintainability and the ability to distribute an application across multiple teams. This video explores the modularity support in Prism. The first part of the video covers Unity and Unit Testing. Inversion of Control using Dependency Injection and Service Location are both covered. The second part of the video details modules and how they're loaded by the Module Catalog.

erikmork

2k Votes

GreenBackgroundBottom
 

Important Code

Mark as Inappropriate Content

This video is licensed under a Creative Commons License.

Creative Commons License Download the Video

Information From the Video

GreenBackgroundTop

Example Class to Test

This is a super simple method to test. It simply returns an integer.
GreenBackgroundBottom
        public int doWork()
        {
            ICalculator calc = new Calculator();
            return calc.Add(5, 8);
        }
 
GreenBackgroundTop

What is a Service in Prism?

A service is simply any class (usually implementing an interface) that is used from another class.
GreenBackgroundBottom
 
GreenBackgroundTop

Add Silverlight Unit Test Project

To add a Unit Test Project in Silverlight, Add a new Silverlight Application and add references to: Microsoft.Silverlight.Testing and Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight
GreenBackgroundBottom
 
GreenBackgroundTop

Changing the Application Startup Method

Change this method to display the Unit Test page.
GreenBackgroundBottom
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            this.RootVisual = UnitTestSystem.CreateTestPage();
        }
 
GreenBackgroundTop

Initial Unit Test

The initial unit test is very simple. It just instantiates the class and verifies the integer output.
GreenBackgroundBottom
    [TestClass]
    public class ModuleAFixture
    {
        [TestMethod]
        public void runServiceA()
        {
            MiddleLayer m = new MiddleLayer();
            Assert.AreEqual(13, m.doWork());
        }

    }
 
GreenBackgroundTop

Inversion of Control (IoC) - Dependency Injection

Inversion of Control allows us to loosely couple classes. Dependency Injection is a kind of IoC where we pass in the dependency during construction of the object.
GreenBackgroundBottom
    public class MiddleLayer
    {
        ICalculator _calc;
        public MiddleLayer(ICalculator calc)
        {
            _calc = calc;
        }

        public int doWork()
        {
            return _calc.Add(5, 8);
        }
    }
 
GreenBackgroundTop

Testing with Dependecy Injection

In the simplest case, testing with Dependency Injection doesn't require a container or Unity or anything else. It just requires "newing" up a test service.
GreenBackgroundBottom
    [TestClass]
    public class ModuleAFixture
    {
        [TestMethod]
        public void runServiceA()
        {
            MiddleLayer m = new MiddleLayer(new TestCalculator);
            Assert.AreEqual(13, m.doWork());
        }

    }

    public class TestCalculator : ICalculator
    {
        #region ICalculator Members

        public int Add(int a, int b)
        {
            return 13;
        }

        #endregion
    }
 
GreenBackgroundTop

Unity in Prism

Unity provides a container that makes Inversion of Control easy. The container allows registration and resolution of types. This is a simple use of Unity. Unity can do extra stuff with object lifetimes, etc. To learn more about Unity, see the References section. In this case, the "container.Resolve" call just instantiates Middle Layer but Unity handles the dependency injection.
GreenBackgroundBottom
        [TestMethod]
        public void runServiceA()
        {
            IUnityContainer container = new UnityContainer();
            container.RegisterType<ICalculator, TestCalculator>();

            MiddleLayer m = container.Resolve<MiddleLayer>();
            Assert.AreEqual(13, m.doWork());
        }
 
GreenBackgroundTop

Service Location

Service Location is another kind of Inversion of Control. In Service Location, the object keeps a reference to the Unity Container and can query that container for services.
GreenBackgroundBottom
    public class MiddleLayer
    {
        ICalculator _calc;
        IUnityContainer _container;
        public MiddleLayer(ICalculator calc, IUnityContainer container)
        {
            _calc = calc;
            _container = container;
        }

        public int doWork()
        {
            //ICalculator calc = new Calculator();
            ICalculator calc = _container.Resolve<ICalculator>();
            return calc.Add(5, 8);

        }
    }
 
GreenBackgroundTop

Modules in Prism

Modules are the unit of work in Prism. They typically fall out along .dll or .xap boundaries. Each Module needs a type that implements IModule. The IModule type is called to bootstrap the Module into the execution of a Prism application.
GreenBackgroundBottom
    public class ModuleA : IModule
    {
        IRegionManager _regionManager;
        IUnityContainer _container;

        public ModuleA(IRegionManager regionManager, IUnityContainer container)
        {
            _regionManager = regionManager;
            _container = container;
        }

        #region IModule Members

        public void Initialize()
        {
            _container.RegisterType<ICompanyService, CompanyService>();
            _regionManager.RegisterViewWithRegion("MainRegion", typeof(ModuleAView));
            
        }

        #endregion
    }
 
GreenBackgroundTop

The Module Catalog

The Module Catalog is used to register and keep track of Modules. The Module Catalog is created in the GetModuleCatalog method of the Bootstrapper and allows Module dependencies to be specified. In addition, Modules can optionally be delay-loaded and only loaded if they're needed.
GreenBackgroundBottom
        protected override IModuleCatalog GetModuleCatalog()
        {
            ModuleCatalog catalog = new ModuleCatalog();
            catalog.AddModule(typeof(ModuleA.ModuleA), InitializationMode.WhenAvailable);
            return catalog;
        }
 
GreenBackgroundTop

Loading the Module by String

The module can be specified to the Module Catalog by string. This allows the scenario where there isn't a project reference between the bootstrapper project and the module project. This is generally better for loose coupling.
GreenBackgroundBottom
        protected override IModuleCatalog GetModuleCatalog()
        {
            ModuleCatalog catalog = new ModuleCatalog();

            //catalog.AddModule(typeof(ModuleA.ModuleA));
            catalog.AddModule("ModuleA", "ModuleA.ModuleA, ModuleA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", 
                InitializationMode.WhenAvailable);
            return catalog;
        }
 
GreenBackgroundTop

Module Catalog from XAML

In addition to the method above, the Module Catalog can be loaded from XAML. This allows very, very loose coupling. It means that a production application could be changed to a test application simply by changing a XAML file.
GreenBackgroundBottom
 
 
GreenBackgroundTop

References

Prism Silverlight

This is a video guide on how to get a Prism project started. It covers modules, catalogs, commanding and regions.

Prism Sample Code

The Prism sample code from this guide.

Prism CodePlex site

This is the main site for Prism and Prism information.

Unity for Silverlight

This is the December 2008 release of Unity for Silverlight. There are other versions of Unity available for desktop applications.

Unit Testing Binaries for Silverlight

This is the Unit Test framework for Silverlight. The source isn't found here, but it's the easiest place to find the binaries.

GreenBackgroundBottom
backgroundBottom
backgroundBottom