views:

1108

answers:

4

I'm reading through the Prism v2 guidelines in which they state:

During initialization, modules use the RegionManager to locate regions in the shell and add one or more views to those regions or register one or more view types to be created within those regions

I understand that views are added in the bootstrapper e.g. in the GetModuleCatalog() method:

protected override IModuleCatalog GetModuleCatalog()
{
    ModuleCatalog catalog = new ModuleCatalog()
        .AddModule(typeof(HelloWorldModule.HelloWorldModule));
    return catalog;
}

But what does it mean to register a view type? Why do modules need to "register a view type" with the shell if they are already "adding their views" as with the above code?

+2  A: 

When you register a type with a Region that type is instatiated whenever the region is shown.

If you locate a region and then add views to it you don't need to register a type with that view, since you're doing the work rather than letting the region manager do it.

Graeme Bradbury
I would think in "AddModule" that we would "add a module". Instead, we "add a module type". In application in which I have worked with modules, we have had methods such as AddModule and we actually did indeed add the modules themselves. Why do we add a "module type" instead of a module itself?
Edward Tanguay
ModuleCatalog doesn't hold a module it holds information about the module.I'd suggesting understanding DI/IoC before progressing with ModuleCatalog since you need to understand that before you'll understand why you dont deal with modules and only module types.
Graeme Bradbury
+5  A: 

In your code you are not adding Views to the bootstrapper but Modules to the ModuleCatalog. A Module in the CAB/Prism/Composite UI world can contain Views, but many times it provides some sort of add-on service that other Modules can use. For example, let's say I have a Shell that happens to uses some docking manager to display views. I want modules to use an API IDockingWindowService to show/hide window. I want the service implementation to be easily interchangeable so I create a Module that contains a service called DockingWindowService and implements IDockingWindowService. I register this Module with the ModuleCatalog.

The composite framework workflow would create this service, registers it with the bootstrapper and any modules loaded after this fact would be able to use the IDockingWindowService. This service is not a view, but logic; just wanted to point out that distinction. That being said, a Module can contain 0 or more Views (or, as a simplification, UserControls). The unit of UI is the View. A Module is more of a logic and/or UI bundling concept.

Back to your particular question: What the documentation is saying is that if you use Regions to display your Views, you can register the View types with the Region. Whenever the Region is shown, it will automatically build the View using the Unity container.

siz
A: 

In your example you are adding module to application modules which I think of it as a loading of library with class implementing IModule interface (module initializer class)

Every time when Intialize method of that module initializer class is invoked, module register it's own IoC mappings and other things needed for module work.

Now, a module can load a view during the module initialziation, (adding the menu item or toolbar item etc). That would cover "adding of one or more views during the module initialization" part of your question.

Beside showing the views during the initialization, module usually contains more views which are not to be shown in the moment of module loading but instead usually in reaction to some event (UserLoogingIn event could requirel login vew to be shown). In order for Prism to show that view all of the mappings between the view and presentation model have to be defined already in module initializer.

Something like this (based on RI code style)

this.container.Register(); this.container.Register();

So, module initializer would register views by defining mappings needed for unity to resolve the view during the region manager operation of loading view.

Nikola Malovic
A: 

In the code above, you're filling out a module catalog. This is part of how modularity works in Prism. I have a screencast explaining it here. Essentially, you're telling Prism to load a .dll or .xap file. These "modules" can contain 2 things: services (think implementations of interfaces) and views.

When a Module (usually a .dll or .xap file) is loaded, an Initialize method is called where you can register services and regions:

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
}

Notice the view registration:

_regionManager.RegisterViewWithRegion("MainRegion", typeof(ModuleAView));

You could be registering any number of views here in the Initialize. And in any Initialize for any Module (again, usually a .xap or .dll).

Erik Mork