【问题标题】:Get/Set HttpContext Session Methods in BaseController vs Mocking HttpContextBase to create Get/Set methods在 BaseController 中获取/设置 HttpContext 会话方法与模拟 HttpContextBase 以创建获取/设置方法
【发布时间】:2011-09-12 23:10:00
【问题描述】:

我在 BaseController 类中创建了 Get/Set HttpContext 会话方法,还模拟了 HttpContextBase 并创建了 Get/Set 方法。

哪个是最好的使用方法。

    HomeController : BaseController
    {
        var value1 = GetDataFromSession("key1") 
        SetDataInSession("key2",(object)"key2Value");

        Or

        var value2 = SessionWrapper.GetFromSession("key3");
        GetFromSession.SetDataInSession("key4",(object)"key4Value");
    }

   public class BaseController : Controller
   {
       public  T GetDataFromSession<T>(string key)
       {
          return (T) HttpContext.Session[key];
       }

       public void SetDataInSession(string key, object value)
       {
          HttpContext.Session[key] = value;
       }
   }

  public class BaseController : Controller
  {
     public ISessionWrapper SessionWrapper { get; set; }

     public BaseController()
     {
       SessionWrapper = new HttpContextSessionWrapper();
     }
  }

  public interface ISessionWrapper
  {
     T GetFromSession<T>(string key);
   void    SetInSession(string key, object value);
  }

  public class HttpContextSessionWrapper : ISessionWrapper
  {
     public  T GetFromSession<T>(string key)
     {
        return (T) HttpContext.Current.Session[key];
     }

     public void SetInSession(string key, object value)
     {
         HttpContext.Current.Session[key] = value;
     }
  }

【问题讨论】:

    标签: asp.net-mvc session httpcontext


    【解决方案1】:

    第二个似乎是最好的。虽然我可能会将这两个写为HttpSessionStateBase 的扩展方法,而不是将它们放入基本控制器中。像这样:

    public static class SessionExtensions
    {
        public static T GetDataFromSession<T>(this HttpSessionStateBase session, string key)
        {
             return (T)session[key];
        }
    
        public static void SetDataInSession<T>(this HttpSessionStateBase session, string key, object value)
        {
             session[key] = value;
        }
    }
    

    然后在控制器、助手或具有HttpSessionStateBase 实例的东西中使用它:

    public ActionResult Index()
    {
        Session.SetDataInSession("key1", "value1");
        string value = Session.GetDataFromSession<string>("key1");
        ...
    }
    

    在 ASP.NET MVC 中编写会话包装器是无用的,因为框架提供的 HttpSessionStateBase 已经是一个抽象类,可以在单元测试中轻松模拟。

    【讨论】:

    • 我通过编辑修正了一个错字,但后来全智者拒绝了它。看看你能不能找到它;)
    【解决方案2】:

    只是对最新帖子的 SetDataInSession 方法进行了一点修正。在我看来,这是一个优雅的解决方案!谢谢达林·迪米特洛夫。

    public static class SessionExtensions
    {
     public static T GetDataFromSession<T>(this HttpSessionStateBase session, string key) {
                return (T)session[key];
            }
    
            public static void SetDataInSession(this HttpSessionStateBase session, string key, object value) {
                session[key] = value;
            }
    }
    
    • 首先创建这个类,之后记得在调用这个方法的Controller类中引用它的命名空间。

    • 获取会话值时:

    string value = Session.GetDataFromSession&lt;string&gt;("key1");

    必须是与会话中持久化的对象兼容的类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 2015-04-09
      相关资源
      最近更新 更多