12 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
2026-04-16T02:56:51.813821+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 9e0ce2fec908c09b |
Region Management Module Documentation
1. Purpose
This module provides a structured abstraction layer over Prism's region management system for a WPF application, enabling consistent view registration, activation, removal, and data refresh operations across regions using dependency injection and MVVM patterns. It defines a ViewDefinition class to declaratively specify view-viewmodel pairs and their target regions, and implements three region manager classes (DataProRegionManager, DTSRegionManager, DTSViewRegionManager) that implement dedicated interfaces (IDataProRegionManager, IDTSRegionManager, IDTSViewRegionManager) to handle view lifecycle operations—including initialization, cleanup, and asynchronous operations—while integrating with Unity container resolution, event aggregation for notifications, and shell view model context updates. The module also provides extension methods (RegionManagerExtensions) to augment the base IRegionManager with utility operations like clearing, activating, and retrieving views by interface type.
2. Public Interface
ViewDefinition Class
-
Constructor:
public ViewDefinition(string regionName, Type viewInterfaceType, Type viewModelInterfaceType)
Creates a view definition specifying the target region, the interface type of the view to be resolved, and the interface type of the viewmodel to be resolved. TheViewTypeproperty is commented out and unused. -
Properties:
public string RegionName { get; }– The name of the region where the view will be displayed.public Type ViewInterfaceType { get; }– The interface type used to resolve the view (e.g.,IViewForMyViewModel).public Type ViewModelInterfaceType { get; }– The interface type used to resolve the viewmodel (e.g.,IMyViewModel).private ViewDefinition()– Private parameterless constructor; not used in public API.
IDataProRegionManager, IDTSRegionManager, IDTSViewRegionManager Interfaces
All three interfaces define identical public members (only IDataProRegionManager is shown; others are identical in signature):
-
void AddView(ViewDefinition viewDefinition, object parameter)
Adds a view to its specified region, resolving view and viewmodel via Unity using the types inviewDefinition, settingview.DataContext, activating the view, and callingviewModel.Initialize(parameter)orviewModel.Initialize()ifparameteris null. -
void AddView(ViewDefinition viewDefinition, object parameter, bool allowMultipleInstances)
Same as above, but ifallowMultipleInstancesisfalse, attempts to activate an existing view instance (viaActivateViewIfExists) and returns early if successful. -
Task AddViewAsync(ViewDefinition viewDefinition, object parameter)
Asynchronous version ofAddView(viewDefinition, parameter, false). -
Task AddViewAsync(ViewDefinition viewDefinition, object parameter, bool allowMultipleInstances)
Asynchronous version ofAddView(viewDefinition, parameter, allowMultipleInstances). UsesInitializeAsync/CleanupAsyncwhere applicable and logs exceptions viaRaiseNotificationevent. -
void RemoveView(IBaseViewModel viewModel)
Finds all views whoseDataContextis the givenviewModel(across all regions), callsviewModel.Cleanup(), deactivates and removes each view. -
void RemoveViewByRegionName(string regionName)
Clears all views from the specified region usingClearRegion. -
void RefreshView(Type interfaceForView, object parameter)
Finds all views implementinginterfaceForViewacross all regions, callsviewModel.Cleanup(), thenviewModel.Initialize(parameter)orviewModel.Initialize(). -
Task RefreshViewAsync(Type interfaceForView, object parameter)
Asynchronous version ofRefreshView, callingCleanupAsyncandInitializeAsync.
Note
: The three manager interfaces (
IDataProRegionManager,IDTSRegionManager,IDTSViewRegionManager) are functionally identical. Their implementations (DataProRegionManager,DTSRegionManager,DTSViewRegionManager) share nearly identical logic, with minor differences inGetShellViewModelByRegionNameand region-specific shell context handling (e.g.,NavigationRegionis only handled inDataProRegionManager).
RegionManagerExtensions Class
-
public static void ClearRegion(this IRegionManager regionManager, string regionName)
Removes all views in the specified region. -
public static IList<object> GetViews(this IRegionManager regionManager, string regionName, Type interfaceForView)
Returns all views in the region that are instances ofinterfaceForView. -
public static object GetView(this IRegionManager regionManager, string regionName, Type interfaceForView)
Returns the first view in the region that is an instance ofinterfaceForView. -
public static bool ActivateViewIfExists(this IRegionManager regionManager, string regionName, Type viewType)
Activates the first view in the region matchingviewType(if present), returningtrueif activated. Internally callsActivateSingleView, which also invokesviewModel.Activated()on the viewmodel. -
public static void DeactivateViews(this IRegionManager regionManager, string regionName)
Deactivates all active views in the region. -
public static void RemoveViews(this IRegionManager regionManager, string regionName)
Removes all active views in the region (note: only active views, not all views). -
public static void ActivateLastView(this IRegionManager regionManager, string regionName)
Activates the last active view in the region; if none are active, activates the last view in the region’s view collection. -
public static void AddViewToRegion(this IRegionManager regionManager, string regionName, object view)
Adds a view to the region without activating. -
public static void AddViewToRegionActivate(this IRegionManager regionManager, string regionName, object view)
Adds and immediately activates the view. -
public static void RemoveView(this IRegionManager regionManager, object view)
Removes the view from all regions it belongs to. -
public static void RemoveView(this IRegionManager regionManager, string regionName, object view)
Deactivates and removes the view from the specified region.
3. Invariants
-
View-ViewModel Coupling: Each view must have its
DataContextset to the resolved viewmodel instance before being added to a region. This is enforced in allAddView/AddViewAsyncimplementations. -
Interface-Based Resolution: Views and viewmodels are resolved using Unity via their interface types (
ViewInterfaceType,ViewModelInterfaceType) stored inViewDefinition. Concrete types are never directly referenced in the manager logic. -
Lifecycle Order: For view initialization,
Initialize/InitializeAsyncis called after the view is added to the region and activated. For view removal,Cleanup/CleanupAsyncis called before deactivation/removal. -
Single Instance Enforcement: When
allowMultipleInstancesisfalse,AddViewAsyncattempts to activate an existing view viaActivateViewIfExistsand returns early if successful—preventing duplicate view instances. -
Region Context Updates: After view addition,
IShellViewModel.ContextMainRegion(andContextNavigationRegioninDataProRegionManager) is set to the view instance. This implies thatIShellViewModelmust be resolvable and must expose these properties. -
Exception Handling: All
AddViewAsync,RefreshViewAsync, andRefreshViewmethods catch exceptions and publish them viaRaiseNotificationevent usingUtility.GetAllErrorMessages(ex).
4. Dependencies
Module Dependencies
- Prism Library:
Prism.Regions(IRegionManager,IRegionCollection,IRegion)Prism.Events(IEventAggregator,EventBaseforRaiseNotification)
- Unity Container:
Unity.IUnityContainerfor DI-based view/viewmodel resolution. - Custom Types:
DTS.Common.Base.IBaseView,DTS.Common.Base.IBaseViewModel(forDataContext,Initialize,Cleanup,Activated, etc.)DTS.Common.Interface.IShellViewModel(forContextMainRegion,ContextNavigationRegion)DTS.Common.Events.RaiseNotification,DTS.Common.Classes.NotificationContentEventArgs,DTS.Common.Utility(for error reporting)DTS.Common.RegionNames(for region name constants likeMainRegion,NavigationRegion)
Module Dependents
- Any module or layer requiring region management (e.g., shell initialization, module bootstrapping) depends on one of the
IDTS*RegionManagerinterfaces. - The
RegionManagerExtensionsare used by internal code and other modules to perform region operations beyond the core manager interface.
5. Gotchas
-
ViewTypeProperty Missing: TheViewDefinitionclass has a commented-outViewTypeproperty and constructor overload. Only interface types are used for resolution—no concrete view/viewmodel types are supported in the current implementation. -
Redundant Manager Implementations:
DataProRegionManager,DTSRegionManager, andDTSViewRegionManagerare nearly identical in implementation. Differences are minimal (e.g.,NavigationRegionhandling only inDataProRegionManager). This suggests possible technical debt or legacy branching. -
GetShellViewModelByRegionNameIncomplete: All managers have aGetShellViewModelByRegionNamemethod that currently only resolvesIShellViewModelregardless of region. The commented-out logic suggests future expansion (e.g., per-region shell view models), but it is unused in production code. -
RefreshView/RefreshViewAsyncScope: These methods iterate over all regions to find views matchinginterfaceForView. This may cause unintended refreshes if the same view interface is used in multiple regions. -
RemoveViewsvsClearRegion:RemoveViews(extension method) only removes active views, whereasClearRegionremoves all views. This distinction may lead to confusion. -
AddToRegion/RequestNavigateNot Implemented: All three region manager classes implementIRegionManagerbut throwNotImplementedExceptionfor manyIRegionManagermembers (e.g.,AddToRegion,RegisterViewWithRegion,RequestNavigate). These are not part of the intended public API and should not be used. -
Async Initialization Assumption:
InitializeAsyncandCleanupAsyncare assumed to exist onIBaseViewModel. If a concrete viewmodel only implements synchronous methods, runtime errors will occur. -
ReferenceEqualsfor ViewModel Matching:RemoveViewusesReferenceEqualsto match viewmodels. This is safe only if viewmodels are singleton-per-region or not shared across views; otherwise, it may fail to find the correct view. -
No Validation of
ViewDefinitionInputs: No null checks or validation ofregionName,viewInterfaceType, orviewModelInterfaceTypeinViewDefinitionconstructor or manager methods—could lead to runtime resolution failures.