【问题标题】:How can I write extensions for Session without having to write separate methods for HttpSessionState and HttpSessionStateBase?如何为 Session 编写扩展而不必为 HttpSessionState 和 HttpSessionStateBase 编写单独的方法?
【发布时间】:2011-03-05 15:28:26
【问题描述】:

我为 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
}

【问题讨论】:

    标签: asp.net-mvc extension-methods httpsession


    【解决方案1】:

    您可以保留初始实现并使用HttpSessionStateWrapper 处理HttpSessionState 案例:

    SomeType t = new HttpSessionStateWrapper(SomeHttpSessionStateInstance)
        .Get<SomeType>();
    

    【讨论】:

      【解决方案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;
          }
      }
      

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2011-05-07
          • 1970-01-01
          • 2018-02-23
          • 2010-09-09
          • 2021-11-05
          • 1970-01-01
          • 2016-07-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多