Search code examples
c#inotifypropertychangedbreeze-sharp

How can I manually notify the Framework of property changes when using the Breeze.Sharp.BaseEntity class?


I have been using the Breeze.Sharp.BaseEntity base class at work for some time and up until now, have been ok with using their GetValue and SetValue properties for plugging into the INotifyPropertyChanged interface. However, occasionally, it is advantageous to notify of changes to multiple properties at once from a single property. Let me give you a simple example.

Let's say that we have a simple sum to perform. We have an ItemAmount property, an ItemQuantity property, and a Total property. Each time either of the ItemAmount or ItemQuantity property values change, we also want the total to update. Traditionally, we could do that like this:

public double ItemAmount
{
    get { return _itemAmount; }
    set 
    {
        _itemAmount = value;
        NotifyPropertyChanged("ItemAmount");            
        NotifyPropertyChanged("Total");
    }
}

public int ItemQuantity
{
    get { return _itemQuantity; }
    set 
    {
        _itemQuantity = value;
        NotifyPropertyChanged("ItemQuantity");            
        NotifyPropertyChanged("Total");
    }
}

public double { get => ItemAmount * ItemQuantity; }

This would make the Framework also update the value of the Total property as either property changed, as required. However, I can find no such way to call the INotifyPropertyChanged interface using their base class, as unbelievably, they seem to have hidden access to their implementation of it.

So, my question is How can I manually notify the Framework of property changes when using the Breeze.Sharp.BaseEntity class?

Is there some way to connect to their implementation that I haven't worked out yet? When I added my own implementation, it didn't seem to connect with the Framework and did not notify of changes successfully. Many thanks in advance.


Solution

  • There's no simple answer, but the way that I found to do this, was to clone the Breeze.Sharp repository and to make the following changes. In the Breeze.Sharp.Standard solution, open the EntityAspect.cs class and simply add your desired method to raise the PropertyChanged event handler, that is already declared in that class:

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

    Then, you can call it from your BaseEntity class like this:

    public int ItemQuantity
    {
        get => GetValue<int>();
        set
        {
            SetValue(value);
            EntityAspect.NotifyPropertyChanged(nameof(Total));
        }
    }
    

    Note that changes to the ItemQuantity property are raised inside the SetValue method.