1

I'm using in my asp.net web site, a singleton class to the users information.

I would like to know, if I put this web site online, when the users start to login, this this class will store the data to only one user.

This is my class:

public class User
{
    private static User instance;        
    private User(){}

    public static User Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new User();
            }
            return instance;
        }
    }

    public Institute LoggedInstitute { get; set; }
    public List<Institute> Institutes { get; set; }
}
abeppler
  • 13
  • 4

3 Answers3

0

There will only be multiple instances of your class if your application is used in a web farm scenario. Refer to this thread: Are static class instances unique to a request or a server in ASP.NET?

William Xifaras
  • 5,212
  • 2
  • 19
  • 21
  • So, just to confirm, if I log in the application, using my credentials, and after me another user log in the application, it will get my user data? – abeppler Jun 23 '15 at 20:23
  • I don't understand why you want to use a static to store the User? If you want this information to be unique per session - you should store this information in Session. – William Xifaras Jun 23 '15 at 20:34
  • Because the data volumn size – abeppler Jun 23 '15 at 20:37
0

when the users start to login, this this class will store the data to only one user.

There will be only one instance of User exists in your application.

Storing each user information in Singleton is a very very very bad idea for web application. Why?

Multiple users request your web application, and they all will share the same instance of User. I guarantee that it is not what you want.

Ideally, in web application, you want to create Principal object, and store it in HttpContext.

FYI: we use Singleton in web application to store information (mostly never change) which is used beginning of the application until the end.

Win
  • 61,100
  • 13
  • 102
  • 181
0

Static fields in C# (and I believe in all .NET languages), such as the one used to hold the singleton instance of your User class, are "unique" within an AppDomain.

If, and only if, you have multiple AppDomains, whether in the same process, in another process, or on another machine (such as in a web farm), as William pointed out in his answer, you could have multiple instances.

Scope of static variables in ASP.NET sites

In .Net is the 'Staticness' of a public static variable limited to an AppDomain or the whole process?

Community
  • 1
  • 1
John K
  • 661
  • 6
  • 10