存储临时信息的最佳实践

时间:2014-02-07 15:51:49

标签: c# asp.net-mvc-5

当我的用户在学生角色登录系统时,他可以选择他注册的各种课程。我已经有了一个过滤器,它会将他重定向到选择类页面,所以他必须选择一个类来访问系统,并在他想要的任何时候更改它,整个系统的上下文都会改变。

就目前而言,我使用下面的代码将IdClass存储在会话变量中,系统使用它来过滤所有相关的查询和函数,比如显示当前类的所有课程。我的问题是:这是一个好习惯吗?这是正确的还是有更好更有效的方法?我正试图遵循模式。

[Serializable]
public sealed class Session
{
    private const string SESSION_FOO = "STUDYPLATFORM_GUID";

    private Session()
    {
        this.IdClass= 0; // Construct it to 0 so it evaluate as there's no Class selected.
    }

    /* This is the session's public IdClass that
    i can get and set throughout the application. */
    public int IdClass { get; set; }

    public static Session Current
    {
        get
        {
            if (HttpContext.Current.Session[SESSION_FOO] == null)
            {
                HttpContext.Current.Session[SESSION_FOO] = new Session();
            }

            return HttpContext.Current.Session[SESSION_FOO] as Session;
        }
    }
}

0 个答案:

没有答案