System Session Variable VS. Own-declared 'Session' Variable

Discussion in 'ASP .Net' started by chowchho, Mar 28, 2008.

  1. chowchho

    chowchho Guest

    I am new to ASP.NET and most of my time spending on windows application. In
    my Windows application, I used to create a class called MySession and and
    instantiate it at the first page and pass around from form to form.

    My quesitons are:
    1) Does this concept same with the Session in ASP.NET?
    2) Can I use the MySession method in ASP.NET?
    3) Wht's the benefit of Session over own-declared session class?

    Thank all.


    Regards,
    Chow
     
    chowchho, Mar 28, 2008
    #1
    1. Advertising

  2. chowchho

    Aidy Guest

    Asp.net gives you a built-in object called Session that lets you store
    whatever you want in it;

    Session["UserID"] = 123;
    Session["Username"] = "Hello";
    User user = new User();
    Session["UserObject"] = user;

    So you could create your own MySession class and just store it in the
    Session object;

    MySession s = new MySession();
    Session["SessionObject"] = s;

    then retrieve it like this;

    MySession s = (MySession) Session["SessionObject"];

    If you look at the global.asax page you can code up a Session start event,
    this is fired when the user first visits your site and this is where you
    would instantiate the object and store it in the Session;

    void Session_Start(object sender, EventArgs e)
    {
    ... code here
    }

    "chowchho" <> wrote in message
    news:...
    >I am new to ASP.NET and most of my time spending on windows application. In
    > my Windows application, I used to create a class called MySession and and
    > instantiate it at the first page and pass around from form to form.
    >
    > My quesitons are:
    > 1) Does this concept same with the Session in ASP.NET?
    > 2) Can I use the MySession method in ASP.NET?
    > 3) Wht's the benefit of Session over own-declared session class?
    >
    > Thank all.
    >
    >
    > Regards,
    > Chow
    >
     
    Aidy, Mar 28, 2008
    #2
    1. Advertising

  3. chowchho

    chowchho Guest

    Thank for your explanation.
    But is it a good practice to declare our own class of session insteads of
    using the system one? In my opinion, own session class is more easy to manage
    and no time-out issue.

    Thank.

    "Aidy" wrote:

    > Asp.net gives you a built-in object called Session that lets you store
    > whatever you want in it;
    >
    > Session["UserID"] = 123;
    > Session["Username"] = "Hello";
    > User user = new User();
    > Session["UserObject"] = user;
    >
    > So you could create your own MySession class and just store it in the
    > Session object;
    >
    > MySession s = new MySession();
    > Session["SessionObject"] = s;
    >
    > then retrieve it like this;
    >
    > MySession s = (MySession) Session["SessionObject"];
    >
    > If you look at the global.asax page you can code up a Session start event,
    > this is fired when the user first visits your site and this is where you
    > would instantiate the object and store it in the Session;
    >
    > void Session_Start(object sender, EventArgs e)
    > {
    > ... code here
    > }
    >
    > "chowchho" <> wrote in message
    > news:...
    > >I am new to ASP.NET and most of my time spending on windows application. In
    > > my Windows application, I used to create a class called MySession and and
    > > instantiate it at the first page and pass around from form to form.
    > >
    > > My quesitons are:
    > > 1) Does this concept same with the Session in ASP.NET?
    > > 2) Can I use the MySession method in ASP.NET?
    > > 3) Wht's the benefit of Session over own-declared session class?
    > >
    > > Thank all.
    > >
    > >
    > > Regards,
    > > Chow
    > >

    >
    >
    >
     
    chowchho, Mar 28, 2008
    #3
  4. re:
    !> But is it a good practice to declare our own class of session insteads of
    !> using the system one? In my opinion, own session class is more easy to manage
    !> and no time-out issue.

    There's no problem at all with using your own version of Session,
    instead of the built-in one, as long as you know what you're doing.

    Just place your class file ( *.cs or *.vb ) in the App_Code directory of your app,
    and it will be automatically compiled, or manually compile an assembly
    from your class file and place it in the /bin directory of your app.




    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en español : http://asp.net.do/foros/
    ======================================
    "chowchho" <> wrote in message
    news:...
    > Thank for your explanation.
    > But is it a good practice to declare our own class of session insteads of
    > using the system one? In my opinion, own session class is more easy to manage
    > and no time-out issue.
    >
    > Thank.
    >
    > "Aidy" wrote:
    >
    >> Asp.net gives you a built-in object called Session that lets you store
    >> whatever you want in it;
    >>
    >> Session["UserID"] = 123;
    >> Session["Username"] = "Hello";
    >> User user = new User();
    >> Session["UserObject"] = user;
    >>
    >> So you could create your own MySession class and just store it in the
    >> Session object;
    >>
    >> MySession s = new MySession();
    >> Session["SessionObject"] = s;
    >>
    >> then retrieve it like this;
    >>
    >> MySession s = (MySession) Session["SessionObject"];
    >>
    >> If you look at the global.asax page you can code up a Session start event,
    >> this is fired when the user first visits your site and this is where you
    >> would instantiate the object and store it in the Session;
    >>
    >> void Session_Start(object sender, EventArgs e)
    >> {
    >> ... code here
    >> }
    >>
    >> "chowchho" <> wrote in message
    >> news:...
    >> >I am new to ASP.NET and most of my time spending on windows application. In
    >> > my Windows application, I used to create a class called MySession and and
    >> > instantiate it at the first page and pass around from form to form.
    >> >
    >> > My quesitons are:
    >> > 1) Does this concept same with the Session in ASP.NET?
    >> > 2) Can I use the MySession method in ASP.NET?
    >> > 3) Wht's the benefit of Session over own-declared session class?
    >> >
    >> > Thank all.
    >> >
    >> >
    >> > Regards,
    >> > Chow
    >> >

    >>
    >>
    >>
     
    Juan T. Llibre, Mar 28, 2008
    #4
  5. chowchho

    Aidy Guest

    > But is it a good practice to declare our own class of session insteads of
    > using the system one? In my opinion, own session class is more easy to
    > manage
    > and no time-out issue.


    No, cos IIS manages if you have timed out, or are a new visitor etc, and you
    use the built-in Session object to properly interact with that. You can't
    impose your own time-out constraints, the web server will dictate when you
    have timed out and when you do your session is destroyed and the next time
    you access a page you're given a new session and are now a new user.

    Coming from a desktop app background you maybe need to spend some time
    familiarising how the lifespan of a web app differs.
     
    Aidy, Mar 28, 2008
    #5
  6. chowchho

    Hans Kesting Guest

    on 28-3-2008, chowchho supposed :
    > I am new to ASP.NET and most of my time spending on windows application. In
    > my Windows application, I used to create a class called MySession and and
    > instantiate it at the first page and pass around from form to form.
    >
    > My quesitons are:
    > 1) Does this concept same with the Session in ASP.NET?
    > 2) Can I use the MySession method in ASP.NET?
    > 3) Wht's the benefit of Session over own-declared session class?
    >
    > Thank all.
    >
    >
    > Regards,
    > Chow


    It might not be so easy to "pass it from page to page", as the
    page-classes themselves live only long enough to serve a single
    request. Every request is handled by a fresh instance of the page.
    This means that you can't just access a property of an instance of some
    other page, as that instance does not exist.

    An other "solution" that might occur to you is to use a singleton.
    DO NOT DO THAT. The web application is a single application that serves
    a number of users. If you use a singleton to store information, then
    EVERY user sees the same information. This might or might not be what
    you want.

    That is the reason that the Session is available in web applications:
    here you can store user-specific information (or rather *session*
    specific information). This session usually depends on a temporary
    cookie.

    Hans Kesting
     
    Hans Kesting, Mar 28, 2008
    #6
  7. chowchho

    sloan Guest

    I really like this approach:

    http://aspalliance.com/810_Implementing_a_Singleton_Pattern_in_C

    I'm looking to extending it (one day) and be able to swap out which "store"
    is being used based on key.

    So I could decide to configuration wise....change whether or not I use
    Session or ViewState .(as one permutation example) based on a simple
    string/key.

    ...

    But what he has is good as is.




    "chowchho" <> wrote in message
    news:...
    >I am new to ASP.NET and most of my time spending on windows application. In
    > my Windows application, I used to create a class called MySession and and
    > instantiate it at the first page and pass around from form to form.
    >
    > My quesitons are:
    > 1) Does this concept same with the Session in ASP.NET?
    > 2) Can I use the MySession method in ASP.NET?
    > 3) Wht's the benefit of Session over own-declared session class?
    >
    > Thank all.
    >
    >
    > Regards,
    > Chow
     
    sloan, Mar 28, 2008
    #7
  8. chowchho

    Patrice Guest

    You could start by using the built in session mechanism this way :
    http://www.codeproject.com/KB/aspnet/typedsessionstate.aspx

    From this starting point point you'll have the flexibility to change the
    underlying store and have the benefit of strongly typed access with
    intellisense...

    --
    Patrice



    "chowchho" <> a écrit dans le message de
    news: ...
    >I am new to ASP.NET and most of my time spending on windows application. In
    > my Windows application, I used to create a class called MySession and and
    > instantiate it at the first page and pass around from form to form.
    >
    > My quesitons are:
    > 1) Does this concept same with the Session in ASP.NET?
    > 2) Can I use the MySession method in ASP.NET?
    > 3) Wht's the benefit of Session over own-declared session class?
    >
    > Thank all.
    >
    >
    > Regards,
    > Chow
     
    Patrice, Mar 28, 2008
    #8
    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,620
    Jon Paal
    Feb 5, 2006
  2. Stefan Siegl
    Replies:
    1
    Views:
    806
  3. Saverio M.
    Replies:
    0
    Views:
    557
    Saverio M.
    Jul 3, 2006
  4. lovecreatesbeauty
    Replies:
    6
    Views:
    377
    red floyd
    Sep 2, 2005
  5. David Filmer
    Replies:
    17
    Views:
    299
    J. Romano
    Aug 18, 2004
Loading...

Share This Page