在加载模块后再增加模块
在加载模块后再增加模块,转载文章HERE
The following sequence diagram shows us that the InitializeModules() method is the last process run http://global-webnet.net/UML/prism.htm
Examining the baseclass code we find that the following is what actually loads the modules; suggesting that it is safe to run again because of the statement in bold:
private void LoadModuleTypes(IEnumerable<ModuleInfo> moduleInfos)
{
if (moduleInfos == null)
{
return;
}
foreach (ModuleInfo moduleInfo in moduleInfos)
{
if (moduleInfo.State == ModuleState.NotStarted)
{
if (this.ModuleNeedsRetrieval(moduleInfo))
{
this.BeginRetrievingModule(moduleInfo);
}
else
{
moduleInfo.State = ModuleState.ReadyForInitialization;
}
}
}
this.LoadModulesThatAreReadyForLoad();
}
I did a quick Proof of Concept with the StockTraderRI application to see if it was as easy as what was suggested to add a module after the BootStrapper process; I added the line in bold below to the App() class
namespace StockTraderRI
{
public partial class App : Application
{
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
//this.RootVisual = new Shell();
var bootstrapper = new StockTraderRIBootstrapper();
bootstrapper.Run();
// Watch module will not load without this line
bootstrapper.PostCatalogProcessing();
}
I then updated the StockTraderRIBootstrapper as follows.
namespace StockTraderRI
{
public partial class StockTraderRIBootstrapper : UnityBootstrapper
{
protected override IModuleCatalog GetModuleCatalog()
{
var catalog = new ModuleCatalog();
catalog.AddModule(typeof(MarketModule))
.AddModule(typeof(PositionModule), "MarketModule");
//.AddModule(typeof(WatchModule), "MarketModule"); <= remarked out
//.AddModule(typeof(NewsModule)); <= remarked out
return catalog;
}
public void PostCatalogProcessing()
{
ModuleCatalog catalog = (ModuleCatalog)Container.Resolve<IModuleCatalog>();
catalog.AddModule(typeof(WatchModule), "MarketModule");
catalog.AddModule(typeof(NewsModule));
InitializeModules();
}