We are developing a WPF / MVVM application that allows the user to search and process contact records.
We have a MainViewModel that contains an observable collection of ContactViewModel objects, each of which wraps the Contact object returned from our business layer. The user interface displays them in a list, and the SelectedItem property is associated with the corresponding SelectedContact property in MainViewModel.
We will also have a button or something where the command is bound to the ICommand ProcessContact opened by MainViewModel.
ProcessContact should accept the selected contact and do something with it, it doesn't really matter.
My question is: what would be the correct way to access the base Contact object wrapped by the selected ContactViewModel? I could just open the Contact property in my view model, but that means the view can potentially bind properties directly to the model.
I found myself looking at instances of the ViewModel very often, which seems wrong when what I really want is the entity that it wraps.
Am I missing something obvious?
Edit: several suggestions thrown by colleagues:
Present the object as a protected property in the ViewModel, which will stop the view from binding to it (if the presentation classes are in a separate assembly)
Do not try to fully access the model. If we want to somehow process the underlying object, we call the method in the ViewModel. In my example, we can have a .Process method with ContactViewModel. ('SelectedContact.Process ())
The second option seems to be the best solution for me, but I'm not sure if we should embed this logic in the ViewModel (but if not, then where?)
source share