Use wrappers to access your cookies, sessions, …

by cweiss 26. January 2009 18:45

As described in my previous post, I will give you some more details about how you can access your cookies in a type-safe and easy way!

Update: Read the follow up post

The simplest way to do this is by using a little wrapper class like this one:

using System;
using System.Globalization;
using System.Web;

namespace CookieWrapper.Web
{
    public static class MyCookies
    {
        public static string UserEmail
        {
            get { return GetValue("UserEmail"); }
            set { SetValue("UserEmail", value, DateTime.Now.AddDays(10)); }
        }

        public static DateTime? LastVisit
        {
            get
            {
                string strDate = GetValue("LastVisit");
                if (String.IsNullOrEmpty(strDate))
                    return null;
                return DateTime.Parse(strDate, CultureInfo.InvariantCulture);
            }
            set
            {
                string strDate = value.HasValue ? value.Value.ToString(CultureInfo.InvariantCulture) : null;
                SetValue("LastVisit", strDate, DateTime.Now.AddDays(10));
            }
        }

        private static string GetValue(string key)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
            if (cookie != null)
                return cookie.Value;
            return null;
        }

        private static void SetValue(string key, string value, DateTime expires)
        {
            HttpContext.Current.Response.Cookies[key].Value = value;
            HttpContext.Current.Response.Cookies[key].Expires = expires;
        }
    }
}

All you have to do is create a static property for every cookie that you would like to work with. As you can see you also have the Expires-times administrated in one single place!

Now you can access the values as seen below and you don’t have to worry about the cookie implementation-details in every place.

tbLastVisit.Text = MyCookies.UserEmail;
MyCookies.LastVisit = DateTime.Now; 

Of course, you can also use this same approach for working with session data or any other key-based collection.

Thanks for reading,
Christian Weiss

Technorati-Tags: ,,

Tags: ,

Comments


January 26. 2009 22:59
John Sheehan
You should use generics on GetValue to handle returning the type you want.


January 27. 2009 02:53
Christian Weiss
Hi John,
thanks for your comment. I didn't use generics in this example, because you can only store strings into cookies. I didn't want somebody to think about storing too complex types in cookies, as this isn't recommended. (cookie size, ...)

but you are right, you definitely can save some more lines, if you use generics! thanks for pointing this out!

best regards,
Christian Weiss


January 28. 2009 09:32
Chris Marisic
I have one suggestion, you might want to abstract this one level further and instead of just having your static GetValue/SetValue methods to actually take in an interface of say ICookieContainer, then implement like a ConcreteCookieContainer that uses the exact code of your GetValue/SetValue and you could also implement a DictionaryCookieContainer that uses a regular Dictionary<string,string> so you could set up unit tests easier and then use an IoC container to inject whichever cookiecontainer you want in your program.


January 28. 2009 09:52
cweiss
Hi Chris,
I'm learning a lot about TDD, IOC, ... right now, but I haven't felt comfortable enough to write this post the way you described it.
I will give it a try today or tomorrow and add it to this post.
looking forward to hearing your feedback!

best regards,
Christian weiss


January 29. 2009 16:26
Christian Weiss
I've just posted a follow-up as described in my previous comment!
check it out: chwe.at/.../...okie-access-with-ASPNet-MVC-RC.aspx

Comments are closed
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 chwe.at