singleton in an aspx app?

  • Thread starter Thread starter Sergei Shelukhin
  • Start date Start date
S

Sergei Shelukhin

Suppose I want to supply a singleton class object that will sit between my
intranet asp.net app and MS SQL database, doing al the chemistry with
connections etc inside of it.

The problem is that I don't really know how to keep the object persistent
for all the requests nade within the app.

I need the object to live for certain periods of time, and I need it to be
easily accessible from aspx.cs files' code.

I guess I will be creating it in global.asax on application start; where do
I store it then? How do I address it?
 
Store it in the Application Context...

Reference it through the application context.
 
Suppose I want to supply a singleton class object that will sit
between my intranet asp.net app and MS SQL database, doing al the
chemistry with connections etc inside of it.

Microsoft already has a set of classes which do this: Microsoft appplicaion
data blocks (you can download the classes from MSDN). Unless you're doing
something special, it's usually not a good idea to use a singleton
structure in ASP.NET.
 
Or in the "Cache" if that singleton can be weakly referenced. The only
difference between Cache and Application objects is that Application ones
are guarenteed to be there whenever you ask and Cached ones, you will need
to verify that they exist before using them.
Just another suggestion.
 
Sergei said:
Suppose I want to supply a singleton class object that will sit
between my intranet asp.net app and MS SQL database, doing al the
chemistry with connections etc inside of it.

The problem is that I don't really know how to keep the object
persistent for all the requests nade within the app.

I need the object to live for certain periods of time, and I need it
to be easily accessible from aspx.cs files' code.

I guess I will be creating it in global.asax on application start;
where do I store it then? How do I address it?

For an exellent article about the singleton pattern (in C#) see:
http://www.yoda.arachsys.com/csharp/singleton.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top