© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
11 replies
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);
    }

    //...
}
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);
    }

    //...
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ WPF IEnumerable PropertyChanged()
C#CC# / help
3y ago
❔ Settings model as a singleton in WPF
C#CC# / help
3y ago
❔ ? WPF Build Action(Resource)
C#CC# / help
3y ago
✅ Singleton Class
C#CC# / help
3y ago