views:

699

answers:

4

I separated all direct session interaction into a separate class and made it static, because I didn't want to create a new object several times. However, i wish to make sure that there are not concurrency issues or other wonky suprises.

Here is the code:

public static class HttpHelper
{

    public static string Get(string key)
    {
        object value = HttpContext.Current.Request.QueryString[key];
        return (value == null) ? null : value.ToString();
    }


    public static string Post(string key)
    {
        object value = HttpContext.Current.Request.Form[key];
        return (value == null) ? null : value.ToString();
    }

    public static string Session(string key)
    {
        object value = HttpContext.Current.Session[key];
        return (value == null) ? null : value.ToString();
    }

    public static void ClearSession(string key)
    {
        HttpContext.Current.Session[key] = null;
    }

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

}
+1  A: 

If there were any functional problems with this, many applications would have failed long ago :)

However, I would point out that this model is not very good for unit testing. You might want to consider making the methods instance members and passing a Session provider into the constructor of this HttpHelper object.

Rex M
Thanks Rex, I may do that. As far as this model goes though. I just wanted to confirm that the session variables will NOT be shared between the users, I want to make sure that the session is unique to each user.
gnomixa
+1  A: 

Here's an answer from a similar question that may help you - it will enable you to avoid using keys to access the session values altogether and give you type-safe properties:

http://stackoverflow.com/questions/621549/how-to-access-session-variables-from-any-class-in-asp-net/621620#621620

Nick
The price in this case that I have to create a property for every var, I will stick with keys.
gnomixa
@gnomixa Your reasoning seems odd. The cost of creating a property for every one of your session objects is always going to be less than the cost of making a typo in your dictionary keys. "prop tab tab" done!
jfar
Read my comments to other answers: I am storing the keys in a static class, hence I don't type magic strings. Type Conversion is quite another matter though lol
gnomixa
A: 

Conceptually, you should be fine. Even in a partial-postback (EG, AJAX) scenario where you might expect race conditions, you should be OK. Session state uses a reader/writer lock to keep you safe.

I tend to do something similar in my projects, though I'm a fan of encapsulating out the actual valid session entries (keys, etc.) into properties. I find it keeps the app code more consistent, and -- more importantly -- removes the possibility of a typo on a magic key string that would make the app behave in wildly unexpected ways.

If you do something similar for app state, then you do have to make sure you lock and unlock values before setting them.

John Rudy
i store the keys, so no magic strings. Thanks!
gnomixa
A: 

I am not sure is this wrapper usefull,but I think you could do the following improvements Instead of

public static string Get(string key)
{
    object value = HttpContext.Current.Request.QueryString[key];
    return (value == null) ? null : value.ToString();
}

you can use

public static string Get(string key)
{
    return HttpContext.Current.Request.QueryString[key];
}

The same for Post method. And is it true that your Session method returns only string , if you can store any object in session by StoreInSession method?

ArsenMkrt
I have changed the session to return object type, however, this is not my original question. Please stay on target. Thanks!
gnomixa