简体   繁体   中英

storing an object in session

I need to save a few things in the session. At the moment, I'm using InProc session on my development machine but I want to deploy using SQL server session and then Azure's AppFabric session.

I created an object that contains several properties, all primitive types. I'm handling the read of session like this:

SessionObject TheCurrentSession = 
    HttpContext.Current.Session["UserAppSession"] as SessionObject;

Basically, I'm wrapping session variables in an object that's serialized and deserialized each time the session loads and saves.

I'm wondering if it would be better to store each property in its native format and have the wrapping object read/save each of its properties in the session as a native type.

So for example, I'd have a method called RecreateSession like this:

public class SessionObject
{
   public void RecreateSession()
   {
        this.SessionObjectProperty1 = 
                         HttpContext.Current.Session["SessionObjectProperty1"];
        this.SessionObjectProperty2 = ...;
        this.SessionObjectProperty3 = ...;
   }
}

I think doing so would prevent the serialization/deserialization process and could make the values accessible directly in other parts of the code (ie. an HTTP module).

Is it worth it to change my session implementation? What are the best practices for this?

Thanks for your suggestions.

Is it worth it to change my session implementation?

Only if it makes it easier for you to use.

What are the best practices for this?

You pretty much are already doing them. Creating a single object to hold several related properties (that are likely to be used together) and storing it in session instead of a bunch of separate session properties makes sense.

我总是创建一个自定义对象,其中每个属性引用一个特定的会话项......我认为它是最好的选择。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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