Understanding RegionManger vs. RegionViewRegistry

Topics: Prism v4 - WPF 4
Jun 23, 2011 at 3:25 PM

Hi there,

Recently when creating some test I found that I had an issue mocking RegionManager as the call RegisterViewWithRegion was a static extension method, I see that in RegionViewRegistry it is simply a regular method and can easily be mocked. I also tested my app, I guess from my limited understanding of the region manager that calling either or would work and I could interchange them easily and impact nothing else in the app (even though others are using RegionManager objects). I was correct, I simply switched my module to take a RegionViewRegistry object in the ctor and everything seems to work just fine.

What I don't understand is why this is like this? Why are there two implementations of this method that do the same thing? (I think)... ultimately they seem to place stuff in the same region manager so it doesn't seem to be an issue but I would very much like to understand this better.

Thanks for your input!

Developer
Jun 23, 2011 at 6:23 PM
Edited Jun 23, 2011 at 7:05 PM

Hi,

As you can read in the UI Composition chapter of the Prism MSDN documentation, there are two approaches for UI composition; view discovery, and view injection.

The view injection approach involves adding (or "injecting") views directly into the specified region, and this requires obtaining an instance of the region, which is usually done through accessing the corresponding RegionManager's Regions collection.

On the other hand, the view discovery approach involves registering views in the RegionViewRegistry, which indicates that, when a region with the specified name is created, a certain view should be added into that region. When following this approach, you don't need to obtain a reference to the RegionManager.

However, for usability purposes, an extension method was placed in the RegionManager class, which internally obtains a reference to the RegionViewRegistry and calls its RegisterViewWithRegion method (since it's likely that you will use both, and it could be more comfortable to have a single dependency on the RegionManager in your component).

To illustrate this, here's the code for the RegisterViewWithRegion extension method in the RegionManager class:

 

public static IRegionManager RegisterViewWithRegion(this IRegionManager regionManager, string regionName, Type viewType)
        {
            var regionViewRegistry = ServiceLocator.Current.GetInstance<IRegionViewRegistry>();

            regionViewRegistry.RegisterViewWithRegion(regionName, viewType);

            return regionManager;
        }

 

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jun 23, 2011 at 8:14 PM

Thanks! Yes - that is very helpful and confirms what my tests already showed me, I will also take another look at the documentation :)