473,804 Members | 2,296 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP.NET Singleton usage

Hi

I'm just starting with singleton and would like to implement in my new
web app.

I have a question

lets say i create a singleton DataHelper that holds a static
SqlConnection object to share
and on my page I do

SqlConnection cn = DataHelper.GetC onnection()
when 2 client request the page making a db call will i get an error
because the connection might be closed or in a state that prevents one
request from using it ?

What would be the advantage of using singleton objects over putting my
objects in application variables?
Thanks

Jan 13 '07 #1
7 2240
fredd00 wrote:
lets say i create a singleton DataHelper that holds a static
SqlConnection object to share
and on my page I do

SqlConnection cn = DataHelper.GetC onnection()

when 2 client request the page making a db call will i get an error
because the connection might be closed or in a state that prevents one
request from using it ?
You will get some weird errors occasionally.

Singleton is not suited for this type of usage.

To get it working you would need to synchronize access and that
would cripple performance.

Just use connection pool.
What would be the advantage of using singleton objects over putting my
objects in application variables?
Among other thing Singleton can be used in code that are
not web specific.

Arne
Jan 14 '07 #2
Hello fredd00,

Just add to Arne, using variables isn;t good idea either. Because asp.net
page has a limited live time

---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

fWhat would be the advantage of using singleton objects over putting
fmy objects in application variables?
Jan 14 '07 #3
Michael Nemtsev wrote:
fWhat would be the advantage of using singleton objects over putting
fmy objects in application variables?
Just add to Arne, using variables isn;t good idea either. Because
asp.net page has a limited live time
I actually read that as having it in the Application object.

Arne
Jan 14 '07 #4
fred00,
If you look at best-practices code like the SqlHelper V2 (Data Access
Application Block) you will see that all the methods are static. The
SqlParameteCach e Class therein is also static, so that it can cache
SqlParameter collections across multiple pages and multiple requests.

Singleton provides an instance of a class, but "only one" instance. Take a
look at the code I mentioned and you will see the difference.

Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"fredd00" wrote:
Hi

I'm just starting with singleton and would like to implement in my new
web app.

I have a question

lets say i create a singleton DataHelper that holds a static
SqlConnection object to share
and on my page I do

SqlConnection cn = DataHelper.GetC onnection()
when 2 client request the page making a db call will i get an error
because the connection might be closed or in a state that prevents one
request from using it ?

What would be the advantage of using singleton objects over putting my
objects in application variables?
Thanks

Jan 14 '07 #5
I used to have a helper class that returned a SQLConnection so i guess
i'm already using pooling.

If i want to cache data (let's say dataset) would it be better to
cache it in a singleton or in the Cache object?

I want to use the Data Access Application block , is there data caching
in it ?

thanks
Peter wrote:
fred00,
If you look at best-practices code like the SqlHelper V2 (Data Access
Application Block) you will see that all the methods are static. The
SqlParameteCach e Class therein is also static, so that it can cache
SqlParameter collections across multiple pages and multiple requests.

Singleton provides an instance of a class, but "only one" instance. Take a
look at the code I mentioned and you will see the difference.

Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"fredd00" wrote:
Hi

I'm just starting with singleton and would like to implement in my new
web app.

I have a question

lets say i create a singleton DataHelper that holds a static
SqlConnection object to share
and on my page I do

SqlConnection cn = DataHelper.GetC onnection()
when 2 client request the page making a db call will i get an error
because the connection might be closed or in a state that prevents one
request from using it ?

What would be the advantage of using singleton objects over putting my
objects in application variables?
Thanks
Jan 14 '07 #6
I think maybe you're trying too hard to find a reason to use a singleton.

I agree that it's use w/in a web page is kinda pointless because all
the server side things are rebuilt on every trip to the server. And
secondly, saving things to session means you are capturing state. State
means, as I think about it, the specific values of a given object
instance. A singleton is not intended for this kind of use. Unless you
are, on any given trip to the server, creating some number of redundant
objects, I just don't see a need for a singleton.

But here is an example of how we use it. We've created a service - it
runs in the background all the time. This service takes documents and
stores them in our doc archive. The DocImporter is a singleton
instance. Whenever that service is called the singleton ensures that
only one DocImporter object is ever created. This DocImporter object
controls the overall importing process in turn uses the factory pattern
to instantiate any of a number of document type objects, dependent on
the document being imported, of course and one for each document being
imported.

Oh, Additionally, the DocImporter spawns an error logging object when
an error is generated. This ErrorLogger is also a singleton. Errors
generated in the importing process are queued up and wait to be logged
by the one and only ErrorLogger object.

Perhaps the main point here is that the singleton object lives
indefinitely and no client needs it's own specific version of it.

On 2007-01-13 17:32:10 -0600, "fredd00" <fr************ *@gmail.comsaid :
Hi

I'm just starting with singleton and would like to implement in my new
web app.

I have a question

lets say i create a singleton DataHelper that holds a static
SqlConnection object to share
and on my page I do

SqlConnection cn = DataHelper.GetC onnection()
when 2 client request the page making a db call will i get an error
because the connection might be closed or in a state that prevents one
request from using it ?

What would be the advantage of using singleton objects over putting my
objects in application variables?
Thanks

Jan 14 '07 #7
See my blog entry:

10/24/2005
Web Session Wrapper for storing and retrieving objects
http://sholliday.spaces.live.com/blog/

In the web environment, I created a singleton which piggyback's off of the
Session object.
It could be an application object as well.

...

Also search for "WeakRefere nce" ............ it allows a holding mechanism
but an "out" if memory demands become too much.


"fredd00" <fr************ *@gmail.comwrot e in message
news:11******** *************@a 75g2000cwd.goog legroups.com...
I used to have a helper class that returned a SQLConnection so i guess
i'm already using pooling.

If i want to cache data (let's say dataset) would it be better to
cache it in a singleton or in the Cache object?

I want to use the Data Access Application block , is there data caching
in it ?

thanks
Peter wrote:
fred00,
If you look at best-practices code like the SqlHelper V2 (Data Access
Application Block) you will see that all the methods are static. The
SqlParameteCach e Class therein is also static, so that it can cache
SqlParameter collections across multiple pages and multiple requests.

Singleton provides an instance of a class, but "only one" instance.
Take a
look at the code I mentioned and you will see the difference.

Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"fredd00" wrote:
Hi
>
I'm just starting with singleton and would like to implement in my new
web app.
>
I have a question
>
lets say i create a singleton DataHelper that holds a static
SqlConnection object to share
and on my page I do
>
SqlConnection cn = DataHelper.GetC onnection()
>
>
when 2 client request the page making a db call will i get an error
because the connection might be closed or in a state that prevents one
request from using it ?
>
What would be the advantage of using singleton objects over putting my
objects in application variables?
>
>
Thanks
>
>

Jan 15 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
12481
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a) explicitly make the arbitrary class's constructor and destructor private b) declare the Singleton a friend of the arbitrary class
3
2502
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic what sort of problems/issues should be considered? Also, I see that a singleton needs to be set up with certain data such as file name, database URL etc. What issues are involved in this, and how would you do this? If someone knows about the...
5
4717
by: LinuxGuy | last post by:
Hi, I have come across singleton class with some member variables are declared as static with public scope. As singleton class always return only one instance. ie. single copy of object is maintained all the time. can someone tell me the reason behind declaring those variables as static one.
7
3619
by: Stephen Brown | last post by:
I have some strange behavior on my web server that seems to point to garbage collection. I have a singleton that tracks web activity on my web site. The singleton works great, except that it restarts periodically. The web services have not been restarted and the error log shows no problems. It is the same problem on 2 different servers, but is worse on the most used server. On the most used server, it gets restarted 5 to 10 times a day...
21
2472
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought maybe a Template can be use for that, but C# does not support Templates (will be C# generics in mid 2005). Does anyone have a solution on how the singleton pattern can be written, in C#, as a framework/ infrastructure class, so users can use this...
3
1405
by: Jeremy Cowles | last post by:
Will the keyword "Me" ever be able to be the target of an assignment? Because singleton classes would be MUCH simplier if you could just point Me to a different address. For example: Class SingletonClassic Private Shared sm_Instance as SingletonClassic Public Shared Sub GetInstance() Return sm_Instance End Sub
6
2279
by: Manuel | last post by:
Consider the classic singleton (from Thinking in C++): ----------------------------------------------------- //: C10:SingletonPattern.cpp #include <iostream> using namespace std; class Singleton { static Singleton s; int i; Singleton(int x) : i(x) { }
15
3058
by: Nick Keighley | last post by:
Hi, I found this in code I was maintaining template <class SingletonClass> SingletonClass* Singleton<SingletonClass>::instance () { static SingletonClass _instance; return &_instance; }
7
2844
by: fredd00 | last post by:
Hi I'm just starting with singleton and would like to implement in my new web app. I have a question lets say i create a singleton DataHelper that holds a static SqlConnection object to share and on my page I do
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10346
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9173
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.