C#C
C#3y ago
Indeed

✅ WPF Store Singleton Action PropertyChanged optimization

Hi!
I have a singleton in my app responsible for weather management
currently each of my properties on the object has an Action<T> where T is property's type, however that means i have to write an action for each property
is there a better way?
public class AppStore {
    private List<WeatherData> _weatherForecasts;
    public IEnumerable<WeatherData> WeatherForecasts => _weatherForecasts;
    public Location? Location { get; private set; }
    public event Action<Location?> LocationChanged;
    public event Action<IEnumerable<WeatherData>> WeatherForecastsChanged;

    public bool IsLoading { get; internal set; }
    public bool IsFetching { get; internal set; }
    public event Action<bool> LoadingChanged;
    public event Action<bool> FetchingChanged;
    
    private async Task FetchWeather() {
        if (Location == null) {
            _weatherForecasts.Clear();
            WeatherForecastsChanged?.Invoke(_weatherForecasts);
            return;
        }

        SetLoading(true);
        IEnumerable<WeatherData> weather = await _weatherProvider.GetWeatherAsync(Location, DateTime.Now, DateTime.Now.AddDays(3));
        _weatherForecasts = weather.ToList();
        WeatherForecastsChanged?.Invoke(_weatherForecasts);
        SetLoading(false);
    }

    private void SetLoading(bool value) {
        if (IsLoading == value) return;

        IsLoading = value;
        LoadingChanged?.Invoke(value);
    }

    //...
}
Was this page helpful?