How static is static? - please help

  • Thread starter Thread starter Tilfried Weissenberger
  • Start date Start date
T

Tilfried Weissenberger

Hi,

I have had a strange situation:

In a back-end DLL for a website I have a function which loads a
cart-object from a HttpContext.Session into a static variable in a
class and serves this object to the visiting user.

While this works fine, as long as the DLL itself resides in the /bin
directory of the web-application, the static object seems to be
globally available to ALL USERS, if I move the DLL to the GAL!

In fact, the whole application started to do wicked things as long as
the DLL(s) were in the GAL. If I moved them to the web-app's /bin
directory and removed the entries in the GAL, the app worked fine.

As I wish to fix the cause, not the symptoms, I apprechiate anyone
shedding any light into this problem.

That was my code:

private static BusinessCase DefCart;

public static BusinessCase GetDefCart(bool AutoCreate) {
if (DefCart != null) return DefCart;

HttpContext ctx = HttpContext.Current;
if (ctx == null) return null;

if (ctx.Session[""+Shop.SessVars.BCase] == null) {
if (AutoCreate)
DefCart = CreateNewDefCart();
else
return null;
} else
DefCart = (BusinessCase)ctx.Session[""+Shop.SessVars.BCase];

return DefCart;
}


regards

Tilli
 
Quite static, as in state-ic. Static variables are comonly used to
maintain state for a class over all instantiations of that class. It helps
to think of them as state-ic. For example, a cafeteria manager app will
look at the date in the morning and see that is is thursday and set the menu
for the rest of the day in a static variable equal to hot dogs. All
instantiations that day will serve hot dogs.

In your app in the GAC, User1, User2, and user3 do not have 3 separate
static cart variables available, they are instead sharing a single static
variable. Your bin directory strategy is also flawed although it appears
to work when you are testing two separate shoppers, one per each app. They
get different carts because they are not instantiating the same class. App
bin libraries are invisible to App2 bin libraries and vice versa. However,
if you get two simultaneous shoppers in App1, they will be sharing the same
cart static variable.

Static variables are the wrong place to store instance specific data
(contents added by the separate shoppers) but a good place to store the
number of wheels on the cart for all shoppers.

John
http://MallSocket.com
 
<g> - thanks for pointing that out to me. Makes sense and also makes
me look like a complete sucker ;)

Funny that in all the books I have these guys seem to "assume" that
"eveyone" knows about the scope of a static member.

Thanks again!

regards, Tilli
 
Back
Top