Search code examples
c#moduleprismregion

Example of factory<ViewModel> in PRISM?


In PRISM, I've got two modules:

|_ ModuleA
|  |_ViewA
|  |_ViewModelA
|  |_ModelA
|
|_ ModuleB
   |_ViewB
   |_ViewModelB
   |_ModelB

And I want to have a factory design where you can choose the Module to show in the shell (I'm using Unity)

I've just created factory designs for models, for never to Views in PRISM.


Solution

  • It shouldn't be too difficult to set this up. Off the top of my head, you'll probably want to add some kind of friendly context name to your module exports (the class that implements IModule and has the ModuleExport attribute), that you'll use to display the available modules. Then reflect all the loaded assemblies and find your ModuleExports, switching to a custom interface (e.g. IMyModuleInfo) that will allow you to get your friendly name. You could put all of this reflection code in your Shell's ViewModel, from which you could return a list of IMyModuleInfo, binding to whatever Shell listing you'd like to show for the user. You could use your IModule implementation to be a view factory, or you could have IMyModuleInfo return the assembly name, from which you could reflect all of the available views.

    Hope that helps.