Click here to Skip to main content
15,894,825 members
Articles / Web Development / HTML

Web Application State

Rate me:
Please Sign up or sign in to vote.
4.87/5 (16 votes)
15 Apr 2008CPOL7 min read 83.5K   1.2K   127  
This article explains how to maintain the web application state.
using System;
using System.Collections.Generic;
using System.Web;

namespace AndrewGolik.Training.Components.Storage
{
    public class CookieStorage<T> : IStorage<T> where T : class
    {
        private HttpServerUtility Utility
        {
            get { return HttpContext.Current.Server; }
        }

        private static HttpCookie CookieIn
        {
            get { return HttpContext.Current.Request.Cookies[typeof (T).FullName]; }
        }

        private static HttpCookie CookieOut
        {
            get
            {
                HttpContext.Current.Response.Cookies[typeof (T).FullName].Expires = DateTime.Now.AddYears(1);
                return HttpContext.Current.Response.Cookies[typeof (T).FullName];
            }
        }


        public void Clear()
        {
            CookieOut.Values.Clear();
        }

        public void Delete(object key)
        {
            foreach (string currentKey in CookieIn.Values.Keys)
            {
                if (key.ToString() != currentKey)
                   CookieOut[currentKey] = CookieIn[currentKey];
            }
            CookieIn.Values.Clear();
            foreach (string currentKey in CookieOut.Values.Keys)
            {
                   CookieIn[currentKey] = CookieOut[currentKey];
            }
        }

        public T Get(object key)
        {
            if (CookieIn == null)
                return null;
            string decodedXml = CookieIn[key.ToString()];
            string xml = Utility.UrlDecode(decodedXml);
            return Serializer<T>.Deserialize(xml);
        }

        public List<T> GetAll(Type type)
        {
            List<T> list = new List<T>();
            if (CookieIn == null)
                return list;

            foreach (string key in CookieIn.Values.Keys)
            {
                string decodedXml = CookieIn.Values[key];
                if (string.IsNullOrEmpty(decodedXml))
                    continue;
                string xml = Utility.UrlDecode(decodedXml);
                T obj = Serializer<T>.Deserialize(xml);
                list.Add(obj);
            }
            return list;
        }

        public void Save(object key, T obj)
        {
            string xml = Serializer<T>.Serialize(obj);
            if (CookieIn != null)
                foreach (string currentKey in CookieIn.Values.Keys)
                {
                    CookieOut[currentKey] = CookieIn[currentKey];
                }
            CookieOut[key.ToString()] = Utility.UrlEncode(xml);
        }
    }
}

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
Web Developer
Belarus Belarus
Andrew Golik is a software professional working in Minsk, Belarus.
He enjoys design infrastructures based on object oriented paradigm. His programming experience includes ASP, ASP.NET, .NET, COM, JAVA, PHP, DHTML, AJAX, blah blah blah....

Andrew Golik's Blog

Comments and Discussions