12 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
2026-04-16T02:17:34.305643+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 716052479006d994 |
Region Manager Module Documentation
1. Purpose
This module provides a structured abstraction layer over Prism's IRegionManager for managing views and viewmodels in a WPF application using Unity DI. Its primary purpose is to enforce a consistent pattern for view registration, activation, removal, and data refresh operations by leveraging ViewDefinition metadata to decouple view/viewmodel instantiation from region management logic. It supports both synchronous and asynchronous operations, handles multiple view instances optionally, and integrates with the application's event aggregation system for error notifications. The module exists to standardize region management across different shell contexts (e.g., MainRegion, NavigationRegion) and to provide reusable, type-safe operations for view lifecycle management.
2. Public Interface
ViewDefinition Class
-
ViewDefinition(string regionName, Type viewInterfaceType, Type viewModelInterfaceType)
Constructor that initializes a view definition with the target region name, the interface type of the view to be resolved, and the interface type of the viewmodel to be resolved. The actual view and viewmodel types are resolved via the DI container using these interfaces. -
string RegionName { get; }
Gets the name of the region where the view will be displayed. -
Type ViewInterfaceType { get; }
Gets the interface type used to resolve the view instance (e.g.,IBaseViewor a custom view interface). -
Type ViewModelInterfaceType { get; }
Gets the interface type used to resolve the viewmodel instance (e.g.,IBaseViewModelor a custom viewmodel interface).
Note
: The
ViewTypeandViewModelTypeproperties are commented out in the source and not used.
IDataProRegionManager, IDTSRegionManager, IDTSViewRegionManager Interfaces
All three interfaces are identical in signature and define the same contract. They extend IRegionManager and add the following methods:
-
void AddView(ViewDefinition viewDefinition, object parameter)
Adds a view to the region specified inviewDefinition, usingparameterto initialize the viewmodel. Does not allow multiple instances (delegates toAddView(viewDefinition, parameter, false)). -
void AddView(ViewDefinition viewDefinition, object parameter, bool allowMultipleInstances)
Adds a view to the region specified inviewDefinition. IfallowMultipleInstancesisfalse, attempts to activate an existing view of the same type instead of adding a new one. -
Task AddViewAsync(ViewDefinition viewDefinition, object parameter)
Asynchronous version ofAddView(viewDefinition, parameter). -
Task AddViewAsync(ViewDefinition viewDefinition, object parameter, bool allowMultipleInstances)
Asynchronous version ofAddView(viewDefinition, parameter, allowMultipleInstances). -
void RemoveView(IBaseViewModel viewModel)
Removes all views associated with the givenviewModelfrom all regions. CallsCleanup()on the viewmodel before removal. -
void RemoveViewByRegionName(string regionName)
Clears all views from the specified region by callingClearRegion(regionName). -
void RefreshView(Type interfaceForView, object parameter)
Finds all views of typeinterfaceForViewacross all regions, callsCleanup()on their viewmodels, and reinitializes them withparameter(ornullif none provided). Synchronous. -
Task RefreshViewAsync(Type interfaceForView, object parameter)
Asynchronous version ofRefreshView. CallsCleanupAsync()andInitializeAsync()on the viewmodels.
RegionManagerExtensions Class (Static)
Provides utility extension methods for IRegionManager:
-
void ClearRegion(this IRegionManager regionManager, string regionName)
Removes all views from the specified region. -
IList<object> GetViews(this IRegionManager regionManager, string regionName, Type interfaceForView)
Returns a list of views in the region that implementinterfaceForView. -
object GetView(this IRegionManager regionManager, string regionName, Type interfaceForView)
Returns the first view in the region that implementsinterfaceForView. -
bool ActivateViewIfExists(this IRegionManager regionManager, string regionName, Type viewType)
Activates the first view ofviewTypein the region if it exists. Returnstrueif activated,falseotherwise. Internally callsActivateSingleView, which also invokesviewModel.Activated()on the viewmodel. -
void DeactivateViews(this IRegionManager regionManager, string regionName)
Deactivates all currently active views in the region. -
void RemoveViews(this IRegionManager regionManager, string regionName)
Removes all active views from the region. -
void ActivateLastView(this IRegionManager regionManager, string regionName)
Activates the last active view in the region, or if none are active, the last view in the region’s view collection. -
void AddViewToRegion(this IRegionManager regionManager, string regionName, object view)
Adds a view to the region without activating it. -
void AddViewToRegionActivate(this IRegionManager regionManager, string regionName, object view)
Adds and activates a view in the region. -
void RemoveView(this IRegionManager regionManager, object view)
Removes the view from all regions it belongs to. -
void RemoveView(this IRegionManager regionManager, string regionName, object view)
Deactivates and removes the view from the specified region.
DTSRegionManager, DTSViewRegionManager, DataProRegionManager Classes
All three classes implement their respective interfaces (IDTSRegionManager, IDTSViewRegionManager, IDataProRegionManager) and are functionally identical in behavior. They differ only in the interface they implement and minor variations in GetShellViewModelByRegionName logic (see Gotchas). They share the same constructor:
DTSRegionManager(IUnityContainer unityContainer, IRegionManager regionManager, IEventAggregator eventAggregator)
Constructor injecting Unity container, Prism region manager, and event aggregator.
All method implementations are as described in the interfaces above.
3. Invariants
ViewDefinitionmust specify non-nullregionName,viewInterfaceType, andviewModelInterfaceType— enforced by constructor parameters; null values are not validated but will cause runtime errors during resolution.- All
AddView/AddViewAsyncoperations resolve view and viewmodel viaIUnityContainerusingviewInterfaceTypeandviewModelInterfaceType— the actual concrete types must be registered in the container. IBaseView.DataContextmust be set to the resolvedIBaseViewModelbefore adding to region — enforced inAddView/AddViewAsyncimplementations.IBaseViewModel.InitializeorInitializeAsyncis called after view registration — initialization is deferred until after view is added to region and bound to viewmodel.RemoveView(IBaseViewModel)removes all views associated with the viewmodel from all regions — not limited to a single region.RefreshView/RefreshViewAsynciterates over all regions — not region-specific, and may affect views in unintended regions.allowMultipleInstances = falseprevents duplicate view instances — by attempting to activate an existing view instead of adding a new one.ClearRegion/RemoveViewByRegionNameremoves all views in the region — no selective removal.
4. Dependencies
Module Dependencies
Microsoft.Practices.Prism.Regions— ProvidesIRegionManager,IRegionCollection,IRegion, andIRegionManagerbase types.Microsoft.Practices.Unity— ProvidesIUnityContainerfor DI.DTS.Common.Base— DefinesIBaseView,IBaseViewModel, andIBaseViewModelinterface methods (Initialize,InitializeAsync,Cleanup,CleanupAsync,Activated).DTS.Common.Events— DefinesRaiseNotificationevent andNotificationContentEventArgs.DTS.Common.Classes— Used forUtility.GetAllErrorMessages(ex)(error aggregation).DTS.Common.Interface— DefinesIShellViewModeland region name constants (e.g.,RegionNames.MainRegion,RegionNames.NavigationRegion).
Dependencies on This Module
- Any module or service that needs to manage views in Prism regions (e.g., shell initialization, module loading, user navigation).
- Likely consumed via DI as
IDTSRegionManager,IDTSViewRegionManager, orIDataProRegionManagerdepending on the shell context.
5. Gotchas
-
ViewTypeandViewModelTypeproperties are commented out — The class only stores interface types (ViewInterfaceType,ViewModelInterfaceType), not concrete types. This is intentional but may be confusing if one expects full type metadata. -
Three region manager implementations with identical logic —
DTSRegionManager,DTSViewRegionManager, andDataProRegionManagerare near-duplicates. OnlyDataProRegionManager.GetShellViewModelByRegionNamehas region-specific logic (resolvesIShellViewModelonly forMainRegion), while the others resolve unconditionally. This duplication suggests technical debt or incomplete refactoring. -
AddViewAsyncdoes not guarantee thread safety — Althoughasync, it performs UI-bound operations (view resolution, settingDataContext, region operations) on the calling thread (likely the UI thread due to Prism’s region manager). No explicit dispatcher marshaling is visible. -
RefreshView/RefreshViewAsyncmay affect multiple regions unintentionally — They iterate over all regions and refresh all views matchinginterfaceForView, which could cause unexpected side effects if the same view type is used in multiple regions. -
RemoveViewByRegionNameusesClearRegion— This removes all views in the region, not just those associated with a specific viewmodel or view type. -
ActivateViewIfExistsreturnsfalseif view does not exist — But does not add or create the view; callers must handle this case explicitly. -
GetShellViewModelByRegionNameis incomplete — InDTSRegionManagerandDTSViewRegionManager, it always resolvesIShellViewModelregardless of region. InDataProRegionManager, it only resolves forMainRegion. This inconsistency may lead toContextMainRegionnot being set for other regions. -
No validation of
parametertype or nullability —Initialize/InitializeAsyncare called with the rawparameterobject; if the viewmodel does not support the provided parameter, runtime errors may occur. -
Error handling uses event aggregation — Exceptions in
AddViewAsync,RefreshView, andRefreshViewAsyncare caught and published viaRaiseNotification. Callers must subscribe to this event to be notified of failures. -
RemoveViewusesReferenceEqualsfor viewmodel matching — This relies on reference equality of viewmodels, which may not hold if viewmodels are recreated or compared by value.