The best way to manage session data

I need to save some data in the Session. I wrote a lot of these properties:

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

When I should use this data, I should write the following code:

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

which seems to me so ugly because I would rather do this:

 FillOrder.Add(accordion.ID); 

but this way my value will not be saved in the session.

Can you come up with a better way to achieve the same result?

Thank you very much!

+4
source share
4 answers

I always use the wrapper class around an ASP.NET session to simplify access to session variables:

 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, eg 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 by itself in an ASP.NET session and allows you to access your session properties with a safe type from any class, for example, for example:

 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 (i.e. new List<string> )
  • it will save you a lot of casting types
  • you do not need to use hard-coded session keys throughout the application (for example, Session ["loginId"]
  • You can document your session elements by adding XML document comments to MySession properties.
+6
source

You can also use the extension method, but I think the M4N example might be better:

EDIT made it a generic type

 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); 
+1
source

You can write your own class that implements ICollection or IList, there you would do Add as Session [...] = ...

0
source

Using one class for all the session variables suggested by @ M4N is a good idea, although it can become a “God” class (in this case you can break it down into several classes implemented this way).

However, you can simply change your property implementation 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 need a setter.

0
source

Source: https://habr.com/ru/post/1387339/


All Articles