Click here to Skip to main content
15,885,932 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi everyone,

I'm developing an MVC3 application;

I need a series of variables which contains necessary info regarding the specific user (and user operations) to be accessed by almost any part of my application.

I came up with the solution of creating the necessary Static Classes, with the properties referring to specific Session Variables, so that once they are set, they'll (theorically) stay the same until changed/removed; for example:

C#
public static class TestClass
    {

        public static string TestProperty {
            get{
                return HttpContext.Current.Session["MyApplication_TestProperty"].ToString();
            }
            set{
                HttpContext.Current.Session["MyApplication_TestProperty"] = value;
            }
        }

    }


Is this a good (or at least decent) solution, or is it better to directly post it in the Hall of Shame and find another way??

Any suggestion or better solution?

Thank you in advance,
Alberto
Posted
Updated 29-Nov-11 5:38am
v2

There is nothing wrong with the idea, but there is a bug in the sample (see the fix below). Another problem is using immediate constant and repeating it twice. How are your going to support it? All constants should be explicit, except some trivial ones like 0, 1, or null. But it could be fixed like this:

C#
public static class TestClass
    {
        const string SessionId = "MyApplication_TestProperty"; //a little better

        public static string TestProperty {
            get{
                return HttpContext.Current.Session[SessionId].ToString();
            }
            set{
                HttpContext.Current.Session[SessionId] = value; //bug fixed.
            }
        }

    }


It will work but is not flexible. More universal and general approach is using the singleton pattern, see:
http://en.wikipedia.org/wiki/Singleton_pattern[^],
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)[^].

This is a good article with implementation samples in C#. I suggest you learn how to use the pattern:
http://csharpindepth.com/Articles/General/Singleton.aspx[^].

Good luck.
—SA
 
Share this answer
 
Comments
Alberto Biasiutti 29-Nov-11 13:12pm    
Thank you for pointing out the Singleton pattern, I'll give a look to it.

Alberto
Sergey Alexandrovich Kryukov 29-Nov-11 13:55pm    
You're welcome, Alberto.
You won't have any problems with that, please try.
Good luck, call again.
--SA
Alberto Biasiutti 30-Nov-11 3:29am    
By using the singleton pattern approach, can I mantain the values across subsequent requests? This is why I thought to use session: I can access it from wherever I want (through the static classes) and it will stay there for all the time I need, even for subsequent requests.
If it works for what you need it for then why not.

One thing to bear in mind is that the HttpContext.Current.Session will only be availble to you in the main thread spawned by MVC. ie. if you spawn another thread to do some work these properties will not be available to you.

Using Static classes like this also mean that you will not be able to easily unit test anything that depends on a call to one of these properties.

But if you're not going to be using additional threads that make calls to these properties and you don't want to write unit tests, why not.
 
Share this answer
 
Comments
Alberto Biasiutti 29-Nov-11 13:17pm    
Thank you for pointing the problem with spawned threads.. teorically it won't be a problem in my case, but I'll evaluate it before proceeding this way.

Thank you again!

Alberto

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900