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

Home Posts Topics Members FAQ

Singleton

Hello,

I am trying to model a session object that is essentially a collection of
different items (connection string, user name, maps etc.) I would like this
session object to be available to other objects within my client
application.

I can do one of two things (1) make the session object a singleton (2) pass
the session object to the methods that need them. Option 2 is a bit more
complicated and messy then option 1. My other goal in this excercise is to
be able to reuse these other objects within a web or application or server
environment, a singleton object would, I believe limit my scalelability and
perhaps not work at all because what I really need is an session singleton
per user session.

Any comments on how I might achive option 1 in a Web environment so that I
can reuse the other objects?

Thanks,
Dan
Nov 20 '05 #1
12 2457
Hi Solex,

My first thought would be a singleton SessionCollecti on object which
provides access to the individual Session objects.

My second thoughts will come when you tell me it won't work for you. ;-)

Regards,
Fergus
MVP [Windows Start button, Shutdown dialogue]
Nov 20 '05 #2
Cor
Hi Solex,
Long time not seen
nice to see you back
Cor
Nov 20 '05 #3
Solex,
I would implement it externally as a singleton, however internally I would
use a Strategy Pattern, possible app.config/web.config based that indicated
to use a private shared field for the backing store or the
HttpContext.Ses sion or a ThreadStatic variable.

See the following on how to create new sections in the app.config/web.config
via the configSections section.

http://msdn.microsoft.com/library/de...onhandlers.asp

and:
http://msdn.microsoft.com/library/de...ionsschema.asp

Also read about the System.Configur ation.Configura tionSettings class and
other classes in the System.Configur ation namespace.

Hope this helps
Jay
Hope this helps
Jay

"solex" <so***@nowhere. com> wrote in message
news:uY******** *****@tk2msftng p13.phx.gbl...
Hello,

I am trying to model a session object that is essentially a collection of
different items (connection string, user name, maps etc.) I would like this session object to be available to other objects within my client
application.

I can do one of two things (1) make the session object a singleton (2) pass the session object to the methods that need them. Option 2 is a bit more
complicated and messy then option 1. My other goal in this excercise is to be able to reuse these other objects within a web or application or server environment, a singleton object would, I believe limit my scalelability and perhaps not work at all because what I really need is an session singleton
per user session.

Any comments on how I might achive option 1 in a Web environment so that I
can reuse the other objects?

Thanks,
Dan

Nov 20 '05 #4
Hi Cor,

Not sure if I am the person, I am relatively new to this group.

Dan

"Cor" <no*@non.com> wrote in message
news:3f******** *************** @reader20.wxs.n l...
Hi Solex,
Long time not seen
nice to see you back
Cor

Nov 20 '05 #5
Jay,

Thanks for your response, the App.config may work for applications settings
but will not work for user settings which may change during app execution.

The strategy pattern is an interesting idea where the Context Object would
be implemented as a Singleton. But is it possible to create a singelton
within the context of a single session. What I really need is for every
session to create its own Session object to be used by the other objects. I
cannot use a singleton that is global to the application on the Web side.

Thanks,
Dan

"Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in message
news:ux******** ******@TK2MSFTN GP09.phx.gbl...
Solex,
I would implement it externally as a singleton, however internally I would
use a Strategy Pattern, possible app.config/web.config based that indicated to use a private shared field for the backing store or the
HttpContext.Ses sion or a ThreadStatic variable.

See the following on how to create new sections in the app.config/web.config via the configSections section.

http://msdn.microsoft.com/library/de...onhandlers.asp
and:
http://msdn.microsoft.com/library/de...ionsschema.asp
Also read about the System.Configur ation.Configura tionSettings class and
other classes in the System.Configur ation namespace.

Hope this helps
Jay
Hope this helps
Jay

"solex" <so***@nowhere. com> wrote in message
news:uY******** *****@tk2msftng p13.phx.gbl...
Hello,

I am trying to model a session object that is essentially a collection of different items (connection string, user name, maps etc.) I would like

this
session object to be available to other objects within my client
application.

I can do one of two things (1) make the session object a singleton (2)

pass
the session object to the methods that need them. Option 2 is a bit more complicated and messy then option 1. My other goal in this excercise is

to
be able to reuse these other objects within a web or application or

server
environment, a singleton object would, I believe limit my scalelability

and
perhaps not work at all because what I really need is an session singleton per user session.

Any comments on how I might achive option 1 in a Web environment so that I can reuse the other objects?

Thanks,
Dan


Nov 20 '05 #6
solex,
Thanks for your response, the App.config may work for applications settings but will not work for user settings which may change during app execution. No no no. (hangs shaking head muttering)

The app.config is used to decide which strategy to use by the MySession
object it has nothing per se do with runtime application settings or user
settings!

Remember app.config is for application settings used at start up. It should
not be used for user settings. Especially that change during app execution,
as app.config is for startup information!
session to create its own Session object to be used by the other objects. I cannot use a singleton that is global to the application on the Web side. Yes you can! Let me try to explain what I stated, as what I stated may not
have been very clear.

The easiest way for me to explain other than what I previously stated is an
example:

Untested, non syntax checked VS.NET 2003 sample:

Public NotInheritable Class MySession
Inherits DictionaryBase

#Region " Singleton Support"

' note the use of the strategy variable, not an instance variable
' this is where I stated 'internally I would use'
Private Shared Readonly m_strategy As MySessionStrate gy

Shared Sub New()
' use app.config/web.config to determine the strategy
Dim strategy As String =
ConfigurationSe ttings.AppSetti ngs("sessionStr ategy")
Dim type As Type = Type.GetType(st rategy)
m_strategy = DirectCast(Acti vator.CreateIns tance(type),
MySessionStrate gy)
End Sub

' Needs to be friend as the strategy may create instances
Friend Sub New()
End Sub

' note the instance property looks like a normal Singleton
' but the strategy actually defines the behavior
' this is where I stated 'externally I would use'
Public Shared Readonly Property Instance() As MySession
Get
Return m_strategy.Inst ance
End Get
End Property

#End Region

Default Public Property Item(ByVal key As String) As Object
Get
Return InnerHashtable( key)
End Get
Set(ByVal value As object)
InnerHashtable( key) = value
End Set
End Property

' and other 'session' level methods.

End Class

Friend MustInherit Class MySessionStrate gy

Public MustOverride ReadOnly Property Instance() As MySession

End Class

Then you need to implement a different MySessionStrate gy class for ASP.NET
apps, Windows Services or Windows programs. Where the strategy decides how
to store the Instance data safely for each of these type of programs.

If you wanted to support both Application & Session like variables you can
add more methods to MySessionStrate gy and implement them there. The
MySession object would have shared methods that then delegated to the
MySessionStrate gy versions.

Rather than have sessionStrategy in appSettings, I would actually create a
custom section that is specific to the library where MySession is
implemented. I would more than likely implement MySession in its very own
class library assembly! (as the constructor is Friend and it needs to be
protected, the other option is to make MySessionStrate gy nested inside of
MySession, giving it the ability to create a new MySession if the derived
classes need to...

No matter the type of program you use it like a normal singleton

MySession.Insta nce("bla bla") = 100

The strategy actually decides how the data is stored nicely for each
'session'.

Hope this helps
Jay

"solex" <so***@nowhere. com> wrote in message
news:OX******** ******@TK2MSFTN GP09.phx.gbl... Jay,

Thanks for your response, the App.config may work for applications settings but will not work for user settings which may change during app execution.

The strategy pattern is an interesting idea where the Context Object would be implemented as a Singleton. But is it possible to create a singelton
within the context of a single session. What I really need is for every
session to create its own Session object to be used by the other objects. I cannot use a singleton that is global to the application on the Web side.

Thanks,
Dan

"Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in message news:ux******** ******@TK2MSFTN GP09.phx.gbl...
Solex,
I would implement it externally as a singleton, however internally I would
use a Strategy Pattern, possible app.config/web.config based that indicated
to use a private shared field for the backing store or the
HttpContext.Ses sion or a ThreadStatic variable.

See the following on how to create new sections in the

app.config/web.config
via the configSections section.

http://msdn.microsoft.com/library/de...onhandlers.asp

and:

http://msdn.microsoft.com/library/de...ionsschema.asp

Also read about the System.Configur ation.Configura tionSettings class and
other classes in the System.Configur ation namespace.

Hope this helps
Jay
Hope this helps
Jay

"solex" <so***@nowhere. com> wrote in message
news:uY******** *****@tk2msftng p13.phx.gbl...
Hello,

I am trying to model a session object that is essentially a collection of different items (connection string, user name, maps etc.) I would
like this
session object to be available to other objects within my client
application.

I can do one of two things (1) make the session object a singleton (2)

pass
the session object to the methods that need them. Option 2 is a bit more complicated and messy then option 1. My other goal in this excercise
is to
be able to reuse these other objects within a web or application or

server
environment, a singleton object would, I believe limit my
scalelability and
perhaps not work at all because what I really need is an session

singleton per user session.

Any comments on how I might achive option 1 in a Web environment so
that I can reuse the other objects?

Thanks,
Dan



Nov 20 '05 #7
Jay,

Thank you for your detailed example. Sorry but I have a few more questions:

(1) First and foremost is when using a singleton in a web environment does
this not mean that for every request everyone will be getting the same
instance of MySessionStrate gy?

(2) Also what is Instance supposed to return in MySessionStrate gy?

Thanks,
Dan


"Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in message
news:Oa******** ******@tk2msftn gp13.phx.gbl...
solex,
Thanks for your response, the App.config may work for applications settings
but will not work for user settings which may change during app execution. No no no. (hangs shaking head muttering)

The app.config is used to decide which strategy to use by the MySession
object it has nothing per se do with runtime application settings or user
settings!

Remember app.config is for application settings used at start up. It should not be used for user settings. Especially that change during app execution, as app.config is for startup information!
session to create its own Session object to be used by the other objects.
I
cannot use a singleton that is global to the application on the Web
side. Yes you can! Let me try to explain what I stated, as what I stated may not
have been very clear.

The easiest way for me to explain other than what I previously stated is an example:

Untested, non syntax checked VS.NET 2003 sample:

Public NotInheritable Class MySession
Inherits DictionaryBase

#Region " Singleton Support"

' note the use of the strategy variable, not an instance variable
' this is where I stated 'internally I would use'
Private Shared Readonly m_strategy As MySessionStrate gy

Shared Sub New()
' use app.config/web.config to determine the strategy
Dim strategy As String =
ConfigurationSe ttings.AppSetti ngs("sessionStr ategy")
Dim type As Type = Type.GetType(st rategy)
m_strategy = DirectCast(Acti vator.CreateIns tance(type),
MySessionStrate gy)
End Sub

' Needs to be friend as the strategy may create instances
Friend Sub New()
End Sub

' note the instance property looks like a normal Singleton
' but the strategy actually defines the behavior
' this is where I stated 'externally I would use'
Public Shared Readonly Property Instance() As MySession
Get
Return m_strategy.Inst ance
End Get
End Property

#End Region

Default Public Property Item(ByVal key As String) As Object
Get
Return InnerHashtable( key)
End Get
Set(ByVal value As object)
InnerHashtable( key) = value
End Set
End Property

' and other 'session' level methods.

End Class

Friend MustInherit Class MySessionStrate gy

Public MustOverride ReadOnly Property Instance() As MySession

End Class

Then you need to implement a different MySessionStrate gy class for ASP.NET
apps, Windows Services or Windows programs. Where the strategy decides how
to store the Instance data safely for each of these type of programs.

If you wanted to support both Application & Session like variables you can
add more methods to MySessionStrate gy and implement them there. The
MySession object would have shared methods that then delegated to the
MySessionStrate gy versions.

Rather than have sessionStrategy in appSettings, I would actually create a
custom section that is specific to the library where MySession is
implemented. I would more than likely implement MySession in its very own
class library assembly! (as the constructor is Friend and it needs to be
protected, the other option is to make MySessionStrate gy nested inside of
MySession, giving it the ability to create a new MySession if the derived
classes need to...

No matter the type of program you use it like a normal singleton

MySession.Insta nce("bla bla") = 100

The strategy actually decides how the data is stored nicely for each
'session'.

Hope this helps
Jay

"solex" <so***@nowhere. com> wrote in message
news:OX******** ******@TK2MSFTN GP09.phx.gbl...
Jay,

Thanks for your response, the App.config may work for applications settings
but will not work for user settings which may change during app

execution.
The strategy pattern is an interesting idea where the Context Object

would
be implemented as a Singleton. But is it possible to create a singelton
within the context of a single session. What I really need is for every
session to create its own Session object to be used by the other objects. I
cannot use a singleton that is global to the application on the Web
side.
Thanks,
Dan

"Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in

message
news:ux******** ******@TK2MSFTN GP09.phx.gbl...
Solex,
I would implement it externally as a singleton, however internally I would use a Strategy Pattern, possible app.config/web.config based that

indicated
to use a private shared field for the backing store or the
HttpContext.Ses sion or a ThreadStatic variable.

See the following on how to create new sections in the

app.config/web.config
via the configSections section.

http://msdn.microsoft.com/library/de...onhandlers.asp

and:

http://msdn.microsoft.com/library/de...ionsschema.asp

Also read about the System.Configur ation.Configura tionSettings class and other classes in the System.Configur ation namespace.

Hope this helps
Jay
Hope this helps
Jay

"solex" <so***@nowhere. com> wrote in message
news:uY******** *****@tk2msftng p13.phx.gbl...
> Hello,
>
> I am trying to model a session object that is essentially a collection of
> different items (connection string, user name, maps etc.) I would like this
> session object to be available to other objects within my client
> application.
>
> I can do one of two things (1) make the session object a singleton
(2) pass
> the session object to the methods that need them. Option 2 is a bit

more
> complicated and messy then option 1. My other goal in this

excercise is to
> be able to reuse these other objects within a web or application or
server
> environment, a singleton object would, I believe limit my scalelability and
> perhaps not work at all because what I really need is an session

singleton
> per user session.
>
> Any comments on how I might achive option 1 in a Web environment so

that
I
> can reuse the other objects?
>
> Thanks,
> Dan
>
>



Nov 20 '05 #8
Dan,
(1) First and foremost is when using a singleton in a web environment does
this not mean that for every request everyone will be getting the same
instance of MySessionStrate gy? Correct, there will only be one instance of any strategy. Remember the
MySessionStrate gy is a Strategy of how to store session data it is not the
mechanism to store that data!

By that I mean a Web App, would use the WebSessionStrat egy which might be
based on HttpContext.Ses sion.

While for a Windows Forms app would use the FormsSessionSta tegy which might
be based on shared variables.

Of course you can define the actual strategy however you want for each
environment.
(2) Also what is Instance supposed to return in MySessionStrate gy? Per the definition I gave it is suppose to return a MySession object.

How that object is persisted between calls to the method is what YOU need to
define!

For a windows forms application which are inherently are single user, where
is the best place to store 'session' information? I would use a shared
field.

For an ASP.NET application which are inherently multiuser, where is the best
place to store 'session' information? I would use a HttpCOntext.Ses sion
field.

I'm leaving the actual answer to this question to you, as you have a better
idea of what you are attempting.

Going back to your original post, you want to be able to store "session"
data independent of the platform. Am I misunderstandin g what you are asking?

Hope this helps
Jay
"solex" <so***@nowhere. com> wrote in message
news:Oe******** ******@TK2MSFTN GP09.phx.gbl... Jay,

Thank you for your detailed example. Sorry but I have a few more questions:
(1) First and foremost is when using a singleton in a web environment does
this not mean that for every request everyone will be getting the same
instance of MySessionStrate gy?

(2) Also what is Instance supposed to return in MySessionStrate gy?

Thanks,
Dan

<<snip>>
Nov 20 '05 #9
Dan,
I should add to my previous post.

The MySession singleton is giving your application a common API to use to
get to 'session' data, while the MySessionStrate gy is giving your
application the felicity to define how best to persist that 'session' data
between calls taking into consideration multiple users & threads...

Hope this helps
Jay

"solex" <so***@nowhere. com> wrote in message
news:Oe******** ******@TK2MSFTN GP09.phx.gbl...
Jay,

Thank you for your detailed example. Sorry but I have a few more questions:
(1) First and foremost is when using a singleton in a web environment does
this not mean that for every request everyone will be getting the same
instance of MySessionStrate gy?

(2) Also what is Instance supposed to return in MySessionStrate gy?

Thanks,
Dan

<<snip>>
Nov 20 '05 #10

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
10
2659
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
1
2454
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and g++'s output. Any help? // singleton.h template <class T> class Singleton : public T { public: static T * Instance();
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...
7
3289
by: Ethan | last post by:
Hi, I have a class defined as a "Singleton" (Design Pattern). The codes are attached below. My questions are: 1. Does it has mem leak? If no, when did the destructor called? If yes, how can I avoid it? Purify does not show it has mem leak. // test if singleto class has mem leakage #include <iostream>
3
3930
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
5313
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++ Design" or similar books, but I feel there are full of clever guys here who could help me out.
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) { }
3
18258
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 is, regardless of where the object is hidden, everyone needs access to it. The global point of access is the object's Instance() method. Individual users need to be prevented from creating their own instances of the Singleton.
3
1808
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design constraints/environment are as follows: 1) Everything is single-threaded during static initialization (as in prior to the open brace of main) 2) The environment may be multi-threaded during nominal program execution (within {} of main) 3) I...
0
9715
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
9595
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
10097
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...
1
7642
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6867
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
5535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.