Files

179 lines
8.0 KiB
Markdown
Raw Permalink Normal View History

2026-04-17 14:55:32 -04:00
---
source_files:
- DataPRO/Modules/SystemSettings/DBImportExport/ViewModel/DBViewModel.cs
generated_at: "2026-04-16T04:40:45.205601+00:00"
model: "Qwen/Qwen3-Coder-Next-FP8"
schema_version: 1
sha256: "197f884be4481037"
---
# ViewModel
## Documentation: `DBViewModel` (`DBImportExport` Module)
---
### 1. **Purpose**
The `DBViewModel` class serves as the view model for database import and export functionality within the DataPRO application. It acts as a bridge between the UI (via `IDBImportView` and `IDBExportView`) and the underlying data layer, handling user interactions for selecting import/export files, validating data, and performing file I/O operations using XML strings. Due to architectural constraints (legacy DataPRO objects reside in a separate project), its functionality is currently limited to managing XML string data flow—*not* direct database manipulation.
---
### 2. **Public Interface**
#### **Constructor**
```csharp
public DBViewModel(
IDBImportView importView,
IDBExportView exportView,
IRegionManager regionManager,
IEventAggregator eventAggregator,
IUnityContainer unityContainer)
```
- Initializes the view model with views, region/event management, and dependency injection containers.
- Sets `DataContext` of both views to `this`.
- Subscribes to `RaiseNotification` and `BusyIndicatorChangeNotification` events.
#### **Lifecycle Methods (No-op)**
All lifecycle methods currently have empty implementations:
- `void Cleanup()`
- `Task CleanupAsync()`
- `void Initialize()`
- `void Initialize(object parameter)`
- `void Initialize(object parameter, object model)`
- `Task InitializeAsync()`
- `Task InitializeAsync(object parameter)`
- `void Activated()`
#### **Event Handlers (Internal)**
- `private void OnBusyIndicatorNotification(bool eventArg)`
Sets `IsBusy` to `eventArg`.
- `private void OnRaiseNotification(NotificationContentEventArgs eventArgsWithTitle)`
Converts `NotificationContentEventArgs` to `Notification` and raises `NotificationRequest`.
#### **Public Methods**
- `public void Export()`
Writes `ExportData` (XML string) to `ExportFileName` using `Encoding.Unicode`.
**Validation checks**:
- Fails early if `ExportFileName` or `ExportData` is null/whitespace, publishing a `ShowStatus` event with failure message.
- If `ExportFileName` already exists, deletes/moves it via `FileUtils.DeleteFileOrMove`.
- Writes file using `File.WriteAllText`.
#### **Interaction Requests (Public)**
- `public InteractionRequest<Notification> NotificationRequest { get; }`
Used to trigger notification popups (e.g., success/error messages).
- `public InteractionRequest<Confirmation> ConfirmationRequest { get; }`
Reserved for confirmation dialogs (not used in current implementation).
#### **Commands (Public)**
- `public DelegateCommand ImportBrowseCommand { get; }`
Opens an `OpenFileDialog` to select an import file. Sets `ImportFileName` and updates `ImportStatusText`.
- `public DelegateCommand ExportBrowseCommand { get; }`
Opens a `SaveFileDialog` to set `ExportFileName`.
#### **Properties (Public)**
- `IDBImportView ImportView { get; set; }`
Reference to the import view instance.
- `IDBExportView ExportView { get; set; }`
Reference to the export view instance.
- `bool IsDirty { get; private set; }`
Always `false` (never set).
- `bool IsBusy { get; set; }`
Bound to busy indicator; updated via `OnBusyIndicatorNotification`.
- `bool IsMenuIncluded { get; set; }`
Property exposed for UI binding (value never read/written beyond property setter).
- `bool IsNavigationIncluded { get; set; }`
Same as above.
- `string HeaderInfo { get; }`
Hardcoded to `"MainRegion"`.
- `string ImportData { get; set; }`
XML string for imported data (no import logic implemented—only storage).
- `string ExportData { get; set; }`
XML string for exported data (no export logic implemented—only storage).
- `string ExportFileName { get; set; }`
Full path to export file.
- `string ImportFileName { get; set; }`
Full path to import file.
- `string ImportStatusText { get; set; }`
Status message for import UI (e.g., warning about database overwrite).
- `event PropertyChangedEventHandler PropertyChanged`
Implements `INotifyPropertyChanged`.
- `void OnPropertyChanged(string propertyName)`
Raises `PropertyChanged` event.
#### **Nested Type**
- `public enum PropertyNames`
Lists property names used in `OnPropertyChanged`:
`ExportFileName`, `ImportFileName`, `ImportStatusText`.
---
### 3. **Invariants**
- `Export()` **requires**:
- `ExportFileName` must be non-null/non-whitespace.
- `ExportData` must be non-null/non-whitespace.
- `Export()` **guarantees**:
- If file exists at `ExportFileName`, it is deleted/moved before writing.
- File is written using `Encoding.Unicode`.
- `ImportBrowseMethod()`:
- Uses `OpenFileDialog` with `CheckFileExists = true` and `CheckPathExists = true`.
- Sets `ImportFileName` only if dialog succeeds.
- `ExportBrowseMethod()`:
- Uses `SaveFileDialog` with `OverwritePrompt = false`.
- Sets `ExportFileName` only if dialog succeeds.
- `IsBusy` is updated synchronously via event subscription (on publisher thread).
- `ImportStatusText` is updated *only* in `ImportBrowseMethod()` (no import operation is triggered by this view model).
---
### 4. **Dependencies**
#### **Imports/Usings**
- `System.ComponentModel`, `System.ComponentModel.Composition`, `System.Threading.Tasks`
- `DTS.Common.Events` (e.g., `RaiseNotification`, `BusyIndicatorChangeNotification`, `ShowStatus`, `StatusInfo`)
- `Prism.Events`, `Prism.Regions`, `Prism.Commands`
- `Unity` (for `IUnityContainer`)
- `DTS.Common.Interface` (e.g., `IDBImportView`, `IDBExportView`, `IDBViewModel`)
- `DTS.Common.Utils` (e.g., `FileUtils`)
- `DTS.Common.Interactivity` (e.g., `InteractionRequest<T>`)
- `System.Text` (for `Encoding`)
- `System.Windows.Forms` (for `OpenFileDialog`, `SaveFileDialog`)
#### **Consumed Types**
- `IDBImportView`, `IDBExportView`, `IDBViewModel` (from `DBImportExport` namespace)
- `NotificationContentEventArgs`, `StatusInfo`, `StatusInfo.StatusState`
- `FileUtils.DeleteFileOrMove`
- `Resources.StringResources` (for UI strings: `ImportFileBrowse_Filter`, `ExportFileBrowse_Filter`, `ExportFileName_Empty`, `ExportFileData_Empty`)
#### **Depended Upon By**
- Prism-based shell/shell regions (via `IRegionManager`).
- Event publishers (e.g., `RaiseNotification`, `BusyIndicatorChangeNotification`).
- Views (`IDBImportView`, `IDBExportView`) bound to this view model.
---
### 5. **Gotchas**
- **No actual import/export logic**:
`ImportData` and `ExportData` are *only* storage properties. No code parses, validates, or processes the XML strings. Import/export is purely file I/O—*no database interaction* occurs in this class.
- **`Cleanup()`/`Initialize()` are no-ops**:
Lifecycle methods exist for interface compliance but perform no work.
- **`IsDirty` is never set**:
Always `false`; likely unused or incomplete.
- **`IsMenuIncluded`/`IsNavigationIncluded` unused**:
Properties exist but have no observed consumers in this file.
- **Hardcoded `HeaderInfo`**:
Always `"MainRegion"`—no dynamic behavior.
- **`LogDummyFunc` does nothing**:
Passed to `FileUtils.DeleteFileOrMove` but never logs (silently ignores errors).
- **No import operation triggered**:
`ImportFileName` is set via browse dialog, but no `Import()` method exists to process the file.
- **`Export()` writes directly without user confirmation**:
Overwrites existing files silently (only uses `FileUtils.DeleteFileOrMove`).
- **`NotificationRequest` conversion quirk**:
`OnRaiseNotification` constructs a new `NotificationContentEventArgs` with empty strings for `Image` and `CommandParameter`—may lose data if callers rely on them.
- **Thread safety**:
`IsBusy` updates occur on `PublisherThread` (via `ThreadOption.PublisherThread`), but other properties lack thread-safety guarantees.
> **None identified from source alone** for critical bugs or anti-patterns beyond the above architectural limitations.