-1

Imagine a Patient model in WPF where one of the properties are "Temperature". Now one doctor might prefer the temperature in Celsius while another might prefer Fahrenheit. If the temperature property needs to change in UI as the doctor changes preference, I guess the model for Patient would have to subscribe to an event. Something like this:

public Patient()
{
    Temperature.Instance.PropertyChanged += TemperatureChanged;
}
~Patient()
{
    Temperature.Instance.PropertyChanged -= TemperatureChanged;
}

But while this would work, as you can infer, we are subscribing using a static class inside the Patient model. Are there some more elegant way of doing this?

Even though the Temperature class is used in a static context, I'm worried about the models not unsubscribing these events (only solution I know of is in the destructor). And that it might lead to degraded performance as the application runs. Is this concern real?

My only alternative right now, is to require the view to reload when preferences like this changes...

Blacktempel
  • 3,935
  • 3
  • 29
  • 53
Werner
  • 1,229
  • 1
  • 10
  • 24
  • 1
    A common approach is to have a MultiBinding with a Binding to the view model property (with a value in standard units, e.g. Kelvin) and a Binding to a display unit. If either of the two changes, the UI is updated. – Clemens Aug 21 '18 at 09:25
  • Yes that might work actually, I shall investigate at once, thanks Clemens! Btw to others - it would be nice/polite to leave a comment and not just down vote. For future reference I would like to know how to get better, thanks. – Werner Aug 21 '18 at 09:33

1 Answers1

1

"Changing preference" is the same thing as setting a property. You could for example define a Units property that can be set to either Celsuis or Fahrenheit and then raise the PropertyChanged event for the property that returns the temperature, e.g.:

public class Patient : INotifyPropertyChanged
{
    private Units _units;
    public Units Units
    {
        get { return _units; }
        set
        {
            _units = value;
            NotifyPropertyChanged();
            NotifyPropertyChanged(nameof(FormattedTemperature));
        }
    }

    private double _temperature;
    public double Temperature
    {
        get { return _temperature; }
        set
        {
            _temperature = value;
            NotifyPropertyChanged();
            NotifyPropertyChanged(nameof(FormattedTemperature));
        }
    }

    public string FormattedTemperature =>
        _temperature.ToString() + (_units == Units.Celsuis ? " C" : " F");


    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") =>
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public enum Units
{
    Celsuis,
    Fahrenheit
}

In the view you bind to the FormattedTemperature property.

It makes no sense to implement a finalizer that unsubscribes from a managed event by the way.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • "It makes no sense to implement a finalizer that unsubscribes from a managed event" - why? Because it is removed from the handler list automatically? On topic: The thing is, that the change, say from Fahrenheit to Celsius, would be in a "SettingsDialog" like a global setting. That is why I was considering that every Patient class should listen to events from that "SettingsDialog" change. I would have a list of perhaps 1000 patients... – Werner Aug 21 '18 at 13:35
  • 1
    An event handler is not removed automatically but the consumers of your class has no way to call a finializer. Implement the `IDisposable` interface if you want to perform some cleanup when your object is no longer being used. On topic: So listen to the dialog change and set the property? – mm8 Aug 21 '18 at 13:38
  • Yes listen and set. But how to listen? Hence my original idea of having the "Temperature" property as static and subscribe to that. There might be 10 views active that needs to know about the change. I'm using Galasoft so a more testable solution could be to use the Galasoft event/command system to notify... – Werner Aug 27 '18 at 10:51
  • What's the problem of subscribing to the PropertyChanged event as I suggested? – mm8 Aug 27 '18 at 13:37