2

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?

1
  • i miss locks ... either lock(/**/) or ReaderWriterLockSlim-usage Commented Feb 5, 2010 at 10:19

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.