Simple question about view model properties

Jul 11, 2011 at 1:41 PM

I'd like to know the functional difference between properties created using the dataprop snippet and

those created through the vmprop snippet.

Thanks in advance...

Coordinator
Jul 11, 2011 at 2:27 PM

The one and only difference between the 2 is that the dataprop code snippet also provides a default value (very useful when deserializing objects).

However, in view models, you probably want to initialize the values in the constructor. What I have seen some people do is set a default value of "new Collection()". The problem is that it's an instance, the the instance will be shared amongst all view models if set as a default value. Therefore, it's best to use the vmprop which does not provide the default value by default so you'll have to set it yourself.

Jul 11, 2011 at 2:41 PM

Very clear answer. Thanks again for your support

Jul 25, 2011 at 7:53 AM

i'm sorry    it also interesting for me     if i   correctly understand   

  that to deserialize i need use dataproperty snippet,   but  in this case i have default value that is not  good for view model,  so :

                   i shouldn't  use  Dataprop snippet for viewmodel objects   that can be serialized

                   i can use dataprop snippet with  collection of simple classes to   store data  and then restore with default values

               yes      that's right   

Coordinator
Jul 25, 2011 at 7:58 AM

The property registered with the vmprop is exactly the same as a property registered with the dataprop. The only difference is that the default value will be used when vmprop is used (thus null for reference types, and default values for value types such as false for boolean). Both are using the same base class (DataObjectBase) which takes care of the serialization. 

You can use dataprop code snippets in the view model as well, however, you have to be very careful with collections because if a view model is instantiated twice and you have set the default value to a specific instance, the same specific instance will be used (and the 2 view models will use the same collection). This also applies to DataObjectBase. So, in general you can set the default values for value types, but I would always recommend to instantiate reference types in the constructor instead of setting a default value.

Jul 26, 2011 at 8:38 AM

thanks Geert for realy clear answer  ....      instantiating concept  it's  the  way  that i  thinking           i not fully understand  you when  you sad  " shared amongst all view models"    but now you 've responded  really clear answer for me too      thanks for support Geert