473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Singleton pattern with webservices

I have inherited a Business Object architecture that makes heavy use
of the Singleton design patter. For example the data access layer
class is implemented as a static Singleton "object", there are also
other business objects logic and security supporting classes that are
all implemented using the Singleton patter. I 'm attempting to make
use of this class library in a webservice application. The problem is
that some of the Singleton objects use static variables to store data
and that will cause problems in the webservice environment where all
static variables are equivalent to application level variables. I've
done some research and found an alternate way of instantiating these
Singleton objects.
I would like to get your opinion as to how reliable this method will
be (when used in the webservice and a single threaded windows app) and
any potential pitfalls. Looks like this type of Singleton
instantiation will guerantee that each web app thread will have its
own static variables.

TIA for your time.

(Found at http://ehsanbraindump.blogspot.com/2...endment-1.html)

public class ConnectionManager
{
/* ConnectionManager implementation */

//Singleton logic:
private ConnectionManager() { }

public static ConnectionManager Instance
{
get
{
if (Thread.GetData(Thread.GetNamedDataSlot("Connectio nManager")) ==
null)
Thread.SetData(Thread.GetNamedDataSlot("Connection Manager"), new
ConnectionManager());

return
(ConnectionManager)System.Threading.Thread.GetData (Thread.GetNamedDataSlot("ConnectionManager"));
}
}
}

Rich

Jul 18 '07 #1
3 2795
koredump <ko******@interia.plwrote:
I have inherited a Business Object architecture that makes heavy use
of the Singleton design patter. For example the data access layer
class is implemented as a static Singleton "object", there are also
other business objects logic and security supporting classes that are
all implemented using the Singleton patter. I 'm attempting to make
use of this class library in a webservice application. The problem is
that some of the Singleton objects use static variables to store data
and that will cause problems in the webservice environment where all
static variables are equivalent to application level variables. I've
done some research and found an alternate way of instantiating these
Singleton objects.
I would like to get your opinion as to how reliable this method will
be (when used in the webservice and a single threaded windows app) and
any potential pitfalls. Looks like this type of Singleton
instantiation will guerantee that each web app thread will have its
own static variables.
Well, firstly I'd say it no longer counts as a singleton - a factory,
more like.

It would be simpler just to use the ThreadStatic attribute, to be
honest, but I dare say it'll work.

However, you *do* need to be careful of ASP.NET thread agility. In some
situations (and it's version-dependent) a request can hop around
multiple different threads during its lifetime. If it's important that
you use a single ConnectionManager for all the different parts of the
life cycle, you'll need to rethink this.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 18 '07 #2
Thanks for that Jon. But...
>In situations (and it's version-dependent) a request can hop around
multiple different threads during its lifetime.
My understanding of how the aspnet worker process works is as follows:
There is a pool of threads that serves all the web requests.
Once a thread is assigned to process a web request it's removed fromt the
threadpool, it then processes the web request from start to finish. After it
is done with the web request, the thread is return to the thread pool.
Are you saying that one thread could be serving multiple requests at the
same exact time?


Jul 19 '07 #3
koredump <te**@test.comwrote:
Thanks for that Jon. But...
In situations (and it's version-dependent) a request can hop around
multiple different threads during its lifetime.

My understanding of how the aspnet worker process works is as follows:
There is a pool of threads that serves all the web requests.
Once a thread is assigned to process a web request it's removed fromt the
threadpool, it then processes the web request from start to finish. After it
is done with the web request, the thread is return to the thread pool.
Are you saying that one thread could be serving multiple requests at the
same exact time?
I'm saying that parts of the same web request's lifecycle can occur on
different threads in some situations, at least with ASP.NET 1.1.

I was investigating this recently and posted my results on a discussion
topic: see http://forum.springframework.net/showthread.php?t=572

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 19 '07 #4

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

Similar topics

1
by: Richard A. DeVenezia | last post by:
foo() generates elements with event handlers that invoke foo function properties. Is this an abhorrent or misthought pattern ? It allows just the one occurence of identifier /foo/ to be changed...
3
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...
11
by: Daniel Billingsley | last post by:
Let's say I'm writing a business app and I want there to be only one instance of the Customer object for each particular customer (representing a database record) being edited. Would it be...
21
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...
13
by: Robert W. | last post by:
At the beginning of my C# days (about 6 months ago) I learned about the Singleton pattern and implemented for Reference data, such as the kind that appears in an Options dialog box. My Singleton...
2
by: Chris Murphy via DotNetMonster.com | last post by:
Hey guys, I've been hitting a brick wall with a problem I've come accross in developing an application. Background: The application uses one primary class that I'm trying to implement with the...
14
by: Paul Bromley | last post by:
Forgive my ignorance on this one as I am trying to use a Singleton class. I need to use this to have one instance of my Class running and I think I understand how to do this. My question however is...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
29
by: Ugo | last post by:
Hi guys, how do you make a singleton access class? Do you know a better way of this one: var singletonClass = (function( ) { // Private variable var instance = null;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.