Click here to Skip to main content
15,891,136 members
Home / Discussions / WPF
   

WPF

 
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 
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 
(quotes no in the same order as in the original text)

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


Here I stumble upon a difficulty. Consider a Person that has a birth date stored in a DateTime. Obviously we don't want to present that in the VM. Instead we want to map the double Person.BirthDate to a text input field in the view - or three text boxes for day, month and year, or some Calendar control. So what's the VM between View and Model going to look like ? Does it contain one (or three) string properties ?

Hmm, maybe something like this ?


public string Day
{
  get { return _person.BirthDate.Day.ToString(); }
  set { d = _person.BirthDate.AddDays(int.Parse(value)).AddDays(-_person.BirthDate.Day); }
}

public string Month
{
  get { return _person.BirthDate.Month.ToString(); }
  set { d = _person.BirthDate.AddMonths(int.Parse(value)).AddMonths(-_person.BirthDate.Month); }
}

public string Year
{
  get { return _person.BirthDate.Year.ToString(); }
  set { d = _person.BirthDate.AddMonths(int.Parse(value)).AddMonths(-_person.BirthDate.Year); }
}


Or maybe a ValueConverter is used somewhere ? I haven't explored that yet...

SledgeHammer01 wrote:
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


I do see your point with having only one instance of an object in memory at all times...especially since I have implemented object-relational mappers like Perl's Tangram. If I have two views looking at the same Model at the same time, I'd want the changes made via one view to be reflected in the other view. Right ? And binding to the Model's property instead of copying them gives me just that.

Or does it ? Back to my Person.BirthDate example, suppose that I have VM1 that exposes the birth date as a single string property (BirthDate) in yyyy/mm/dd format. When the View sets the VM's BirthDate property the Model.BirthDate is updated. Now VM2 uses three string properties: Day, Month and Year. But changing the birth date via VM1 fires a "BirthDate" event that is significant only to the View that displays VM1. The change notifications are sent as changes to the VM's properties, whereas (if memory serves) in Smalltalk MVC they are sent as updates to the Model's properties.

There is also another possibility: bind the two views to the same ViewModel. This leads to another difficulty: what if the two views are so different that we'd normally use two different ViewModel classes ? We'd be forced to artifically merge them into some sort of Janus VM with four properties: Day, Month, Year, BirthDate.

Back to my real situation...I have a User which contains a collection of Reports. All that is persisted to a DB via NHibernate via a DAO. I want to support the New/Open/Save/SaveAs cycle for Reports. It means that "opening" a report means picking one from the collection in the User object. In the scheme you propose, Save would simply tell the DAO to update the object - no need to copy stuff from VM to M. But how do I implement SaveAs ? By the time I know the end user wants to make a new object, the old one has already been messed up.
edit: grammar

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 

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.