Hello,
I have a design problem involving class instances as global variables. To give you my background, I've programmed lots in Java, and most of it has been large class structures. I'm learning c# the hard way, but the similarities to Java are very helpful. However, in this case they are hurting, because I can't figure out the best way to store a class that can be accessed across the scope of an ASP.NET application.
To begin with, there are three types of users to my site - people viewing my site (WebUsers), people operating my site (Operators), and both operators and WebUsers are stored as the third type. A list of WebUsers is stored as a dictionary in a CustomerObserver class, and a dictionary of Operators is stored in an OperatorManager class. All the users are stored in a dictionary in the UserManager class. Finally, an instance of each of those three classes is stored in a Client class, and all the Clients are stored in a ClientManager class. This ClientManager is the one and only instance that will be allowed, application-wide. I'm trying to make this ClientManager accessible to all pages and classes, but have run into problem with both methods I've tried.
1) I initially kept all the function and members of these classes static, so that they could be accessible with just the class name. For example, I could get any WebUser object from anywhere with CustomerObserver.WebUser[UserID]. However, the scope of these static instances lasted only as long as the page I was viewing existed. As an example, if you viewed page1.aspx, the page would create a WebUser object for you and save it in the CustomerObserver's dictionary of WebUsers. However, when you visit page2.aspx and tried to access the WebUser dictionary, the WebUser object created in page1 has now disappeared.
2) I now have the ClientManager stored as an application variable, Application["ClientManager"]. It can be accessed by any page, but we run into problems in two ways. First, there's no "pretty and easy" way to access it quickly (such as CustomerObserver.WebUser[UserID] above), and only pages, not classes, can access it. So, each page that accesses some functionality of ClientManager must retrieve the application variable, pass it along to the class that needs it, perform any functionality that requires any member or sub-member of ClientManager, and then store the ClientManager back in the Application variable, in case it changed. Not only is this clunky, but it is also not thread-safe, so each time ClientManager is accessed it must lock the application until ClientManager is stored again. Scalability with this design is impossible.
Is there any way to have a global object variable accessible across an application?
--
Message posted via http://www.dotnetmonster.com