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:
- A WCF project in which I have implemented all the services and technical data management based on Entity Framework 5 technology
- The Silverlight client, which I implemented the classes and views based on the MVVM pattern
- 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.
Silverlight 5 Toolkit to run sample project, free to download from this link.
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.
ModelManager.Instance.Load(this.viewModel, OperationType.LOAD_USER, new Object
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
if (null != cache.LoadProperty(CacheMapping.USER_CONTEXT))
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
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
Write methods of
ModelManager without risk of losing information.
- 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