Silverlight 5 Hosting :: Cache System in Silverlight 5 RIA Application

In this article I will explain how I chose to implement the global caching mechanism of the application data that I put in some projects of mine developed in. NET. The scenario consists of three projects that were created in Microsoft Visual Studio 2012:

  1. A WCF project in which I have implemented all the services and technical data management based on Entity Framework 5 technology.
  2. The Silverlight client, which I implemented the classes and views based on the MVVM pattern;
  3. A class library that I created from the template “Portable Library” available in the new edition of Visual Studio: this type of project allows me to have available a dll class library usable not only in Silverlight, but also in WPF or other technologies  like Windows Phone.

Additional Requirements

Silverlight 5 Toolkit to run sample project, free to download from this link

Background

The idea behind this implementation technique, is that of having available a container from which to read ( and write ) informations from time to time that are necessary for the proper functioning of the view that the user is currently viewing in the client application.

The presence or absence of the information in the cache is that which controls the actual execution of all requests for reading of data to WCF services.

Using the code

Looking through the code in the attached VS solution, you will see that most of the work is done in class ModelManager, implemented with the singleton pattern because its methods can be used in all classes of the application without the need for multiple instances.

Conceptually, my preferred approach is to require the ModelManager to perform a particular read operation, also by sending the necessary parameters in array of Object type, which then will be treated in an appropriate way in each specific method

ModelManager.Instance.Load(this.viewModel, OperationType.LOAD_USER, new Object[] {"Username", "Password"});

In the next step, in the Load method of ModelManager, try to read the property (correctly mapped) with its name directly from the cache. If the requested property is in the cache, then it will read its value, and write the corresponding property of the ViewModel current.

if (null != cache.LoadProperty(CacheMapping.USER_CONTEXT))
{
viewModelContext.WriteProperty(MainViewModelMapping.USER_CONTEXT, cache.LoadProperty(CacheMapping.USER_CONTEXT), typeof(UserContext));
}

If the requested property is not in the cache, then we will read from the WebService, then write the result (errors excepted of course) in memory, in both the cache and in the current ViewModel.

cache.WriteProperty(CacheMapping.USER_CONTEXT, uc, typeof(UserContext));
viewModelContext.WriteProperty(MainViewModelMapping.USER_CONTEXT, uc, typeof(UserContext));

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. 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, manage 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

{
   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

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

for example when user request logout or by using a Timer for automatic save.

Comments are closed.

Sponsors

JaringanHosting.com

Cheap Reseller Hosting

Best Blog Hosting Provider

Best European ASP.NET 4.0 Hosting Provider