Best Practice for Maintaining Class (s)

I am creating a web application that uses an external class to handle most of the work and rules for a site. Most pages will need access to this class to get the information they need. In the past, I would put such a class in a session variable, so it's easily available when required and doesn't have to be repeated all the time.

First question, is it a bad idea to fill this class with a session variable (is it not very big)?

Second question, if it doesn't matter whether it is worth keeping the site's application class class in the session, is there a way to write a centralized method to capture or store the class in the session? I don't want to use a bunch of repeating code page after the page gets the class, checks it there, creating it if not, etc.

0


source to share


3 answers


Before deciding where to store your class, you have to answer two questions:

  • How long should this class live?
  • In what area should it be visible?

Examples that answer both questions: request, user session, application.



If this class is stateless (no data and only logic), then it can probably live for a lifetime of application. If it is only data that is unique to each user (which does not need to be reloaded on every request), you can send it directly to the session and skip the next paragraphs.

Now that you have determined your lifespan, you have several solutions. The best solution for lifestyle management is the IoC container. A simpler solution is to just abstract storage and use a static facade like Current.MyClass, where an instance of MyClass is stored in a request, session, or application depending on what storage Current was provided.

However, you shouldn't implement a singleton in the specified class, as it doesn't have to decide for itself how many instances you need and this limits your ability to replace with another class with the same interface if required.

+3


source


Without knowing any frameworks you are using, it doesn't seem like a smart idea to put the class in the session store - it will be copied once per user - unless it has data that is unique to that user.



The best advice I can give would be to have a singleton class (you can use it - this is a design pattern) and then just have a function in that class that will return the class you want, or create it if it doesn't already exist ...

0


source


The jury still fails if the class is to be saved in the session in the first place. With the application I asked this question for, I decided not to fill the class with session, it really wasn't necessary, I was lazy. Developing this method for session management has been worth the time as this is something that has bothered me for some time in web development.

Which from my research wrote a static class to manage my session variables. This class handles all reads and writes per session and stores them all strictly for input for loading. This has always bothered me with repeating code all over the place for a session. Also leaves typos.

There are two articles I like that I found on this one, I can only find one of them now and include the other when I find it.

The first one was in Code Project This could be the second link

The template is simple and straightforward. I also created a class for requests to grab parameters from url query strings. I see no reason not to extend it to cookies.

This was my first use of the template, I only used strings, so the private method is a bit limited, but it can be easily modified to use any class or primitive type.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

namespace BDZipper.Site
{
    /// <summary>
    /// This class maintains all session variables for us instead of handeling them 
    /// individually in the session.  They are also strongly typed.
    /// </summary>
    public static class SessionManager
    {

        # region Private Constants
        // Define string constant for each property.  We use the constant to call the session variable
        // easier not to make mistakes this way.
        // I think for simplicity, we will use the same key string in the web.config AppSettings as
        // we do for the session variable.  This way we can use the same constant for both!
        private const string startDirectory = "StartDirectory";
        private const string currentDirectory = "CurrentDirectory";

        # endregion

        /// <summary>
        /// The starting directory for the application
        /// </summary>
        public static string StartDirectory
        {
            get
            {
                return GetSessionValue(startDirectory, true);
            }
            //set
            //{
            //    HttpContext.Current.Session[startDirectory] = value;
            //}
        }

        public static string CurrentDirectory
        {
            get
            {
                return GetSessionValue(currentDirectory, false);
            }
            set
            {
                HttpContext.Current.Session[currentDirectory] = value;
            }
        }
        //TODO: Update to use any class or type
        /// <summary>
        /// Handles routine of getting values out of session and or AppSettings
        /// </summary>
        /// <param name="SessionVar"></param>
        /// <param name="IsAppSetting"></param>
        /// <returns></returns>
        private static string GetSessionValue(string SessionVar, bool IsAppSetting)
        {
            if (null != HttpContext.Current.Session[SessionVar])
                return (string)HttpContext.Current.Session[SessionVar];
            else if (IsAppSetting)  // Session null with appSetting value
                return ConfigurationManager.AppSettings[SessionVar];
            else
                return "";
        }
    }
}

      

0


source







All Articles