Files
DP44/docs/ai/Common/DTS.Common/Base/ViewModel.md

63 lines
3.6 KiB
Markdown
Raw Normal View History

2026-04-17 14:55:32 -04:00
---
source_files:
- Common/DTS.Common/Base/ViewModel/ViewModelBase.cs
- Common/DTS.Common/Base/ViewModel/BaseViewModel.cs
generated_at: "2026-04-17T15:41:12.237629+00:00"
model: "zai-org/GLM-5-FP8"
schema_version: 1
sha256: "4dad13537847aaee"
---
# Documentation: DTS.Common.Base ViewModel Classes
## 1. Purpose
This module provides two abstract base classes for the Model-View-ViewModel (MVVM) pattern within a WPF application. `ViewModelBase<T>` serves as a foundational class for ViewModels that manage their own commands and actions, inheriting from `DependencyObject` to support WPF dependency properties. `BaseViewModel<TModel>` provides a more feature-rich base class with Prism integration, offering built-in support for event aggregation, dependency injection via Unity, interaction requests, and lifecycle management (initialization, activation, cleanup). Both classes implement property change notification and track busy/dirty states for UI binding.
---
## 2. Public Interface
### ViewModelBase<T>
**Type Parameters:** `T` - The type of the Model object.
| Member | Signature | Description |
|--------|-----------|-------------|
| `Model` | `public object Model { get; set; }` | Gets or sets the Model object. Note: This is typed as `object`, not `T`. |
| `IsBusy` | `public bool IsBusy { get; protected set; }` | Indicates whether the object is executing an asynchronous process. |
| `IsDirty` | `public virtual bool IsDirty { get; protected set; }` | Indicates whether the Model has been changed. |
| `ErrorOccurred` | `public virtual event EventHandler<ErrorEventArgs> ErrorOccurred` | Event raised when an error occurs during processing. |
| `PropertyChanged` | `public virtual event PropertyChangedEventHandler PropertyChanged` | Event raised when a property changes. |
**Protected Abstract Methods (must be implemented by derived classes):**
| Method | Signature | Description |
|--------|-----------|-------------|
| `InitializeAsync` | `protected abstract Task<T> InitializeAsync()` | Implement async initialization; the result sets the Model property. |
| `DoRefresh` | `protected abstract void DoRefresh(Func<T> factoryMethod)` | Creates or retrieves a new Model instance via a static factory method. |
| `OnError` | `protected abstract void OnError(Exception error)` | Raises the `ErrorOccurred` event when an error occurs. |
| `OnModelChanged` | `protected abstract void OnModelChanged(T oldValue, T newValue)` | Invoked when the Model changes; allows unhooking/hooking event handlers. |
| `OnPropertyChanged` | `protected abstract void OnPropertyChanged(string propertyName)` | Raises the `PropertyChanged` event. |
---
### BaseViewModel<TModel>
**Type Parameters:** `TModel` - The type of the Model object (must be a reference type: `where TModel : class`).
**Constructors:**
| Constructor | Signature | Description |
|-------------|-----------|-------------|
| Default | `protected BaseViewModel()` | Parameterless constructor. |
| DI Constructor | `protected BaseViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer)` | Initializes aggregator, container, and calls `CreateCommands()`. Note: `regionManager` parameter is unused. |
**Properties:**
| Property | Signature | Description |
|----------|-----------|-------------|
| `Aggregator` | `protected IEventAggregator Aggregator { get; private set; }` | Prism event aggregator for pub/sub messaging. |
| `Container` | `protected IUnityContainer Container { get; private set; }` | Unity DI container. |
| `Model` | `public TModel Model { get; set; }` | The strongly-typed model object. |
| `Confirmation