MVVM
View ↔ ViewModel ↔ Model: the ViewModel exposes bindable state and commands.
Context
Apps with declarative binding (WPF, MAUI, Avalonia, Blazor).
Solution
The View binds to the ViewModel, which exposes observable properties and ICommands. The Model represents the domain.
public class OrderViewModel : INotifyPropertyChanged
{
private string _customer = "";
public string Customer { get => _customer; set { _customer = value; OnChange(); } }
public ICommand SaveCommand { get; }
public OrderViewModel(IOrderService svc) =>
SaveCommand = new RelayCommand(async () => await svc.SaveAsync(new Order(Customer)));
public event PropertyChangedEventHandler? PropertyChanged;
private void OnChange([CallerMemberName] string? n = null) =>
PropertyChanged?.Invoke(this, new(n));
}Tradeoffs
| Pro | Con |
|---|---|
| Clear separation of concerns | Notification boilerplate |
| Testable without bringing up UI | Bindings difficult to debug |
#architecture #ui #wpf #maui