6

我为 Session 编写了以下扩展方法,以便我可以按对象的类型持久化和检索对象。这对我的解决方案很有效,但我最终不得不复制我的扩展方法来覆盖旧的 HttpSessionState 和新的 HttpSessionStateBase。我想找到一种方法将这些恢复到涵盖两种类型的集合。有什么想法吗?

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
}
4

3 回答 3

3

您可以保留您的初始实现并使用HttpSessionStateWrapper来处理这种HttpSessionState情况:

SomeType t = new HttpSessionStateWrapper(SomeHttpSessionStateInstance)
    .Get<SomeType>();
于 2010-06-19T07:30:54.360 回答
2

我找到了一个可行的答案,但有一些缺点。我希望有人能够改进它。

@Andrew Hare 说两者都没有实现公共基础或接口。好吧,实际上,他们确实如此。它们都实现了 IEnumerable 和 ICollection。问题是,有了这些信息,您是否要创建扩展方法来扩展 IEnumerable 或 ICollection,而这些方法实际上只适用于 Session?也许,也许不是。无论如何,这是一种使用同时扩展 HttpSessionState 和 HttpSessionStateBase 的扩展方法来消除重复的方法:

public static class SessionExtensions
{
    public static T Get<T>( this ICollection collection )
    {
        return collection.Get<T>( typeof( T ).Name );
    }

    public static T Get<T>( this ICollection collection, string key )
    {
        object obj = null;
        dynamic session = collection as HttpSessionState ?? ( dynamic ) ( collection as HttpSessionStateBase );

        if( session != null )
        {
            obj = session[key];

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

        return (T)obj;
    }

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

    public static void Put<T>( this ICollection collection, T obj, string key )
    {
        dynamic session = collection as HttpSessionState ?? ( dynamic ) ( collection as HttpSessionStateBase );
        if(session!=null)
            session[ key ] = obj;
    }
}

我对这个解决方案并不着迷,但感觉至少是朝着一个有趣的方向迈出了一步。

于 2010-06-18T23:25:40.067 回答
1

不幸的是,这些类型都没有您可以使用的通用基类或接口 - 现在您必须复制扩展方法。

于 2010-06-18T22:28:58.160 回答