In my new WPF Application, I am reusing a Model class. In that model class, all the properties, in their setters, fire NotifyPropertyChanged. In my application, I do not really have a use case of firing INPC for individual property. I need to know that if I keep the current architecture where individual properties fire INPC every time they get changed, will it cause any performance implications? Does it make sense to have individual properties fire INPC when it is not required?
Generally speaking, anytime you are running a piece of code that you don't have to, you are potentially causing performance issues.
As a rule of thumb, when you write your setters for your properties instead of just setting your backing field and raising the change event, you should check the equality before you notify, thus avoiding unnecessary updates.
for example:
public int MyInteger
{
get { return this._myInteger; }
set {
if ( value != this._myInteger )
{
this._myInteger = value;
RaiseChangedEvent("MyInteger");
}
}
you should also check for attached events in your RaiseChangedEvent
methods, so if there are no listeners, you don't throw a null reference exception and you don't unnecessarily make a call:
private void RaiseChangedEvent(string propertyName)
{
var changedHandler = this.PropertyChanged ;
if ( changedHandler != null )
changedHandler(this, new PropertyChangedEventArgs( propertyName ) );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With