A way to refer to items in Session object without using Session directly

Discussion in 'ASP .Net' started by Andy B, May 29, 2008.

  1. Andy B

    Andy B Guest

    Is there a way to refer to things in Session object without calling Session
    itself?
     
    Andy B, May 29, 2008
    #1
    1. Advertising

  2. Yea,

    Just keep it in both places.... Session and other container/place you will
    use to get those things...
    ---------------------------------------------------------------------------
    On a serious note..... What are you trying to do.
    Will HttpContext.Current.Session help?
    you can access Session object from your business logic without passing
    Session around....If that is what you trying to avoid.

    George.

    "Andy B" <> wrote in message
    news:%...
    > Is there a way to refer to things in Session object without calling
    > Session itself?
    >
    >
    >
     
    George Ter-Saakov, May 29, 2008
    #2
    1. Advertising

  3. Andy B

    Teemu Keiski Guest

    Hi,

    if you intend to keep your component loosely coupled, so that it does not
    require ASP.NET's session to exist (if you intend to use it in other types
    of apps or in a web service etc), you can design so that you have a type in
    the component library which represents the data the usual component in that
    library needs. E.g create a type to abstract the concept of a session for
    your components.

    You can instantiate that object (session type) in ASP.NET, store it in
    ASP.NEt's Session if needed, but when using components from the library, you
    pass it(them) the session object so the component wouldn't need to do grabs
    to the ASP.NET Session.

    Some background why just HttpContext.Current.Session is a bad thing:
    http://aspadvice.com/blogs/joteke/archive/2006/04/23/16785.aspx

    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net

    "Andy B" <> wrote in message
    news:%...
    > Is there a way to refer to things in Session object without calling
    > Session itself?
    >
    >
    >
     
    Teemu Keiski, May 29, 2008
    #3
  4. Andy B

    Andy B Guest

    I am trying to avoid having to do something like this every time I want an
    object from Session:
    ((DataSet)Session["ContractDataSet"]).Tables["..."]...;
    or:
    DataSet ContractDataSet = (DataSet)Session["ContractDataSet"];
    //...whatever with CongtractDataSet

    Session["ContractDataSet"]=ContractDataSet;

    I want a way to create and refer to just ContractDataSEt but have it linked
    to Session["ContractDataSet"] as well as ContractDataSet. I just need the
    code in VB 9.




    "George Ter-Saakov" <> wrote in message
    news:...
    > Yea,
    >
    > Just keep it in both places.... Session and other container/place you will
    > use to get those things...
    > ---------------------------------------------------------------------------
    > On a serious note..... What are you trying to do.
    > Will HttpContext.Current.Session help?
    > you can access Session object from your business logic without passing
    > Session around....If that is what you trying to avoid.
    >
    > George.
    >
    > "Andy B" <> wrote in message
    > news:%...
    >> Is there a way to refer to things in Session object without calling
    >> Session itself?
    >>
    >>
    >>

    >
    >
     
    Andy B, May 29, 2008
    #4
  5. Andy B

    dbgrick Guest

    Re: A way to refer to items in Session object without using Sessio

    You could make a wrapper class and create a property. The get and set for
    the property will then call the session object:

    public class MyDataClass
    {

    public static DataSet ContractDataSet
    {
    get
    {
    return (DataSet)Session["ContractDataSet"];
    }
    set
    {
    Session["ContractDataSet"] = value;
    }
    }

    }
    You might want to do some null checks as well.

    Regards,
    Rick Davis
    DBG Software


    "Andy B" wrote:

    > I am trying to avoid having to do something like this every time I want an
    > object from Session:
    > ((DataSet)Session["ContractDataSet"]).Tables["..."]...;
    > or:
    > DataSet ContractDataSet = (DataSet)Session["ContractDataSet"];
    > //...whatever with CongtractDataSet
    >
    > Session["ContractDataSet"]=ContractDataSet;
    >
    > I want a way to create and refer to just ContractDataSEt but have it linked
    > to Session["ContractDataSet"] as well as ContractDataSet. I just need the
    > code in VB 9.
    >
    >
    >
    >
    > "George Ter-Saakov" <> wrote in message
    > news:...
    > > Yea,
    > >
    > > Just keep it in both places.... Session and other container/place you will
    > > use to get those things...
    > > ---------------------------------------------------------------------------
    > > On a serious note..... What are you trying to do.
    > > Will HttpContext.Current.Session help?
    > > you can access Session object from your business logic without passing
    > > Session around....If that is what you trying to avoid.
    > >
    > > George.
    > >
    > > "Andy B" <> wrote in message
    > > news:%...
    > >> Is there a way to refer to things in Session object without calling
    > >> Session itself?
    > >>
    > >>
    > >>

    > >
    > >

    >
    >
    >
     
    dbgrick, May 29, 2008
    #5
  6. Andy B

    Andy B Guest

    Re: A way to refer to items in Session object without using Sessio

    So this would be a static class that is maintained outside the bounds of the
    page that uses it then? And I would probably put other methods/events in
    this class that use the same session object?


    "dbgrick" <> wrote in message
    news:...
    > You could make a wrapper class and create a property. The get and set for
    > the property will then call the session object:
    >
    > public class MyDataClass
    > {
    >
    > public static DataSet ContractDataSet
    > {
    > get
    > {
    > return (DataSet)Session["ContractDataSet"];
    > }
    > set
    > {
    > Session["ContractDataSet"] = value;
    > }
    > }
    >
    > }
    > You might want to do some null checks as well.
    >
    > Regards,
    > Rick Davis
    > DBG Software
    >
    >
    > "Andy B" wrote:
    >
    >> I am trying to avoid having to do something like this every time I want
    >> an
    >> object from Session:
    >> ((DataSet)Session["ContractDataSet"]).Tables["..."]...;
    >> or:
    >> DataSet ContractDataSet = (DataSet)Session["ContractDataSet"];
    >> //...whatever with CongtractDataSet
    >>
    >> Session["ContractDataSet"]=ContractDataSet;
    >>
    >> I want a way to create and refer to just ContractDataSEt but have it
    >> linked
    >> to Session["ContractDataSet"] as well as ContractDataSet. I just need the
    >> code in VB 9.
    >>
    >>
    >>
    >>
    >> "George Ter-Saakov" <> wrote in message
    >> news:...
    >> > Yea,
    >> >
    >> > Just keep it in both places.... Session and other container/place you
    >> > will
    >> > use to get those things...
    >> > ---------------------------------------------------------------------------
    >> > On a serious note..... What are you trying to do.
    >> > Will HttpContext.Current.Session help?
    >> > you can access Session object from your business logic without passing
    >> > Session around....If that is what you trying to avoid.
    >> >
    >> > George.
    >> >
    >> > "Andy B" <> wrote in message
    >> > news:%...
    >> >> Is there a way to refer to things in Session object without calling
    >> >> Session itself?
    >> >>
    >> >>
    >> >>
    >> >
    >> >

    >>
    >>
    >>
     
    Andy B, May 29, 2008
    #6
  7. Andy B

    Andy B Guest

    Re: A way to refer to items in Session object without using Sessio

    If all it was used for was to wrap around the Session, then it would be kind
    of useless. Since the design and code of the wizard I am working on deals
    with a lot of transactions between datasets and the frontend, I will need to
    create other methods and properties to get the work done. Insdead of making
    tons of senseless clutter inside the page, I can just make it inside a class
    and then reference the class in the page code behind. Unless of course, that
    isn't a good idea...


    "Mark Rae [MVP]" <> wrote in message
    news:...
    > "dbgrick" <> wrote in message
    > news:...
    >
    >> You could make a wrapper class and create a property.

    >
    > That will not prevent the Session object from actually being called,
    > albeit behind the scenes - all it will do is create a completely
    > unnecessary additional layer of code for no benefit whatsoever...
    >
    > If you want to use the Session cache, then you need to refer to it - no
    > getting round that...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
     
    Andy B, May 30, 2008
    #7
  8. Andy B

    Damien Guest

    Re: A way to refer to items in Session object without using Sessio

    On May 29, 11:12 pm, "Mark Rae [MVP]" <> wrote:
    > "dbgrick" <> wrote in message
    >
    > news:...
    >
    > > You could make a wrapper class and create a property.

    >
    > That will not prevent the Session object from actually being called, albeit
    > behind the scenes - all it will do is create a completely unnecessary
    > additional layer of code for no benefit whatsoever...
    >
    > If you want to use the Session cache, then you need to refer to it - no
    > getting round that...
    >

    It ensures a) that there's no typos anywhere where the same object is
    being accessed, and b) that there's agreement at all points of access
    on what the data type is. (a) could be achieved by introducing a
    constant, but (b) could only be achieved in that case by searching the
    codebase. It's a lot nicer if you're changing a datatype to do it
    inside this wrapper class and then have the compiler tell you of any
    problems with that.

    Damien
     
    Damien, May 30, 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. AP  No spam
    Replies:
    5
    Views:
    30,084
    ronynaidu84
    Oct 6, 2011
  2. Jason Heyes
    Replies:
    7
    Views:
    1,841
    msalters
    Dec 14, 2004
  3. Replies:
    5
    Views:
    452
    Chris Hills
    Jun 21, 2007
  4. Anjan Bhowmik
    Replies:
    1
    Views:
    488
    Misbah Arefin
    Feb 14, 2008
  5. Henrik
    Replies:
    1
    Views:
    442
    Ken Cox [Microsoft MVP]
    Jul 6, 2006
Loading...

Share This Page