Introduction
In the first part of this series of articles, we have seen how to read the data and save it in the cache of the application. Now our attention will shift to the saving of data.
Background
The basis of this implementation remains the knowledge of the pattern MVVM.
Once the data is read and stored in the cache and in the viewmodel, we can change them at will, deciding the best technique for saving.
In the ModelManager
class are implemented, similar to the methods of reading, two management methods of writing data, a "local" only to manage the cache and the viewmodel
, and one dedicated to the WebService
which forces the invocation of methods implemented for each saving operation provided.
Using the Code
As you can see from the application code up to date, managing the operations of data writing is very simple.
We can use three possible solutions.
1. Object Creation, First Save
In this case, we do not have a specific property already read and saved in the cache, but we are creating a new object, saving it in the ViewModel
, and we want to save it to make it available, if necessary, to the rest of the application.
private void ForceWrite(object sender, RoutedEventArgs e)
{
ModelManager.Instance.WriteForce(this.viewModel, OperationType.WRITE_USER, new Object[]
{ ucToSave }, true);
}
The last parameter specified in the arguments, indicates that ModelManager
will also update the property.
2. M-V-V-M
Using the potential of the MVVM pattern to automate the updating of objects in the ViewModel
. This will automatically update the cache, because as we have seen for reading, the various properties are assigned by reference:
Text="{Binding Path=UserContext.Email, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
At this point, we can invoke data saving simple way by invoking
WriteForce
method
private void SaveMvvm(object sender, RoutedEventArgs e)
{
ModelManager.Instance.WriteForce(this.viewModel, OperationType.WRITE_USER, new Object[]
{ this.viewModel.UserContext }, false);
}
In this case, the last parameter specified in the arguments, indicates that ModelManager
should not update the property in the cache and in the viewmodel.
3. Save to Cache Methods
If we want to perform some operations and save data only, for example, before logout, we can invoke the Write
method in ModelManager
class:
private void SaveUser(object sender, RoutedEventArgs e)
{
ModelManager.Instance.Write
(this.viewModel, OperationType.WRITE_USER, new Object[] { ucToSave });
}
This method will enqueue new write operation in writeOperationsQueue
.
After all, when we need data update, we can invoke:
public void WritePendingChanges()
{
while (writeOperationsQueue.Count > 0)
{
}
}
For example, when user requests logout or by using a Timer
for automatic save.
Points of Interest
One of the most interesting aspects of this implementation is the ability to write correctly the properties of the particular ViewModel
that we are using at any particular point in the application.
The queue of write operations implemented allows us to decide whether to temporarily retain the data or save immediately by sending the appropriate requests to the WbService
.
History
- October 28, 2012 : Part 1. Loading data
- November 10, 2012: Part 2. Writing data
- Coming soon: Part 3. Entity Framework 5, data management
- Coming soon: Part 4. Handle multiple request in multiple ViewModel context