Dr Schizo
Dr Schizo

Reputation: 4366

Static collection and asp.net

I am developing a generic layer that allows me to perform the basic save, delete and load operations however came across a point which I don't know the answer too and thought instantly of StackOverflow.

I have an asp.net app which references a seperate project (Business Layer which contains all my class definitions). One of my classes (ObjectFactory) takes care of instantiating objects and stores them in a static dictionary (privately). The idea being that if its already been instantiated it will retrieve its definition from the static collection. I have chosen to do this because I only want to get the propertyinfo[] of a class once as according to an MSDN article by Joel Pobar getting this information is quite expensive.

My question is, although the asp.net project references my class library which contains all my business logic, will this static collection be available globally? For example, if two users are concurrently on my site and access two seperate parts of my website which involves instantiating two seperate objects will the cache in ObjectFactory contain both items? Or is the static collection on a per user basis, i.e. the static collection will only contain items for a particular user?

As my business layer doesn't have access to the web cache I am unsure of whether to make my business layer reference System.Web and then insert into it?

Sorry if I may have confused anyone but I am more than happy to provide further info if required too.

Thanks in advance, Onam.

Upvotes: 2

Views: 842

Answers (3)

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64943

Regular static fields are available for all threads during a given application life-cycle.

Anyway, caution when using static fields. Pay attention to the fact that regular Dictionary<TKey, TValue> isn't thread-safe.

You'll need to consider blocking other threads to your dictionary while one is performing any write operation (add, remove...) or you'll have synchronization problems (for example, your properties' reflected information could be added twice - or more -).

Upvotes: 0

Chris Shain
Chris Shain

Reputation: 51359

In addition to @Steven's answer, I would add that the static collection will be emptied every time the application pool recycles, and if you end up running on multiple servers, each one will have it's own collection.

So in addition to making the items in the dictionary thread safe, you should use ConcurrentDictionary and it's GetOrAdd and AddOrUpdate methods to ensure thread-safe access to the dictionary itself, rtaher than the plain Dictionary class, which is not thread safe.

Upvotes: 1

Steven
Steven

Reputation: 172786

Objects that are stored in static variables are shared throughout the App Domain. All threads (all web requests) will use the same value. This means that those objects have to be thread-safe, otherwise you will be heading to failure, and -of course- this will only fail after you rolled out to production, because these problems will usually not show during development, and often not in staging as well (nobody is usually hardly really testing your system at that point).

Upvotes: 3

Related Questions