MVVM - MarkDirty the Model from the ViewModel

MVVM - MarkDirty the Model from the ViewModel

Old forum URL: forums.lhotka.net/forums/t/9090.aspx


t.kehl posted on Wednesday, June 16, 2010

Hi.

I have created a BusinessObject Contact (EditableRoot). Now, I have also created a ViewModel which holds a Contact-Object as Model. In the ViewModel, I have extended a Property that I need for Display some Information from a ChildList of Contact. This Property will be binded to the View. I load this property, when I create the ViewModel and save the values, when the ViewModel will save the Model. Now, I am searching the best way, to set the Model Dirty from the ViewModel, when the value of my new Property in the ViewModel has been changed. The Property IsDirty of the Model (BusinessBase) has now setter and the Method MarkDirty() is protected. So, how should I solve this problem?

Thank you for any help!

Best Regards, Thomas

Jav replied on Wednesday, June 16, 2010

t.kehl
Now, I have also created a ViewModel which holds a Contact-Object as Model

Reading this sentence in your post, I get the feeling that you may not be creating the ViewModel the right way.  In fact it appears that you are probebly not creating your object graph (Contact and its ChildList) correctly.  If you create them correctly using the Csla techniques, you will not need to Mark Dirty any object manually. When any child in the ChildList is modified, Csla will take care of marking the appropriate objects in the objectgraph dirty.

Finally you create a ViewModel (when using Csla) like this

                  public class ContactViewModel : ViewModel<Contact>

With that the Contact automatically becomes the Model in your ViewModel, and you can access any and all of its children using the Model property.  In fact "Model" will be the Contact object and can be treated as such.

Jav

t.kehl replied on Wednesday, June 16, 2010

Hi Jav

I create the ViewModel like you wrote with

public class ContactViewModel : ViewModel<Contact>

But I have the fallowing problem:

Contact has a ChildList SearchTermChildList:

public Child.SearchTermChildList SearchTermChildList {
   get {
      if (!(FieldManager.FieldExists(SearchTermChildListProperty))) {
         LoadProperty(SearchTermChildListProperty, Child.SearchTermChildList.GetSearchTermChildList(this.Id));
      }
      return GetProperty(SearchTermChildListProperty);
   }
}

The objects in this ChildList are of Type SearchTerm. Each SearchTerm-Objekt has a Property Name. Now, I need all Names of all SearchTermChilds concatenated to one string with LineFeed beteween each Name and show the whole string in a TextBox. The User can now write, change or delete lines in this TextBox. This means, there should be created new Child-Objects, child-objects should be edited or deleted. I plan now, to create the whole string when the ViewModel will be created and also create a Property in the ViewModel, that I can bind this string to the TextBox in the View. Now, when the user will write in the textbox, this property in the ViewModel will be changed what mean, I have to manipulate the child-List, when the Model will be saved. For this, I have also to mark the model as dirty, when the user changes the text in the textbox (so, for example the save-button will be enabled).

I have the meaning, that it should be possible to extend the model with "View-Properties" in the ViewModel and then I should have an interaction between the ViewModel and the Model (perhaps set dirty) - or is this the wrong way?

Instead extend the ViewModel with "DisplayProperties" I could also use IConverter and bind directly to the SearchTermChildList. But on this way, I have the Problem, that the Property SearchTermChildList is readonly and I don't know a way how I can write the changes back.

Best Regards, Thomas

 

Copyright (c) Marimer LLC