MVVM-Light: Best Practice for Wrapping Model Properties in a ViewModel where Set <T> cannot be used

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 purpose of this method is to encapsulate various side effects that can result from setting a property (property change notification, messaging, etc.).

I want to show properties of my model through properties in ViewModel, delegating to getter / setter models. For example, for a Student (model), I am currently implementing a wrapper for Student.Name in the ViewModel as follows:

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 the SetModelBase Set function like this to access 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);  }
    }
}

      

This is illegal as you cannot pass an object property by reference. Can anyone think of how to use the Set architecture while still being able to refer to the Model for use as a backing store?

+3


source to share





All Articles