Hi,
We're designing a WPF / MVVM application that allows the user to search and manipulate contact records.
We have a MainViewModel that contains an observable collection of ContactViewModel objects, each one of which wraps a Contact entity returned from our business layer. The UI displays these in a list, with the SelectedItem property bound to a corresponding SelectedContact property on the MainViewModel.
We'll also have a button or something where the command is bound to a 'ProcessContact' ICommand exposed by MainViewModel.
ProcessContact needs to takes the selected contact and do something with it, it doesn’t really matter what.
My question is: What would be the correct way of getting at the underlying Contact object wrapped by the selected ContactViewModel? I could just expose a Contact property on my view model, but that then means the view could potentially bind to properties directly off the model.
I've found myself passing ViewModel instances around a lot, which feels wrong when what I really want is the entity it's wrapping.
Am I missing something obvious?
Edit: A couple of suggestions thrown around by colleagues:
Expose the entity as a protected property on the ViewModel, which would stop the view binding to it (assuming the view classes are in a separate assembly)
Stop trying to access the model altogether. If we want to process the underlying entity in some way, we call a method on the ViewModel. In my example we might have a .Process method off ContactViewModel. ( ‘SelectedContact.Process()’ )
The second option feels like a better solution to me, but not sure if we should be putting that much logic into the ViewModel (but if not there, then where?)