Skip to content

Commit df7e6f6

Browse files
Address PR feedback: public mock ViewModels and caching
- Changed mock ViewModels from internal to public to ensure XAML type resolution compatibility. - Implemented caching for Overview, Details, and Settings ViewModels to preserve state between sidebar navigation. - Added disposal logic for cached ViewModels to prevent resource leaks. - Updated SelectedContentViewModel type to ViewModelBase. Co-authored-by: efargas <9705611+efargas@users.noreply.github.com>
1 parent 7736524 commit df7e6f6

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

docs/templates/ui-integration/FeatureViewModel.template.cs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ public class [FEATURE_NAME]ViewModel : ViewModelBase, IDisposable
2525
private ObservableCollection<string> _categories = new();
2626

2727
// Main content state
28-
private object? _selectedContentViewModel;
28+
private [FEATURE_NAME]OverviewViewModel? _overviewVm;
29+
private [FEATURE_NAME]DetailsViewModel? _detailsVm;
30+
private [FEATURE_NAME]SettingsViewModel? _settingsVm;
31+
private ViewModelBase? _selectedContentViewModel;
2932
private string _statusMessage = string.Empty;
3033
private bool _isLoading;
3134

@@ -90,7 +93,7 @@ public string? SelectedCategory
9093
/// Gets or sets the ViewModel for the main content area.
9194
/// This switches based on sidebar selection.
9295
/// </summary>
93-
public object? SelectedContentViewModel
96+
public ViewModelBase? SelectedContentViewModel
9497
{
9598
get => _selectedContentViewModel;
9699
set => this.RaiseAndSetIfChanged(ref _selectedContentViewModel, value);
@@ -169,30 +172,27 @@ private void UpdateMainContent()
169172
}
170173

171174
/// <summary>
172-
/// Creates the overview ViewModel for the main content area.
175+
/// Creates or returns the cached overview ViewModel for the main content area.
173176
/// </summary>
174-
private object Create[FEATURE_NAME]OverviewViewModel()
177+
private [FEATURE_NAME]OverviewViewModel Create[FEATURE_NAME]OverviewViewModel()
175178
{
176-
// Create and return the overview ViewModel
177-
return new [FEATURE_NAME]OverviewViewModel();
179+
return _overviewVm ??= new [FEATURE_NAME]OverviewViewModel();
178180
}
179181

180182
/// <summary>
181-
/// Creates the details ViewModel for the main content area.
183+
/// Creates or returns the cached details ViewModel for the main content area.
182184
/// </summary>
183-
private object Create[FEATURE_NAME]DetailsViewModel()
185+
private [FEATURE_NAME]DetailsViewModel Create[FEATURE_NAME]DetailsViewModel()
184186
{
185-
// Create and return the details ViewModel
186-
return new [FEATURE_NAME]DetailsViewModel();
187+
return _detailsVm ??= new [FEATURE_NAME]DetailsViewModel();
187188
}
188189

189190
/// <summary>
190-
/// Creates the settings ViewModel for the main content area.
191+
/// Creates or returns the cached settings ViewModel for the main content area.
191192
/// </summary>
192-
private object Create[FEATURE_NAME]SettingsViewModel()
193+
private [FEATURE_NAME]SettingsViewModel Create[FEATURE_NAME]SettingsViewModel()
193194
{
194-
// Create and return the settings ViewModel
195-
return new [FEATURE_NAME]SettingsViewModel();
195+
return _settingsVm ??= new [FEATURE_NAME]SettingsViewModel();
196196
}
197197

198198
/// <summary>
@@ -246,6 +246,12 @@ protected virtual void Dispose(bool disposing)
246246
if (disposing)
247247
{
248248
_disposables.Dispose();
249+
250+
// Dispose cached ViewModels if they implement IDisposable
251+
(_overviewVm as IDisposable)?.Dispose();
252+
(_detailsVm as IDisposable)?.Dispose();
253+
(_settingsVm as IDisposable)?.Dispose();
254+
249255
_logger.LogDebug("[FEATURE_NAME]ViewModel disposed");
250256
}
251257

0 commit comments

Comments
 (0)