6

次のSessionの拡張メソッドを作成して、オブジェクトのタイプごとにオブジェクトを永続化および取得できるようにしました。これは私のソリューションではうまく機能しますが、古いHttpSessionStateと新しいHttpSessionStateBaseをカバーするために、拡張メソッドを複製する必要がありました。これらを両方のタイプをカバーする1つのセットに戻す方法を見つけたいと思います。何かご意見は?

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

初期の実装を維持し、HttpSessionStateWrapperHttpSessionStateを使用してケースを処理できます。

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

うまくいく答えを見つけましたが、いくつかの欠点があります。誰かがそれを改善できることを願っています。

@Andrew Hareは、共通のベースまたはインターフェースを実装していないと述べました。実際、そうです。どちらも IEnumerable と ICollection を実装しています。問題は、その情報を参考にして、実際には Session 専用の 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;
    }
}

私はこのソリューションに夢中ではありませんが、少なくとも興味深い方向への一歩のように感じます.

于 2010-06-18T23:25:40.067 に答える
1

残念ながら、これらの型のどちらにも、使用できる共通の基本クラスまたはインターフェイスがありません。現時点では、拡張メソッドを複製する必要があります。

于 2010-06-18T22:28:58.160 に答える