Click here to Skip to main content
15,890,973 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: Style.TargetType = Interface || abstract class Pin
Kunal Chowdhury «IN»20-Jul-11 0:23
professionalKunal Chowdhury «IN»20-Jul-11 0:23 
QuestionMVVM: Transfering data from ViewModel to Model, when and how ? [modified] Pin
Jean-Louis Leroy19-Jul-11 1:38
Jean-Louis Leroy19-Jul-11 1:38 
AnswerRe: MVVM: Transfering data from ViewModel to View, when and how ? Pin
Pete O'Hanlon19-Jul-11 1:45
mvePete O'Hanlon19-Jul-11 1:45 
AnswerRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
Abhinav S20-Jul-11 0:42
Abhinav S20-Jul-11 0:42 
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 
GeneralRe: MVVM: Transfering data from ViewModel to Model, when and how ? Pin
SledgeHammer0120-Jul-11 8:43
SledgeHammer0120-Jul-11 8:43 
Jean-Louis Leroy wrote:

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 ?

---

No, that is exactly what you do *NOT* do.

In your example, you have DocumentViewModel that holds a reference to a Document(Model). Document(Model) has a string Content property (the content of the text file).

So far, so good.

The next step is where you head off the reservation a bit Smile | :) .

DocumentViewModel should DEFINITELY NOT have its own string Content property. It should simply *wrap* the one from the Document(Model). I.e.

DocumentViewModel.cs:

public string Content
{
get
{
return _document.Content;
}

set
{
if (_document.Content != value)
{
_document.Content = value;
OnPropertyChanged("Content");
}
}
}

The reasons for this are two-fold:

1) you don't have to worry about keeping the VM and M in sync, since they are always by design in sync
2) you are not storing the same data in multiple places. Imagine if the text file is 100 bytes... no biggie right? your design uses up 200 bytes of memory. No biggie. Now imagine if the text file is 1MB or 10MB or 100MB... with your design, you are using up 2MB, 20MB or 200MB of memory. Not good.

Rule #1 in good software design, ESPECIALLY in WPF & MVVM... is that you should only have one instance of an object in memory at all times. The reason why its more important in WPF is that imagine this scenario:

SomeObject (inst 1) ... somebody binds to it ... all good... anytime SomeObject (inst 1) is modified, the UI is updated

Now imagine that somebody went and made a COPY of SomeObject and now you have SomeObject (inst 2)... changes made to one will not update the properties in the other and data binding will be broken Smile | :) .
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 

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.