Click here to Skip to main content
12,244,103 members (56,422 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4.4K views
7 bookmarked
Posted

Easy Sessions using Extension Methods

, 22 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
How to make sessions easy to use by extending .NET object class

Introduction

Working with sessions in ASP.NET can often be uncomfortable and lead to errors. By using these extension methods, this task will be much easier.

Background

You are used to program with C#, ASP.NET, sessions and extension methods.

Using the Code

The Extension Methods

Extending object class in order to add all objects methods to save sessions.

public static class Sessions
{
    public static object ToSession(this object obj)
    {
        string sessionId = obj.GetType().FullName;

        System.Web.HttpContext.Current.Session.Remove(sessionId);
        System.Web.HttpContext.Current.Session.Add(sessionId, obj);

        return obj;
    }

    public static object FromSessionOrNew(this object obj, object objNew=null)
    {
        string sessionId = obj.GetType().FullName;

        if ((System.Web.HttpContext.Current.Session[sessionId] as object) != null){
            obj = System.Web.HttpContext.Current.Session[sessionId] as object;
        }
        else{
            obj = objNew;

            System.Web.HttpContext.Current.Session.Remove(sessionId);
            System.Web.HttpContext.Current.Session.Add(sessionId, obj);
        }

        return obj;
    }

    public static void RemoveFromSession(this object obj)
    {
        string sessionId = obj.GetType().FullName;
        System.Web.HttpContext.Current.Session.Remove(sessionId);
    }
}

Sample of Use of FromSessionOrNew

It is an instance of an object from session. Optionally, if session is null, a default instance (Foo() {Name="Lois"}) can be returned and stored in session, so, this method is useful both get an object from session and store it for the first time.

public class Foo
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public ActionResult Index()
{
    foo = (Foo)new Foo().FromSessionOrNew(new Foo() { Name = "Lois", Age = 30 });
}  

To store an object to session or remove it, use these methods:

foo.ToSession();
foo.RemoveSession(); 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

meula
Software Developer (Senior)
Unknown
No Biography provided

You may also be interested in...

Comments and Discussions

 
SuggestionTemplated version Pin
John Brett3-Jun-14 3:30
memberJohn Brett3-Jun-14 3:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 23 May 2014
Article Copyright 2014 by meula
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid