Sobes.tech
Middle

What is the MVVM pattern?

sobes.tech AI

Answer from AI

MVVM (Model-View-ViewModel) is an architectural pattern used in developing user interfaces, especially in WPF, Xamarin.Forms, and Avalonia. It divides the application into three interconnected layers:

  • Model: Represents the business logic and data of the application. The Model does not know about the View and ViewModel.
  • View: Represents the user interface. It displays data from the ViewModel and forwards user actions to the ViewModel. The View does not contain logic for data processing or decision-making.
  • ViewModel: Acts as an intermediary between the Model and the View. It provides data from the Model in a format suitable for the View and handles commands from the View. The ViewModel encapsulates presentation logic and does not have a direct reference to the View.

Key principles of MVVM:

  • Separation of Concerns: Each layer has a clear role.
  • Data Binding: The View binds to the ViewModel through data bindings, allowing the View to automatically update when data in the ViewModel changes and vice versa.
  • Commands: User actions in the View (e.g., button clicks) are passed to the ViewModel via commands, ensuring loose coupling between the View and ViewModel.
  • Observable Collections: Used for data collections that need to be updated in the View upon changes, using ObservableCollection.

Advantages of MVVM:

  • Improved testability (the ViewModel is easily testable without UI).
  • Simplified UI development (designers can work on the View independently of the ViewModel logic).
  • Better code maintainability.
  • Reuse of ViewModels across different Views (e.g., for mobile and desktop applications).

Example structure:

// Model
public class User
{
    public string Name { get; set; }
    public int Age { get; set; }
}

// ViewModel
using System.ComponentModel; // For INotifyPropertyChanged
using System.Runtime.CompilerServices; // For CallerMemberName

public class UserViewModel : INotifyPropertyChanged
{
    private User _user;

    public UserViewModel(User user)
    {
        _user = user;
    }

    public string Name
    {
        get { return _user.Name; }
        set
        {
            if (_user.Name != value)
            {
                _user.Name = value;
                OnPropertyChanged(); // Notify View of change
            }
        }
    }

    public int Age
    {
        get { return _user.Age; }
        set
        {
            if (_user.Age != value)
            {
                _user.Age = value;
                OnPropertyChanged();
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

// View (XAML in WPF)
/*
<StackPanel DataContext="{Binding}"> <!-- Binding to ViewModel -->
    <TextBlock Text="{Binding Name}" />
    <TextBox Text="{Binding Name, Mode=TwoWay}" /> <!-- Two-way binding -->
    <TextBlock Text="{Binding Age}" />
    <TextBox Text="{Binding Age, Mode=TwoWay}" />
</StackPanel>
*/