One of the ever recurring questions in a MVVM architecture is how the model and the view model are kept in sync – that is, how does the model inform the view model of changes? Especially when collections (lists etc.) are involved, this can lead to lots of synchronizing (and mostly more or less duplicated) code to get the changes up to the view model.
In this application I used an approach that puts the burden of pushing values from the model to the view model in the view model base class (ViewModelObject). I’d like to have it as simple as possible. Properties in the view model are marked with an attribute, so the view model in the case of a notification from the model can transfer the value from the model to the view model.
The most basic transfer is an “as is” transfer, that is the value is copied from a model property to a view model property unchanged (unconverted). Quite often some string conversions are required when the model for example has an integer property and the corresponding view model property is a string and the value should be formatted to be using thousands separators and a fixed number of decimals.
These are the currently available attributes:
|BindToModelAsCurrency||Transfers the value from the model to the view model, converting it to a string as a currency representation (for example “$(5,000)” for -5000)|
|BindToModelAsDate||Transfers the value from the model to the view model, converting it to a string as a date representation (for example “Tuesday, 4 Apr 1980”)|
|BindToModelAsIs||Transfers the value from the model to the view model without any conversions (model and view model properties must have the same type)|
|BindToModelAsList||Links a list (ObservableCollection) from the model to a list (ObservableCollection) in the view model. Operations on the model list (insertion, removal etc.) are duplicated in the view model list. Model objects are automatically replaced by corresponding view model objects.|
|BindToModelAsNumber||Transfers the value from the model to the view model, converting it to a string as a number representation (for example “5,000.00” for 5000)|
|BindToModelAsString||Transfers the value from the model to the view model, using the ToString() method|
|BindToModelAsVObject||Transfers the value from the model to the view model, creating a specified ViewModelObject for it and passing the model object to it which is stored in the ModelObject member|
The usage is pretty simple: put one of the attributes before a property and the property in the model (with the same name) gets automatically transferred to the view model property when it changes:
public string Comment
SetProperty(ref _comment, value);
public string MemKB
SetProperty(ref _memKB, value);
The game’s main page you can find here: All Those Bugs Page