Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

Implementing a singleton pattern on a given session

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RSH - 09 Oct 2007 22:44 GMT
Is it possible to implement a singleton on a given session under asp .net?
While purists would say than thats technically not a singleton, i dont know
what else to call it.

Thanks!
RSH
Michael Nemtsev, MVP - 09 Oct 2007 23:08 GMT
Hello RSH,

Why not to use the session  then? which works like singleton in your case?

---
WBR,
Michael  Nemtsev [.NET/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

R> Is it possible to implement a singleton on a given session under asp
R> .net? While purists would say than thats technically not a singleton,
R> i dont know what else to call it.
R>
R> Thanks!
R> RSH
Michael Nemtsev, MVP - 09 Oct 2007 23:09 GMT
Hello RSH,

why not to use just the sesion in this case?

---
WBR,
Michael  Nemtsev [.NET/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

R> Is it possible to implement a singleton on a given session under asp
R> .net? While purists would say than thats technically not a singleton,
R> i dont know what else to call it.
R>
R> Thanks!
R> RSH
sloan - 09 Oct 2007 23:34 GMT
http://sholliday.spaces.live.com/blog/cns!A68482B9628A842A!151.entry

I have a web singleton implemented there.

The link above is 2.0, go back to the main blog page, and there is a 1.1
version as well.

> Is it possible to implement a singleton on a given session under asp .net?
> While purists would say than thats technically not a singleton, i dont
> know what else to call it.
>
> Thanks!
> RSH
Jesse Houwing - 10 Oct 2007 18:01 GMT
Hello RSH,

> Is it possible to implement a singleton on a given session under asp
> .net? While purists would say than thats technically not a singleton,
> i dont know what else to call it.

It's quite easy. Just make sure the static Instance property or method will
save and fetch the object from the session instead of from a static member.

public class SessionSingleton
{
     public static SessionSingleton Instance
     {
           private SessionSingleton(){}

           get
           {
                 // You need to add locking if you want to use this variable
multithreaded. The easy thing is that if you're not using threads yourself,
ASP.NET will make sure the session scope is exclusive to the current page.
                 SessionSingleton result = Session["MYCONSTANT"];
                 if (result == null)
                 {
                       result = new SessionSingleton();
                       Session["MYCONSTANT"] = result;
                 }
                 return result;
           }

           public string SomeMethod()
           {
                 return "From the SessionSingleton";
           }
     }
}

We use this method quite often to 'shield' the actual Session object from
the programmer and force him to use a typed method to access his values.

--
Jesse Houwing
jesse.houwing at sogeti.nl

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.