Click here to Skip to main content
15,892,965 members
Articles / Programming Languages / C# 4.0

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.2K   78   8  
An effective way to implement a caching system in Silverlight (MVVM) RIA
using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PortableLibrary;
using PortableLibrary.Common;
using SilverlightClient.Common;
using SilverlightClient.Mapping;

namespace SilverlightClient.Core
{
    public class ModelManager
    {
        #region SINGLETON

        static ModelManager instance = null;
        static readonly object padlock = new object();

        public static ModelManager Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new ModelManager();
                    }
                    return instance;
                }
            }
        }

        #endregion SINGLETON

        private GlobalCache cache;

        private LoginService.LoginClient loginServiceClient;

        public ModelManager()
        {
            this.cache = new GlobalCache();

            this.loginServiceClient = new LoginService.LoginClient();
        }

        public void Load(BaseViewModel viewModelContext, String operation, Array parameters)
        {
            switch (operation)
            {
                case OperationType.LOAD_USER:
                    if (null != cache.LoadProperty(CacheMapping.USER_CONTEXT))
                    {
                        viewModelContext.WriteProperty(MainViewModelMapping.USER_CONTEXT, cache.LoadProperty(CacheMapping.USER_CONTEXT), typeof(UserContext));
                    }
                    else
                    {
                        LoadForce(viewModelContext, operation, parameters);
                    }
                    break;
            }
        }

        public void LoadForce(BaseViewModel viewModelContext, String operation, Array parameters)
        {
            switch (operation)
            {
                case OperationType.LOAD_USER:
                    UserLogin(viewModelContext, parameters);
                    break;
            }
        }

        private void UserLogin(BaseViewModel viewModelContext, Array parameters)
        {
            String username = parameters.GetValue(0) as String;

            String password = parameters.GetValue(1) as String;

            loginServiceClient.UserLoginCompleted += (sender, e) =>
            {
                var uc = e.Result as UserContext;

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

            loginServiceClient.UserLoginAsync(username, password);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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