views:

137

answers:

4

Hi all, i'm building a web application with asp.net c# and i have a class that i want to use in multiple pages witouth instantiate it every time. I need to load the data in it and never lose them during the user session time. I thought about the singleton-pattern but it shared the instance of the class beetween browsers. How can i solve the problem?

+6  A: 

Singleton is not the answer. Look at Session State, ViewState and Cookies.

Chuck Conway
Singleton *might* be the answer if he needs to gaurantee that reentrant calls only generate a single use of the class/object. That wouldnt be strictly needed unless the OP was using threading or some other method of running concurrent code for a single user session
GrayWizardx
i must have only one instance of the class for each user that open the page. My problem is that with singleton pattern and static instance i share the data beetween users and so browsers
Stefano
@Stefano If you use a session you can achieve what you want. Only one instance of the data across pages and for the life of the session. However, this will fail if the same user logs in from a different computer or different browser.
Chuck Conway
+1  A: 
UserData data = new UserData(somedata);

Session["UserData"] = data;

next page

UserData data = (UserData) Session["UserData"];
JungleFreak
A: 

If you have inproc session state you can use something like this

class Singleton
{
    static object locker = new Object();

    public static Singleton Instance
    {
        get
        {
            var inst = HttpContext.Current.Session["InstanceKey"] as Singleton;
            if (inst == null)
            {
                lock (locker)
                {
                    inst = HttpContext.Current.Session["InstanceKey"] as Singleton;
                    if (inst == null)
                    {
                        inst = new Singleton();
                        HttpContext.Current.Session["InstanceKey"] = inst;
                    }
                }
            }
            return inst;
        }
    }
}

Code can be improved, to avoid locking for all users. Don't know if this is a good idea to implement Singleton like that, I'd recommend you to see if you can design your code in other way.

Kirill Muzykov
A: 
public sealed class MyClass
    {
        private static object sync = new object();

        private MyClass()
        {
            test = new Random().Next(1, 5).ToString();
        }

        public string test;

        public static MyClass Instance()
        {
            if (!HttpContext.Current.Items.Contains("mykey"))
            {
                lock (sync)
                {
                    if (HttpContext.Current.Items["mykey"] == null)
                    {
                        HttpContext.Current.Items.Add("mykey", new MyClass());
                    }
                }
            }

            return (MyClass)HttpContext.Current.Items["mykey"];
        }
    }

this is my actually my sample test class. i load the page for the first time and i create the instance of the class. I reload the page and i have a new class instance. With debugger i can see that the Items collection is resetted... Why?

Stefano
Because HttpContext.Current.Items collection is not persisted between requests, any item you put there will live only from start of request to the end. This is why I'm using Session in my answer.
Kirill Muzykov
I would recommend you to read this posthttp://msdn.microsoft.com/en-us/magazine/cc300437.aspx
Kirill Muzykov
could be ok to use a static dictionary object that store for each user his class instance?
Stefano