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.
public int doWork()
{
ICalculator calc = new Calculator();
return calc.Add(5, 8);
}
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = UnitTestSystem.CreateTestPage();
}
[TestClass]
public class ModuleAFixture
{
[TestMethod]
public void runServiceA()
{
MiddleLayer m = new MiddleLayer();
Assert.AreEqual(13, m.doWork());
}
}
public class MiddleLayer
{
ICalculator _calc;
public MiddleLayer(ICalculator calc)
{
_calc = calc;
}
public int doWork()
{
return _calc.Add(5, 8);
}
}
[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
}
[TestMethod]
public void runServiceA()
{
IUnityContainer container = new UnityContainer();
container.RegisterType<ICalculator, TestCalculator>();
MiddleLayer m = container.Resolve<MiddleLayer>();
Assert.AreEqual(13, m.doWork());
}
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);
}
}
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
}
protected override IModuleCatalog GetModuleCatalog()
{
ModuleCatalog catalog = new ModuleCatalog();
catalog.AddModule(typeof(ModuleA.ModuleA), InitializationMode.WhenAvailable);
return catalog;
}
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;
}
This is a video guide on how to get a Prism project started. It covers modules, catalogs, commanding and regions.
The Prism sample code from this guide.
This is the main site for Prism and Prism information.
This is the December 2008 release of Unity for Silverlight. There are other versions of Unity available for desktop applications.
This is the Unit Test framework for Silverlight. The source isn't found here, but it's the easiest place to find the binaries.