代码之家  ›  专栏  ›  技术社区  ›  Brettski

维护现场等级的最佳实践

  •  0
  • Brettski  · 技术社区  · 17 年前

    我正在构建一个web应用程序,它使用外部构建的类来处理站点的大部分工作和规则。大多数页面都需要访问该类才能获得它需要显示的信息。在过去,我会将这样一个类放在会话变量中,这样在需要时就可以很容易地访问它,而不需要不断地重新实例化。

    第一个问题,将这个类塞入会话变量(它不是很大)是个坏主意吗?

    第二个问题,如果在会话中存储sites应用程序层类不是一个坏主意,那么有没有一种方法可以编写一个集中的方法来获取类或将类存储到会话中?我不想一页接一页地重复使用代码来获取类、检查类、创建类(如果不是),等等。

    3 回复  |  直到 16 年前
        1
  •  3
  •   Andrey Shchekin    17 年前

    1. 这个班应该活多久?
    2. 它应该在什么范围内可见?

    回答这两个问题的示例:请求、用户会话、应用程序。

    现在,在你决定寿命后,你有几种解决方案。生活方式管理的最佳解决方案是IoC容器。更简单的解决方案是抽象存储并使用静态外观,如Current.MyClass,其中MyClass实例存储在请求、会话或应用程序中,具体取决于向Current提供的存储。

    但是,您不应该在指定的类中实现singleton,因为它本身不应该决定您需要多少实例,并且它限制了您在需要时用另一个具有相同接口的类替换is的能力。

        2
  •  0
  •   tomjen    17 年前

    我能给出的最好建议是创建一个单例类(你可以用谷歌搜索它——这是一个设计模式),然后在该类中创建一个函数,返回你需要的类,或者如果它还不存在的话创建它。

        3
  •  0
  •   Brettski    17 年前

    这个类是否应该首先存储在会话中,目前还没有定论。对于我提出问题的应用程序,我选择了不将类塞进会话,这真的没有必要,我很懒。制定这种方法来管理会话一直都是值得的,因为这在web开发中困扰了我一段时间。

    我的研究成果是编写一个静态类来管理会话变量。这个类处理会话的所有读写操作,并将它们都保持为强类型,以便我引导。它总是困扰着我使用重复代码到处会话垃圾。也能减少打字错误。

    有两篇我喜欢的文章,我在上面找到了,我现在只能找到其中一篇,当我找到它时,我会包括另一篇。

    第一次是在 Code Project link

    模式简单明了。我还为从url查询字符串获取参数的请求构建了一个类。我认为没有理由不把它扩展到饼干。

    这是我第一次使用该模式,我只使用了字符串,所以私有方法有点有限,但是可以很容易地更改为使用任何类或基元类型。

    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 "";
            }
        }
    }