how did a singleton object achieve application scope in asp.net when it was declared a page variable

Discussion in 'ASP .Net' started by Rich, Feb 21, 2006.

  1. Rich

    Rich Guest

    The following code produced a singleton object with application scope
    when it should have had page scope:

    public class Singleton
    {
    private static Singleton uniqueInstance = null;
    private Singleton()
    {
    }
    public static Singleton getInstance()
    {
    if (uniqueInstance == null)
    uniqueInstance = new Singleton();
    return uniqueInstance;
    }
    }
    public partial class Default1:page
    {
    Singleton s = Singleton.getInstance();
    void Page_Load(...)
    {...}
    void Button1Clicked(...)
    {...}
    }
    public partial class Default2:page
    {
    Singleton s = Singleton.getInstance(); // this retrieved the
    instance from Default1
    void Page_Load(...)
    {...}
    void Button1Clicked(...)
    {...}
    }



    Can anyone please explain how a Singleton object created within a Page
    class is able to get application scope?


    Rich
     
    Rich, Feb 21, 2006
    #1
    1. Advertising

  2. Rich

    Rune B Guest

    sure, it's stored for all users in the static field " private static
    Singleton uniqueInstance; "
    -- Instead you can save it in the HttpContext.Items collection, so it is a
    "singleton per HttpRequest" instead:

    public class Singleton
    {
    private Singleton()
    {
    }

    static Singleton(){
    _ticket = new object();
    }

    private static object _ticket;

    public static Singleton getInstance(HttpContext context)
    {
    Singleton uniqueInstance = context.Items[_ticket] as Singleton;
    if(uniqueInstance == null)
    {
    uniqueInstance = new Singleton();
    context.Items.Add(_ticket, uniqueInstance);
    }
    return uniqueInstance;
    }

    }

    R-)



    "Rich" <> wrote in message
    news:...
    > The following code produced a singleton object with application scope
    > when it should have had page scope:
    >
    > public class Singleton
    > {
    > private static Singleton uniqueInstance = null;
    > private Singleton()
    > {
    > }
    > public static Singleton getInstance()
    > {
    > if (uniqueInstance == null)
    > uniqueInstance = new Singleton();
    > return uniqueInstance;
    > }
    > }
    > public partial class Default1:page
    > {
    > Singleton s = Singleton.getInstance();
    > void Page_Load(...)
    > {...}
    > void Button1Clicked(...)
    > {...}
    > }
    > public partial class Default2:page
    > {
    > Singleton s = Singleton.getInstance(); // this retrieved the
    > instance from Default1
    > void Page_Load(...)
    > {...}
    > void Button1Clicked(...)
    > {...}
    > }
    >
    >
    >
    > Can anyone please explain how a Singleton object created within a Page
    > class is able to get application scope?
    >
    >
    > Rich
    >
     
    Rune B, Feb 21, 2006
    #2
    1. Advertising

  3. Rich

    sloan Guest

    Check my blog

    http://spaces.msn.com/sholliday/

    10/24/2005

    Everywhere I have
    System.Web.HttpContext.Current.Session

    You'd replace with
    System.Web.HttpContext.Current.Application (going from memory)





    "Rich" <> wrote in message
    news:...
    > The following code produced a singleton object with application scope
    > when it should have had page scope:
    >
    > public class Singleton
    > {
    > private static Singleton uniqueInstance = null;
    > private Singleton()
    > {
    > }
    > public static Singleton getInstance()
    > {
    > if (uniqueInstance == null)
    > uniqueInstance = new Singleton();
    > return uniqueInstance;
    > }
    > }
    > public partial class Default1:page
    > {
    > Singleton s = Singleton.getInstance();
    > void Page_Load(...)
    > {...}
    > void Button1Clicked(...)
    > {...}
    > }
    > public partial class Default2:page
    > {
    > Singleton s = Singleton.getInstance(); // this retrieved the
    > instance from Default1
    > void Page_Load(...)
    > {...}
    > void Button1Clicked(...)
    > {...}
    > }
    >
    >
    >
    > Can anyone please explain how a Singleton object created within a Page
    > class is able to get application scope?
    >
    >
    > Rich
    >
     
    sloan, Feb 21, 2006
    #3
  4. There's a great write-up in singleton pattern in C# and making it thread
    safe - your implementation isn't - at:
    http://www.yoda.arachsys.com/csharp/singleton.html

    But whatever mechanics you use to implement a singleton, static fields are
    scoped to the entire appDomain/application. I'm not sure why you think it
    should be page scoped, when you declare a static field there's only one
    instance for the entire appdomain..that's just how it works :)

    What exact scope are you looking for? For a given page and all users (create
    a static field in the page)? For a given user (store in the session)? For a
    given request(store in the HttpContext)...

    Karl
    --
    http://www.openmymind.net/



    "Rich" <> wrote in message
    news:...
    > The following code produced a singleton object with application scope
    > when it should have had page scope:
    >
    > public class Singleton
    > {
    > private static Singleton uniqueInstance = null;
    > private Singleton()
    > {
    > }
    > public static Singleton getInstance()
    > {
    > if (uniqueInstance == null)
    > uniqueInstance = new Singleton();
    > return uniqueInstance;
    > }
    > }
    > public partial class Default1:page
    > {
    > Singleton s = Singleton.getInstance();
    > void Page_Load(...)
    > {...}
    > void Button1Clicked(...)
    > {...}
    > }
    > public partial class Default2:page
    > {
    > Singleton s = Singleton.getInstance(); // this retrieved the
    > instance from Default1
    > void Page_Load(...)
    > {...}
    > void Button1Clicked(...)
    > {...}
    > }
    >
    >
    >
    > Can anyone please explain how a Singleton object created within a Page
    > class is able to get application scope?
    >
    >
    > Rich
    >
     
    Karl Seguin [MVP], Feb 21, 2006
    #4
  5. Rich

    Rich Guest

    Thanks all for the response. In regard to Karl's, wouldn't a given
    request automatically have a session associated with that request (ie 1
    user = 1 request)?

    Rich
     
    Rich, Feb 22, 2006
    #5
  6. A request is shorter than lived than a session. Yes a request is associated
    with a session,but you can scope a singleton to live only for the individual
    request - subsequent request (made by the same user in the same session)
    would have a new instance.

    Karl

    --
    http://www.openmymind.net/



    "Rich" <> wrote in message
    news:...
    > Thanks all for the response. In regard to Karl's, wouldn't a given
    > request automatically have a session associated with that request (ie 1
    > user = 1 request)?
    >
    > Rich
    >
     
    Karl Seguin [MVP], Feb 22, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JohnZing

    declared or not declared ?

    JohnZing, Feb 5, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    1,606
    Jon Paal
    Feb 5, 2006
  2. imghani
    Replies:
    1
    Views:
    788
    Laurent Bugnion
    Aug 18, 2006
  3. Replies:
    3
    Views:
    1,486
  4. vaysagekv

    Scope of a variable declared in for loop

    vaysagekv, Feb 23, 2010, in forum: C Programming
    Replies:
    56
    Views:
    8,651
    Keith Thompson
    Feb 25, 2010
  5. Paul McMahon
    Replies:
    3
    Views:
    210
    David A. Black
    Jun 9, 2008
Loading...

Share This Page