The CompositeWPF's UnityBootstrapper baseclass has a virtual method called GetModules(); this method will return an IModuleEnumerator containing a list of Modules (ModuleInfo) that you want to load for your solution. The following, from the CompositeWPF Commanding solution, is a typical implementation:
namespace Commanding
{
class CommandingBootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
Shell shell = Container.Resolve<Shell>();
shell.Show();
return shell;
}
protected override IModuleEnumerator GetModuleEnumerator()
{
return new StaticModuleEnumerator().AddModule(typeof(OrderModule));
}
}
}
In the case of my SDMS application I chose to use the DirectoryLookupModuleEnumerator(path) class so that I can return an IModuleEnumerator for the currently running application path as follows:
public class LoadReferencedModules
{
public static IModuleEnumerator GetModules(Type type)
{
string path = new FileInfo(type.Assembly.Location).DirectoryName;
return new DirectoryLookupModuleEnumerator(path);
}
}
This method effectively allows my MainShell to load any module it has a reference to; this means that all I have to do to load an application is set a reference to it and I'm done. Below I show how the main shell accomplishes this.
public App()
{
// Display shell with Loading... message
ShowShell();
// Configure the container and load modules
BootStrapper bootStrapper = new BootStrapper(CreateShell, GetType());
bootStrapper.Run();
}
protected DependencyObject CreateShell(IUnityContainer container)
{
container.Resolve<ILogger>()
.Log("CreateShell (Buildup)", Category.Debug, Priority.Low);
// At this point the bootstrapper has configured the
// unity container - we can buildup our shell
container.BuildUp(shell);
return shell;
}
Notice I send the BootStrapper above GetType(), the BootStrapper in turn will use this type to get the location of the asemblies to load:
public BootStrapper(ShellCreationDelegate createShell, Type type)
{
_createShell = createShell;
_moduleType = type;
_getModules = GetModuleEnumeratorDirectory;
}
protected IModuleEnumerator GetModuleEnumeratorDirectory()
{
return LoadReferencedModules.GetModules(_moduleType);
}
Tags:
compositewpf,
sdms,
moduleloader
Categories:
CompositeWPF