User & application-state variables...

Discussion in 'ASP .Net' started by Jeremy, Apr 15, 2004.

  1. Jeremy

    Jeremy Guest

    I am relativly new to ASP.NET, and my question is this:

    Is it better to create a wrapper class that contains all the running
    settings for the application and then load that into the app state, or is it
    better to load the scalar variables and store them individually?

    In case I'm not making my self clear, is it better to do this:

    '// Load each variable individually
    Context.Cache.Add( "UserFirstName", txtFirstName.Text ... )
    Context.Cache.Add( "UserLastName", txtLastName.Text ... )

    or this:

    '// Store vars in a class, and then cache the class
    Config.ActiveUser.FirstName = txtFirstName.Text
    Config.ActiveUser.LastName = txtLastName.Text

    Context.Cache.Add( "Config", Config ... )

    ?

    Thanks,
    Jeremy
     
    Jeremy, Apr 15, 2004
    #1
    1. Advertising

  2. Both ways are valid. It's all about tradeoffs.
    The first way is simpler, the second way allows for more custom
    functionality.
    Simplicity vs. Power is the kind of decision we developers face nearly every
    day.
    You must decide which way is best for your app.

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://Steve.Orr.net


    "Jeremy" <> wrote in message
    news:tFDfc.35635$...
    > I am relativly new to ASP.NET, and my question is this:
    >
    > Is it better to create a wrapper class that contains all the running
    > settings for the application and then load that into the app state, or is

    it
    > better to load the scalar variables and store them individually?
    >
    > In case I'm not making my self clear, is it better to do this:
    >
    > '// Load each variable individually
    > Context.Cache.Add( "UserFirstName", txtFirstName.Text ... )
    > Context.Cache.Add( "UserLastName", txtLastName.Text ... )
    >
    > or this:
    >
    > '// Store vars in a class, and then cache the class
    > Config.ActiveUser.FirstName = txtFirstName.Text
    > Config.ActiveUser.LastName = txtLastName.Text
    >
    > Context.Cache.Add( "Config", Config ... )
    >
    > ?
    >
    > Thanks,
    > Jeremy
    >
    >
     
    Steve C. Orr [MVP, MCSD], Apr 15, 2004
    #2
    1. Advertising

  3. Jeremy

    Matt Berther Guest

    Hello Jeremy,

    > Is it better to create a wrapper class that contains all the running
    > settings for the application and then load that into the app state, or
    > is it better to load the scalar variables and store them individually?


    My answer is going to be geared toward session, however, I think that you'll be able to apply this anywhere.

    Typically, the first thing that I do when I start up a web project that requires session state is implement a wrapper class for it that looks like this:

    class SessionManager
    {
    private const string SESSION_MANAGER = "SESSION_MANAGER";
    private string username;

    private SessionManager()
    {
    }

    public string UserName
    {
    get { return username; }
    set { username = value; }
    }

    public static SessionManager Instance
    {
    SessionManager manager = HttpContext.Current.Session[SESSION_MANAGER] as SessionManager;
    if (manager == null)
    {
    manager = new SessionManager();
    HttpContext.Current.Session[SESSION_MANAGER] = manager;
    }
    return manager;
    }
    }

    Using this class is very simple. All you need to do is add property accessors to it for items that you wish to store in session, as I have done in the above example with the UserName property.

    When I want to access the session state in the code, I type
    SessionManager.Instance.UserName = "someValue"; or
    string someValue = SessionManager.Instance.UserName;

    What I have now is a singleton pattern that exposes any session state that I need in a strongly typed fashion (ie: no longer do I run the risk of accidentally typing in the wrong string when I do Session["someValue"]). I can also control whether or not a property is read only at compile time.

    I hope this answers your question.

    --
    Matt Berther
    <a href="http://www.mattberther.com">http://www.mattberther.com</a>
     
    Matt Berther, Apr 15, 2004
    #3
  4. Jeremy

    Scott Allen Guest

    As another follow up -

    Be careful not to put user specific information (like first and last
    name) into the cache without some unique per user identifier (or
    consider using the Session object). Otherwise two different users may
    see the same name pulled from the cache when they come to your site.

    --
    Scott
    http://www.OdeToCode.com

    On Thu, 15 Apr 2004 15:27:29 -0700, "Steve C. Orr [MVP, MCSD]"
    <> wrote:

    >Both ways are valid. It's all about tradeoffs.
    >The first way is simpler, the second way allows for more custom
    >functionality.
    >Simplicity vs. Power is the kind of decision we developers face nearly every
    >day.
    >You must decide which way is best for your app.
     
    Scott Allen, Apr 16, 2004
    #4
    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. =?Utf-8?B?QkI=?=

    Application State vs. "global" variables

    =?Utf-8?B?QkI=?=, Feb 19, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    462
    Kevin Spencer
    Feb 19, 2004
  2. =?Utf-8?B?RXJpYyBNY1ZpY2tlcg==?=

    Why can't Application state be managed like Session state?

    =?Utf-8?B?RXJpYyBNY1ZpY2tlcg==?=, Nov 7, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    1,555
    Steven Cheng[MSFT]
    Nov 10, 2005
  3. Replies:
    6
    Views:
    486
    John Timney \( MVP \)
    Nov 30, 2005
  4. APA
    Replies:
    1
    Views:
    642
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jan 21, 2007
  5. Replies:
    9
    Views:
    1,008
Loading...

Share This Page