Welcome to BARMAGY Sign in | Join | Help

[SOS] Save Our State – Application Variables

                Continuing what we were saying… So session doesn’t work for you and you need something that is:

-          Variable shared by all users

-          Saved in server memory

 

That is exactly what application variables is all about, one bucket for all users, variables are labeled inside it. Interestingly enough application come very close to:

-          ASP.NET Caching – but what is very deferent is caching you can create dependency & time for expiration, additionally ASP.NET runtime controls memory allocated for cached object and remove the least accessed periodically.

-          Static variables: now let us stop for a while here cause this one is pretty inter,

o   static variables are inited when app domain is initiated (static ctors however is with first call to the class). App variables you control adding to them

o   static variables are not thread safe, app vars provide lock/unlock method for thread safety purposes.

o   Static variables are not shared across app domains/servers same applies for app vars.

o   Static variables increases your server memory consumption so app vars, however you can write simple code to dump app vars and you get quick statistics on what is your application is doing. That you cannot do with static variables (or you can using reflection which is not something straight forward)

 

 

So when to use application variables and for what?

                Use them when you want to share variables across classes, where this variable represents an application wide functionary (static variables are used to flag module wide functions). That is probably the most logical design decision

 

 

 Next is view state...

Published Tuesday, February 13, 2007 5:10 PM by KAL

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: [SOS] Save Our State – Application Variables

hmm... first, application variables are not really thread safe. Cache variables are, but those later ones can be removed from the memory any time.
Of course, application variables also will be removed if and when the application restarts, this can be as easy as modifying the web.config, "multiple"(I think default is 15, don't fully remember) modifications to the same ASP.NET file (aspx/ascx, etc...).

I agree that static variables are a real mess when used for web.

If your data is REALLY static, use web.config (application settings section or custom section.. choice is completely yours), or resource files (I prefer most for relatively long strings for example), otherwise, use cache. In all cases, uses static "Properties" to encapsulate your data, so that the most of the code doesn't care where the value really came from. One think to add to the "get" of such property is a check whether the value is OK (i.e. not null), and otherwise gets it from the right storage (DB maybe?), or if no alternate storage either set it to certain default value or throw exception (depends on your situation).
Wednesday, February 14, 2007 1:28 AM by Mohamed_Meligy

# re: [SOS] Save Our State – Application Variables

True App Vars are not thread safe, however with usage of lock/unlock they are, which is basiclly out of the box locking mechanism (easier to use when it comes to web development)

static variables however you will have to provide your own locking mechanism.

appreciate the comment though..
Wednesday, February 14, 2007 11:56 AM by Kal

What do you think?

(required) 
required 
(required)