In MVVM-Light, the ViewModelBase class has a Set method:
protected bool Set<T>(ref T field, T newValue = default(T), bool broadcast = false, [CallerMemberName] string propertyName = null);
The goal of that method is to encapsulate various side effects that you might want to occur as a result of setting a property (property change notification, messaging, etc).
I want to expose the properties of my Model via properties in the ViewModel by delegating to the Model's getter/setter. E.g., for the Student (model), I am currently implementing the Student.Name wrapper in the ViewModel like this:
public class ViewModelBaseWrapped<T> : ViewModelBase where T: DomainObject {
public ViewModelBaseWrapped(T wrapped) {
m_wrappedDomainObject = wrapped;
}
private T m_wrappedDomainObject;
protected T Wrapped { get { return m_wrappedDomainObject; } }
}
public class StudentViewModel : ViewModelBaseWrapped<Student> {
public StudentViewModel(Student wrapped) : base(wrapped) { }
public string FirstName {
get { return Wrapped.FirstName; }
set { Wrapped.FirstName = value; }
}
}
However, I would prefer to use ViewModelBase's Set function as so, to get access to the additional encapsulated functionality:
public class StudentViewModel : ViewModelBaseWrapped<Student> {
public StudentViewModel(Student wrapped) : base(wrapped) { }
public string FirstName {
get { return Wrapped.FirstName; }
set { Set(ref Wrapped.FirstName, value); }
}
}
That is illegal, however, because you can't pass a property of an object by ref. Can anyone think of a way to use the Set architecture while still being able to defer to the Model for use as the backing store?