Click here to Skip to main content
13,089,386 members (63,172 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 17 Nov 2012

Cache System in Silverlight 5 RIA Application - Part 2 - Data write

, 17 Nov 2012
Rate this:
Please Sign up or sign in to vote.
An effective way to implement a caching system in Silverlight (MVVM) RIA


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.


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)
   // Object creation or other operation performed

   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)
   // Some modification

     (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)
    //Invoke operation method

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.


  • 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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Michele Ziparo
Software Developer
Italy Italy
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 17 Nov 2012
Article Copyright 2012 by Michele Ziparo
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid