简体   繁体   English

管理会话数据的更好方法

[英]Better way to manage Session data

I need to persist in Session some data. 我需要在Session中保留一些数据。 I wrote many properties like that: 我写了很多这样的属性:

public List<string> FillOrder
{
    get { return Session[SessionKeys.QueryFillOrder] as List<string> ?? new List<string>(); }
    set { Session[SessionKeys.QueryFillOrder] = value; }
}

When I have to consume this data I have to write code like that: 当我必须使用这些数据时,我必须编写这样的代码:

List<string> fillOrder = FillOrder;
fillOrder.Add(accordion.ID);
FillOrder = fillOrder;

that seems to me so ugly, because I would prefer to do that: 在我看来这么难看,因为我更愿意这样做:

FillOrder.Add(accordion.ID);

but this way my value would not be saved back in Session. 但这样我的价值就不会保存在Session中。

Can you think of any better way to achieve the same result? 你能想出更好的方法来达到同样的效果吗?

Thank you very much! 非常感谢你!

I always use a wrapper class around the ASP.NET session to simplify access to session variables: 我总是在ASP.NET会话周围使用包装类来简化对会话变量的访问:

public class MySession
{
    // private constructor
    private MySession()
    {
       FillOrder = new List<string>();
    }

    // Gets the current session.
    public static MySession Current
    {
      get
      {
        var session = (MySession)HttpContext.Current.Session["__MySession__"];
        if (session == null)
        {
          session = new MySession();
          HttpContext.Current.Session["__MySession__"] = session;
        }
        return session;
      }
    }

    // **** add your session properties here, e.g like this:
    public List<string> FillOrder {get; set; }
    public string Property1 { get; set; }
    public DateTime MyDate { get; set; }
    public int LoginId { get; set; }
}

This class stores one instance of itself in the ASP.NET session and allows you to access your session properties in a type-safe way from any class, eg like this: 此类在ASP.NET会话中存储自身的一个实例,并允许您以类型安全的方式从任何类访问会话属性,例如:

MySession.Current.FillOrder.Add(accordion.ID);

int loginId = MySession.Current.LoginId;

string property1 = MySession.Current.Property1;
MySession.Current.Property1 = newValue;

DateTime myDate = MySession.Current.MyDate;
MySession.Current.MyDate = DateTime.Now;

This approach has several advantages: 这种方法有几个优点:

  • you can initialize your session variables in the constructor (ie new List<string> ) 你可以在构造函数中初始化你的会话变量(即new List<string>
  • it saves you from a lot of type-casting 它可以帮助您避免大量的类型转换
  • you don't have to use hard-coded session keys throughout your application (eg Session["loginId"] 您不必在整个应用程序中使用硬编码会话密钥(例如Session [“loginId”]
  • you can document your session items by adding XML doc comments on the properties of MySession 您可以通过在MySession的属性上添加XML doc注释来记录会话项

You can use an extension method as well, but I do think the example by M4N might be better: 您也可以使用扩展方法,但我认为M4N的示例可能更好:

EDIT made it a generic type EDIT使它成为通用类型

public static class Extensions
{
    public static void AddWithSession<T>(this List<T> list, T value, string key)
    {
        list.Add(value);
        HttpContext.Current.Session[key] = list;
    }
}

str.AddWithSession(accordion.ID,SessionKeys.QueryFillOrder);

您可以编写一个实现ICollection或IList的自己的类,在那里您将实现Add as Session [...] = ...

Using a single class for all Session variables as suggested by @M4N is a good idea, though it risks becoming a "God" class (in which case you could partition into several classes implemented in this way). 对@ M4N建议的所有Session变量使用单个类是一个好主意,虽然它有可能成为“上帝”类(在这种情况下,你可以分成几个以这种方式实现的类)。

However you could just change your property implemetation as follows: 但是,您可以按如下方式更改属性实现:

public List<string> FillOrder 
{     
    get 
    { 
        List<string> result = Session[SessionKeys.QueryFillOrder] as List<string>;
        if (result == null)
        {
            result = new List<string>();
            Session[SessionKeys.QueryFillOrder] = result;
        }
        return result;
    }     
    set { Session[SessionKeys.QueryFillOrder] = value; } 
} 

In this example, you probably don't want a setter. 在此示例中,您可能不需要setter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM