471,075 Members | 762 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

Static fields in ASP.NET pages

Hello

I'm writing an application in ASP.NET 1.1 and have come across a
problem using static fields in my page classes.

I have lots of controls on the page that all need to bind to the same
datasource, so I thought I'd create a singleton in the base class that
reads from the database and provides a DatasSet to all instances of the
various derived classes. I've discovered, though, that the static
values persist between roundtrips, presumably because the singleton
instance hasn't been released by the garbage collector. For this
reason, I've decided that using static in my ASP.NET pages is probably
unsuitable.

Does anyone have any recommendations for using static members in
ASP.NET, or is there a more appropriate method for implementing
singletons in ASP.NET?

Thanks a lot, Mark

Mar 23 '06 #1
3 1491
Mark,

Note, that static members persist not only between roundtrips of the same
session, but also between different sessions of the same application. This
means that multiple users working in the same time will be working on the
same objects.

Eliyahu

<ma**********@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Hello

I'm writing an application in ASP.NET 1.1 and have come across a
problem using static fields in my page classes.

I have lots of controls on the page that all need to bind to the same
datasource, so I thought I'd create a singleton in the base class that
reads from the database and provides a DatasSet to all instances of the
various derived classes. I've discovered, though, that the static
values persist between roundtrips, presumably because the singleton
instance hasn't been released by the garbage collector. For this
reason, I've decided that using static in my ASP.NET pages is probably
unsuitable.

Does anyone have any recommendations for using static members in
ASP.NET, or is there a more appropriate method for implementing
singletons in ASP.NET?

Thanks a lot, Mark

Mar 23 '06 #2
> Hello

I'm writing an application in ASP.NET 1.1 and have come across a
problem using static fields in my page classes.

I have lots of controls on the page that all need to bind to the same
datasource, so I thought I'd create a singleton in the base class that
reads from the database and provides a DatasSet to all instances of the
various derived classes. I've discovered, though, that the static
values persist between roundtrips, presumably because the singleton
instance hasn't been released by the garbage collector. For this
reason, I've decided that using static in my ASP.NET pages is probably
unsuitable.

Does anyone have any recommendations for using static members in
ASP.NET, or is there a more appropriate method for implementing
singletons in ASP.NET?

Thanks a lot, Mark


A singleton should not be "released by the garbage collector". It
should remain alive for the duretion of the application.
An asp.net application is a single application that keeps on running
until you explicitly stop it. During this lifetime it serves many
sessions consisting of many requests.

It is possible to use static members and singletons in asp.net, but you
need to know the effects: *all* users will the use the *same*
instances. If that is what you want, fine.

What you may want is static *methods*. You don't need to instantiate
the class, you can (or rather: need to) call these on the *type*.
Example:

public sealed class MyLibrary
{
private MyLibrary() { } // no public constructor needed

public static DataSet GetData(int id)
{
// ....
return ..;
}
}

and you call that with:

DataSet ds = MyLibrary.GetData(1);

Hans Kesting
Mar 23 '06 #3
Thanks Hans. Perhaps I ought to be a little more explicit with my
requirements.

My intention is that the singleton instance should be alive for the
duration of the request, on a per-user basis. When the page goes out of
scope, the singleton should be destroyed; or, more precisely, the data
should be reloaded when the page is loaded afresh by the server in
response to a fresh request (i.e., not a postback).

There are two problems: the singleton instance is shared across
threads, but I suspect I may be able to deal with that using the
ThreadStaticAttribute attribute; more importantly is the fact that the
singleton is surviving roundtrips to the server. If I implement
ThreadStaticAttribute then perhaps this won't be the case. This leads
to another question: is a thread created when a request is made of the
server, and does it end when the request has been fulfilled, or does it
end when the session goes out of scope?

As for your static method suggestion, what effect does marking it as
static have? Presumably the function will be executed every time it is
called, so that will defeat the whole reason I wanted a singleton in
the first place: to make sure the data is only loaded once from the
database per visit to the page (including postbacks).

Aargh!

Mark

Mar 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Fernando Lopes | last post: by
3 posts views Thread by Alexander | last post: by
6 posts views Thread by Victor Hadianto | last post: by
2 posts views Thread by utab | last post: by
5 posts views Thread by pittendrigh | last post: by
12 posts views Thread by chandu | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.