2010-06-18 4 views
6

Session에 대한 다음 확장 메서드를 작성하여 개체를 해당 형식으로 유지하고 검색 할 수 있도록했습니다. 이것은 내 솔루션에 잘 작동하지만 이전 HttpSessionState와 새로운 HttpSessionStateBase를 다루기 위해 확장 메서드를 복제해야했습니다. 이 두 가지 유형을 모두 다루는 한 세트로 다시 가져올 수있는 방법을 찾고 싶습니다. 이견있는 사람?HttpSessionState 및 HttpSessionStateBase에 대한 별도의 메서드를 작성하지 않고도 Session 확장을 작성할 수 있습니까?

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 
} 

답변

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

내가이 솔루션에 대한 미친 아니지만, 적어도, 흥미로운의 단계, 같은 느낌 방향.

1

불행히도 이러한 유형에는 사용할 수있는 공통 기본 클래스 나 인터페이스가 없습니다. 이제는 확장 메소드를 복제해야합니다.

3

당신은 당신의 초기 구현을 유지하고 HttpSessionState 경우 처리 할 수 ​​HttpSessionStateWrapper을 사용할 수

SomeType t = new HttpSessionStateWrapper(SomeHttpSessionStateInstance) 
    .Get<SomeType>(); 
관련 문제