tags:

views:

84

answers:

3

I am using the followign object in my asp.net page

private static Dictionary<string, List<Guid>> OpenNodes = new Dictionary<string,    List<Guid>>();  
//Page start
if(!OpenNodes.ContainsKey(Session.SessionID))  
{  
    List<Guid> list = new List<Guid>();  
    OpenNodes.Add(Session.SessionID, list);  
}

//User clicked on a node
Guid id = new Guid(e.Node.Value);  
tmpList = OpenNodes[Session.SessionID];  
tmpList.Add(id);  
OpenNodes[Session.SessionID] = tmpList;  

Is it agood practive or is there a similar 'better' way to achieve the same?

+4  A: 

A web server is multi threaded. You need to synchronize access to shared/static objects

Arve
+7  A: 

You should not replace Session with static fields. Session is much more flexible and less error-prone. You can easily make it work in Web farms (you can't do that with static fields). You can consider replacing some Application variables with static fields. It's important to know that static fields don't provide any thread safety mechanism out of the box and you should manually control (lock) them appropriately.

Mehrdad Afshari
+3  A: 

In this case, you can just use the Session state. I see no pro's in using the pattern you propose here.

If you really need to mimic some static field. You should use the Application state.

GvS