MVVM pattern guidance: Building WPF frontend with JSON REST API integration

I need assistance in designing a proper MVVM architecture for my WPF application. This app connects to a JSON REST API for data handling and I want to avoid using quick solutions like DataGrid with DataSet.

I’m employing the Caliburn.Micro framework to learn clean architectural patterns. The backend API is already established, but I’m responsible for creating the entire frontend and data access layer.

In traditional methods, tracking changes with DataSet.Tables and examining RowState values to identify which records need to be inserted, updated, or deleted is straightforward. However, this approach does not align well with the MVVM binding patterns.

Here are a few strategies I’m considering:

  1. Generate backup copies of my entity collections when opening edit forms and compare these with modified collections during save operations.
  2. Introduce tracking properties such as HasChanges and IsNewRecord in my ViewModels.
  3. Utilize ObservableCollection with CollectionChanged events to keep track of deletions.
public class CustomerViewModel : PropertyChangedBase
{
    private string _name;
    private bool _hasChanges;
    
    public string Name 
    {
        get => _name;
        set 
        {
            _name = value;
            HasChanges = true;
            NotifyOfPropertyChange();
        }
    }
    
    public bool HasChanges 
    {
        get => _hasChanges;
        set => Set(ref _hasChanges, value);
    }
}

What’s the recommended pattern for tracking changes in MVVM applications that require batch updates to the API?

the memento pattern works gr8 for this. just snapshot ur viewmodel’s original state when u load data, then compare it to the current state when saving. way simpler than change tracking services and u dont need to mess with ur models.

have u considered using entity framework’s change tracker for your viewmodels? also, are u planning for optimistic concurrency? and what happens when users leave mid-edit - will u save the changes or just discard them?

In my experience with WPF and REST APIs, a highly effective strategy combines your second and third suggestions. Rather than manually tracking changes within your ViewModels, consider implementing a dedicated change tracking service that monitors your domain models through property change notifications.

You can create a ChangeTracker class that maintains collections for newly added, modified, and deleted entities. By utilizing INotifyPropertyChanged in your entities, the tracker can automatically detect modifications. For deletions, I recommend using soft deletes—mark entities as deleted instead of permanently removing them from collections.

This approach ensures that change tracking logic remains independent of your ViewModels, allowing them to concentrate solely on presentation aspects. The change tracker will take care of identifying what needs to be synchronized with your API. When it’s time to save, you’ll have clear, organized lists detailing the necessary operations.

This pattern is highly scalable and integrates seamlessly with dependency injection frameworks, making your application more testable and maintainable than embedding tracking logic within the ViewModels.