Skip to content

Persistent Services

The most powerful feature of Stores. Register an INotifyPropertyChanged class as a persistent service, and every property change is automatically written to the backing store. When the app restarts, the object is rehydrated from the store.

  1. Create a class that implements INotifyPropertyChanged:

    [Reflector] // Optional but recommended — removes reflection for AOT safety
    public partial class AppSettings : INotifyPropertyChanged
    {
    string theme = "light";
    public string Theme
    {
    get => theme;
    set { theme = value; OnPropertyChanged(); }
    }
    bool notificationsEnabled = true;
    public bool NotificationsEnabled
    {
    get => notificationsEnabled;
    set { notificationsEnabled = value; OnPropertyChanged(); }
    }
    // INotifyPropertyChanged implementation
    public event PropertyChangedEventHandler? PropertyChanged;
    void OnPropertyChanged([CallerMemberName] string? name = null)
    => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
  2. Register as a persistent service:

    // Uses the default "settings" store
    builder.Services.AddPersistentService<AppSettings>();
    // Or target a specific store
    builder.Services.AddPersistentService<SecureSettings>("secure");
  3. Inject and use — changes are persisted automatically:

    public class SettingsViewModel(AppSettings settings)
    {
    public AppSettings Settings => settings;
    public void ToggleNotifications()
    {
    // This change is automatically persisted to the store
    Settings.NotificationsEnabled = !Settings.NotificationsEnabled;
    }
    }