Click here to Skip to main content
15,903,012 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
Jean-Louis Leroy20-Jul-11 0:51
Jean-Louis Leroy20-Jul-11 0:51 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
SledgeHammer0120-Jul-11 6:36
SledgeHammer0120-Jul-11 6:36 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
Jean-Louis Leroy20-Jul-11 8:22
Jean-Louis Leroy20-Jul-11 8:22 
Hmm, I don't think I have a "fundamental misunderstanding". In my app the ViewModel does not store or load data by itself - it tells a DAO to do so. As for validation, it asks the Model to do it.

Ok, let's imagine that we rewrite Notepad the MVVM way. We have a Document object with a string property - say Content. And a DocumentViewModel that holds a reference to a Document. It has a a string Content property too, which is loaded from the underlying Document. And then a DocumentView with a text box that binds to the DocumentViewModel's Content. I know I could bind to Document.Content directly but I've beaten that horse in my original post already.

Am I on the right track here ?

Now loading the ViewModel from the Model has been dealt with but what about the data flowing back from Mr/Ms User to the View to the ViewModel to the Model ?

My current take is this…

ViewModel exposes commands such as NewCommand, OpenCommand, SaveCommand, SaveAsCommand - and then some more like Copy/Cut/Paste Commands.

NewCommand.Execute checks whether pending changes exist and possibly offers to save. Then it sets the VM's Document to a "new Document()". The VM updates its Content property and fires an event.

SaveCommand.CanExecute returns True if the document has been changed. SaveCommand.Execute copies the text from VM.Content to Document.Content then tells some object to save the document - via an interface so it can be mocked and unit-tested.

SaveAsCommand.Execute…hmmm…I guess it sets the VM's Document to a "new Document()" before transferring the VM.Content to Document.Content. But this assumes that the VM has copied all the Document's information. Or maybe the Document is cloneable and we replace the VM.Document with VM.Document.Clone() ?

Now in my real app, it's a bit messier because the Document is a composition of other objects that have their VM too.
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
SledgeHammer0120-Jul-11 8:43
SledgeHammer0120-Jul-11 8:43 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? [modified] Pin
Jean-Louis Leroy21-Jul-11 23:01
Jean-Louis Leroy21-Jul-11 23:01 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
Jean-Louis Leroy21-Jul-11 23:20
Jean-Louis Leroy21-Jul-11 23:20 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
Pete O'Hanlon21-Jul-11 23:29
mvePete O'Hanlon21-Jul-11 23:29 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
Jean-Louis Leroy21-Jul-11 23:51
Jean-Louis Leroy21-Jul-11 23:51 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
Pete O'Hanlon22-Jul-11 0:02
mvePete O'Hanlon22-Jul-11 0:02 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
SledgeHammer0122-Jul-11 6:39
SledgeHammer0122-Jul-11 6:39 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
Sacha Barber22-Jul-11 11:27
Sacha Barber22-Jul-11 11:27 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
SledgeHammer0122-Jul-11 6:28
SledgeHammer0122-Jul-11 6:28 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
Pete O'Hanlon21-Jul-11 23:27
mvePete O'Hanlon21-Jul-11 23:27 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
SledgeHammer0122-Jul-11 6:45
SledgeHammer0122-Jul-11 6:45 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
Pete O'Hanlon22-Jul-11 9:59
mvePete O'Hanlon22-Jul-11 9:59 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
SledgeHammer0122-Jul-11 10:11
SledgeHammer0122-Jul-11 10:11 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
Pete O'Hanlon22-Jul-11 20:44
mvePete O'Hanlon22-Jul-11 20:44 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
_Maxxx_21-Jul-11 13:55
professional_Maxxx_21-Jul-11 13:55 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
BubingaMan25-Jul-11 1:46
BubingaMan25-Jul-11 1:46 
AnswerRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
RobCroll20-Jul-11 1:53
RobCroll20-Jul-11 1:53 
AnswerRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
_Maxxx_21-Jul-11 14:06
professional_Maxxx_21-Jul-11 14:06 
QuestionDynamically changing themes from DLL at run-time Pin
Prasoon Chaudhary19-Jul-11 1:20
Prasoon Chaudhary19-Jul-11 1:20 
AnswerRe: Dynamically changing themes from DLL at run-time Pin
Varsha Ramnani20-Jul-11 21:23
professionalVarsha Ramnani20-Jul-11 21:23 
GeneralRe: Dynamically changing themes from DLL at run-time Pin
Prasoon Chaudhary20-Jul-11 22:42
Prasoon Chaudhary20-Jul-11 22:42 
QuestionFind an empty element in a Silverlight Grid Pin
Neo1010119-Jul-11 0:31
Neo1010119-Jul-11 0:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.