Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write extensions for Session without having to write separate methods for HttpSessionState and HttpSessionStateBase?

I wrote the following extension methods for Session so that I can persist and retrieve objects by their type. This works well for my solution, but I ended up having to duplicate my extension methods to cover the old HttpSessionState and the new HttpSessionStateBase. I'd like to find a way to get these back down to one set that covers both types. Any thoughts?

public static class SessionExtensions
{
    #region HttpSessionStateBase

    public static T Get<T>(this HttpSessionStateBase session)
    {
        return session.Get<T>(typeof(T).Name);
    }

    public static T Get<T>( this HttpSessionStateBase session, string key )
    {
        var obj = session[key];

        if( obj == null || typeof(T).IsAssignableFrom( obj.GetType() ) )
            return (T) obj;

        throw new Exception( "Type '" + typeof( T ).Name + "' doesn't match the type of the object retreived ('" + obj.GetType().Name + "')." );
    }

    public static void Put<T>(this HttpSessionStateBase session, T obj, string key)
    {
        session[key] = obj;
    }

    public static void Put<T>(this HttpSessionStateBase session, T obj)
    {
        session.Put(obj, typeof(T).Name);
    }

    #endregion

    #region HttpSessionState

    public static T Get<T>( this HttpSessionState session )
    {
        return session.Get<T>( typeof( T ).Name );
    }

    public static T Get<T>( this HttpSessionState session, string key )
    {
        var obj = session[ key ];

        if( obj == null || typeof( T ).IsAssignableFrom( obj.GetType() ) )
            return ( T ) obj;

        throw new Exception( "Type '" + typeof( T ).Name + "' doesn't match the type of the object retreived ('" + obj.GetType().Name + "')." );
    }

    public static void Put<T>( this HttpSessionState session, T obj )
    {
        session.Put( obj, typeof(T).Name );
    }

    public static void Put<T>( this HttpSessionState session, T obj, string key )
    {
        session[ key ] = obj;
    }

    #endregion
}
like image 705
Byron Sommardahl Avatar asked Jun 18 '10 22:06

Byron Sommardahl


People also ask

How will you maintain the sessions in MVC?

ASP.NET MVC provides three ways (TempData, ViewData and ViewBag) to manage session, apart from that we can use session variable, hidden fields and HTML controls for the same.

What is HttpSessionStateBase?

The HttpSessionStateBase class is an abstract class that contains the same members as the HttpSessionState class. The HttpSessionStateBase class enables you to create derived classes that are like the HttpSessionState class, but that you can customize and that work outside the ASP.NET pipeline.

What does count property of session specifies?

Count. Gets the number of items in the session-state collection. IsCookieless. Gets a value indicating whether the session ID is embedded in the URL or stored in an HTTP cookie.

What is session in MVC C#?

Session is used to store data values across requests. Whether you store some data values with in the session or not Asp.Net MVC must manage the session state for all the controllers in your application that is time consuming.


1 Answers

You could keep your initial implementation and use HttpSessionStateWrapper to handle the HttpSessionState case:

SomeType t = new HttpSessionStateWrapper(SomeHttpSessionStateInstance)
    .Get<SomeType>();
like image 67
Darin Dimitrov Avatar answered Sep 20 '22 17:09

Darin Dimitrov