We've had a bit of a discussion between myself and a few fellow colleagues in terms of the best layer to have INotifyPropertyChanged implemented. Here's the scenario:
- Data Access Layer returns a collection of simple objects
- Business Layer massages the objects and performs extra validation, etc
- UI Layer binds to objects and uses INotifyPropertyChanged for change tracking
The scenario here is that the data returned by the DA layer is the same data that is consumed by the UI layer. Do we implement INotifyPropertyChanged in the UI layer and thus have multiple near-clones of the same object throughout the different layers or do we implement INotifyPropertyChanged in the DA layer, adding unnecessary complexity to the layer that is not concerned with property change tracking.
Concerns:
- Having a separate object for DA, Business and UI layer is clean but adds a lot of redundancy
- Implementing INotifyPropertyChanged in DA feels wrong as DA is not concerned with this concept, potentially coupling DA and UI layers
My personal preference in this scenario is:
public class DaObject
{
public virtual string Value { get; set; }
}
// May or may not need a business object here. DA and UI may be enough.
public class BusinessObject : DaObject
{
// Business layer logic/validation here...
}
public class UiObject : BusinessObject, INotifyPropertyChanged
{
public override string Value
{
get { return base.Value; }
set
{
base.Value = value;
NotifyOfPropertyChanged(nameof(Value));
}
}
// INotifyPropertyChanged implementation here...
}
Thanks!