If my model implements INotifyPropertyChanged and does my VM need to?
Clarification: In the real case where you have SomeOtherProp then INotifyPropertyChanged absolutely needs to be implemented. What I am really after is how much work I have to do (replicate) for well formed models.
Example:
namespace Question
{
public interface IFoo : INotifyPropertyChanged { }
public interface IBar : INotifyPropertyChanged { }
public interface IModel : INotifyPropertyChanged
{
IFoo Foo { get; set; }
ObservableCollection<IBar> BarCollection { get; }
}
public class VM : TypeSafeViewModelBase
//Clarification: added VM base clase with typesafe override for RaisePropertyChanged
{
private IModel _model;
public VM( IModel model )
{
this._model = model;
//Clarification: added this call...
this._model.PropertyChanged += ( sender, args ) => base.RaisePropertyChanged(args.PropertyName);
//That is the one I have questions about and ultimateley what I want to avoid
}
public IFoo Foo { get { return this._model.Foo; } }
public ObservableCollection<IBar> BarCollection { get { return this._model.BarCollection; } }
//clarification: added this prop declaration
//I know this would be needed as this property is backed by a private member of this class
private string _someOtherProp;
public string SomeOtherProp
{
get { return this._someOtherProp; }
set
{
this._someOtherProp = value;
base.RaisePropertyChanged(() => this.SomeOtherProp);
}
}
}
}
Does VM need to implement INotifyPropertyChanged? And relay all the events to the V? Or do things in the V bind to lowest level objects which implement the PropertyChanged and CollectionChanged interfaces?
I can’t seem to find a definitive answer for how much glue code I need to write if I have a well formed, notifying model layer…
PS. I am developing in SL4 using Prism and Ninject if that matters. My model is mutable, stateful, and in local memory (I keep a local cache because hitting the server after every operation is not practical).
No, you don’t need to implement interface itself in your case. For example, VM doesn’t need to implement
INotifyPropertyChangeif you use RIA services business object since those already do implement it.However! Most likely you still want to do that because properties like “IsBusy”, “CanSave” and so on usually belong to VM itself and then you need interface.
Usually every application have some type of
VMBaseobject that implementsINotifyPropertyChanged,INotifyDataErrorInfoand so on. And every VM inherits from this base class