代码之家  ›  专栏  ›  技术社区  ›  Byron Sommardahl

如何编写会话扩展而不必为httpsessionstate和httpsessionstatebase编写单独的方法?

  •  6
  • Byron Sommardahl  · 技术社区  · 16 年前

    我为会话编写了以下扩展方法,以便根据对象的类型来持久化和检索对象。这对我的解决方案很有效,但最终我不得不复制扩展方法来覆盖旧的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
    }
    
    3 回复  |  直到 16 年前
        1
  •  3
  •   Darin Dimitrov    16 年前

    您可以保留最初的实现和使用 HttpSessionStateWrapper 处理 HttpSessionState 案例:

    SomeType t = new HttpSessionStateWrapper(SomeHttpSessionStateInstance)
        .Get<SomeType>();
    
        2
  •  2
  •   Byron Sommardahl    16 年前

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

    @AndrewHare说,两者都不能实现公共基础或接口。嗯,事实上,他们有。它们都实现IEnumerable和ICollection。问题是,对于这两个信息,您想创建扩展方法来扩展实际上只用于会话的IEnumerable或ICollection吗?也许,也许不是。无论如何,这里有一种方法可以通过同时扩展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;
        }
    }
    

    我对这个解决方案并不着迷,但至少在一个有趣的方向上,它感觉像是一个步骤。

        3
  •  1
  •   Andrew Hare    16 年前

    不幸的是,这两种类型都没有您可以使用的公共基类或接口——目前,您必须复制扩展方法。