I am trying to implement MVVM in a new project (based on WPF & Prism) in which almost every screen (view) will have a search screen shown first where the user may choose to add/edit records.
The search screen takes a refernce to the VM which implements one of my interfaces ISearchProvider (which has one method IEnumerable Search(string criteria)).
Should this Search method return
- a collection of VM’s (with their models properly set) which will get shown in the grid in the search screen or
- just Models so that when the user chooses to edit a record (Model), the chosen model is given to an instance of a VM which then gets edited or
- some third option?
Whatever you plan to show to user should be wrapped with View Model.
You mentioned that user will select model to edit. This should be view model; user will select model (entity) to edit, but what he’ll be shown (or what your view will see) should be list of view models representing some entity. User (or in this case view) should not need any knowledge of actual model.
Now, the view model should not be doing any search itself. It should delegate search to component that’s responsibility is to perform search and return result (be it some kind of DAO, your custom tool – options may vary). What is more, the same view model should also be responsible for “feeding” view with data that may or may not arrive from search.
To give it code example:
Also, it’s probably good idea to make ISearchProvider interface expose single
Searchcommand. This way, binding from view is really simple.To sum up, 2 simple points: