Click here to Skip to main content
15,896,154 members
Articles / Web Development / ASP.NET

Session, Cookie, Query String & Cache Variables Unified

Rate me:
Please Sign up or sign in to vote.
4.06/5 (8 votes)
1 Dec 2009Ms-PL5 min read 56K   297   34  
A wrapper to work with the session, cookie, query string or cache in a unified, flexible and type safe manner while offering full support for JSON serialization.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace System.Web
{
    public partial class Local : LocalBase
    {
        /// <summary>
        /// An experimental class allowing to specify any object having a Text property in the target and source list.
        /// Checkboxes, DropDownLists and RadioButtonLists are also supported.
        /// </summary>
        /// <typeparam name="T">The generic type T for the variable.</typeparam>
        public class CompositeVar<T> : Var<T>
        {
            List<object> _targetControls { get; set; }
            List<object> _sourceControls { get; set; }

            public CompositeVar(string key, object targetObjects, params object[] sourceObjects)
                : this(default(T), key, new List<object> { targetObjects }, sourceObjects) { }

            public CompositeVar(string key, List<object> targetObjects, params object[] sourceObjects)
                : this(default(T), key, targetObjects, sourceObjects) { }

            public CompositeVar(T defaultValue, string key, List<object> targetObjects, params object[] sourceObjects)
                : base(defaultValue, key, null, null, null, null)
            {
                List<Sources> sourceTypes = new List<Sources>();

                if (sourceObjects != null)
                {
                    foreach (object obj in sourceObjects)
                        if (obj.GetType() == typeof(Sources))
                            sourceTypes.Add((Sources)obj);
                        else
                            _sourceControls.Add(obj);

                    SourceTypes = sourceTypes.ToArray();
                }

                if (targetObjects != null)
                {
                    foreach (object obj in targetObjects)
                        if (obj.GetType() == typeof(byte))
                            TargetTypes += (byte)obj;
                        else
                            _targetControls.Add(obj);
                }
            }

            //public new T Value
            //{
            //    get
            //    {
            //        foreach (object obj in _sourceControls)
            //        {
            //            string propertyName = (obj);
            //            object value = Reflection.GetPropertyValue<object>(obj, propertyName);
            //            if (value != null)
            //                return Serialization.FromJson<T>(value.ToString());
            //        }
            //        return default(T);
            //    }
            //    set
            //    {
            //        foreach (object obj in _targetControls)
            //        {
            //            string propertyName = Reflection.GetInputPropertyName(obj);
            //            Reflection.SetPropertyValue<string>(obj, propertyName, Serialization.ToJson<T>(value));
            //        }
            //    }
            //}

        }
    }
}

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 Microsoft Public License (Ms-PL)


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

Comments and Discussions