Some time ago I met someone ask me, in the prism, can put the module information in the XML file. This nature no problem, we do a modulecatalog of their own, but I have not done, today is just a simple realization of a moment, interested friends can refer to.
Why to divide the module, this of course has many advantages, such as independent development, independent testing, independent loading, and there is no direct reference between the modules, the modification will not cause problems in other modules, always benefit a lot. When we do a small Silverlight program, we can do it without thinking about prism, but if you do an RIA, like business application, there may be hundreds of pages, different data, tables, and so on, if you don't divide the modules, It must have been a porridge in the end. Architecture is designed to make our projects more organized, and to be better controlled when they become more and more complex, and to adapt to changing requirements at all times.
In Prism2.0, the configuration information of the module itself can be placed into XAML, in the smaple, we can see the following code:
1 <modularity:modulecatalog xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2 xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
3 xmlns:sys= "Clr-namespace:system;assembly=mscorlib"
4 xmlns:modularity= "clr-namespace:microsoft.practices.composite.modularity;assembly=microsoft.practices.co Mposite ">
5 <modularity:moduleinfogroup ref= "modulex.xap" initializationmode= "OnDemand" >
6 <modularity:moduleinfo modulename= "Modulex" moduletype= "Modulex.modulex, Modulex, Version=1.0.0.0"/>
7 </Modularity:ModuleInfoGroup>
8 <modularity:moduleinfogroup ref= "moduleswy.xap" initializationmode= "whenavailable" >
9 <modularity:moduleinfo modulename= "Moduley" moduletype= "Moduley.moduley, Moduleswy, Version=1.0.0.0" >
Ten <Modularity:ModuleInfo.DependsOn>
<sys:String>ModuleW</sys:String>
</Modularity:ModuleInfo.DependsOn>
</Modularity:ModuleInfo>
<modularity:moduleinfo modulename= "Modulew" moduletype= "Modulew.modulew, Moduleswy, Version=1.0.0.0" >
</Modularity:ModuleInfo>
</Modularity:ModuleInfoGroup>
<!--Module Info without a group-->
<modularity:moduleinfo ref= "modulez.xap" modulename= "Modulez" moduletype= "Modulez.modulez", ModuleZ, Version=1. 0.0.0 "/>
</Modularity:ModuleCatalog>
In bootstrapper, we can invoke the following:
protected override IModuleCatalog GetModuleCatalog()
{
return
ModuleCatalog.CreateFromXaml(
new Uri("/RemoteModuleLoading;component/ModulesCatalog.xaml", UriKind.Relative));
}
If we were to put it in XML, it would be simple to declare a file like the following:
<?xml version= "1.0" encoding= "Utf-8"?>
<Modules>
<module name= "Leftmenumodule" mode= "whenavailable" SilverlightMonitorApplication.LeftMenuModule.LeftMenuModule, Silverlightmonitorapplication, version=1.0.0.0, Culture=neutral, Publickeytoken=null "/>
<module name= "Bottommenumodule" mode= "OnDemand" SilverlightMonitorApplication.BottomMenuModule.BottomMenuModule, Silverlightmonitorapplication, version=1.0.0.0, Culture=neutral, Publickeytoken=null ">
<Module.DependsOn>
<ModuleName>LeftMenuModule</ModuleName>
<module name>module1</modulename>
</Module.DependsOn>
</Module>
<module name= "Module1" mode= OnDemand "type=" SilverlightMonitorApplication.Module1.Module1, Silverlightmonitorapplication, version=1.0.0.0, culture=neutral, Publickeytoken=null ">
</Module>
<module name= "Module2" mode= OnDemand "type=" SilverlightMonitorApplication.Module2.Module2, Silverlightmonitorapplication, version=1.0.0.0, culture=neutral, Publickeytoken=null ">
</Module>
</Modules>
And then realize a modulecatalog, I am here to name Xmlmodulecatalog, this xmlmodulecatalog of course to inherit from Modulecatalog. Of course, you can also have a better way to achieve. I will provide a way of thinking, the code I write, on the writing code, I was unable to catch up with full-time developers.
public class XmlModuleCatalog : ModuleCatalog
{
public XmlModuleCatalog(string xmlUri)
: base()
{
try
{
XElement doc = XElement.Load(xmlUri);
var q = from x in doc.Descendants("Module")
select new ModuleInfo(x.Attribute("name").Value, x.Attribute("type").Value, (from c in x.Descendants ("ModuleName")
select c.Value).ToArray()
) { InitializationMode = x.Attribute("mode").Value.Equals("OnDemand") ? InitializationMode.OnDemand : InitializationMode.WhenAvailable };
foreach (ModuleInfo m in q)
{
this.AddModule(m);
}
}
catch
{
throw new ArgumentException("XML file is not found or invalid!");
}
}
}