Click here to Skip to main content
15,886,067 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Cache System in Silverlight 5 RIA Application - Part 1 - Data Load

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
4 Nov 2012CPOL2 min read 10.1K   78   8  
An effective way to implement a caching system in Silverlight (MVVM) RIA

Introduction

In this series of articles, 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) information 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.

C#
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.

C#
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.

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

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. You can define many classes derived by BaseViewModel class and send it as parameter to Load and Write methods of ModelManager without risk of losing information.

History

  • October 28, 2012 : Part 1. Loading data
  • Part 2. Writing data
  • Coming soon: Part 3. Entity Framework 5, data management
  • Coming soon: Part 4. Handle multiple request in multiple ViewModel context

License

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


Written By
Software Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --