zoukankan      html  css  js  c++  java
  • MVC开发中,共同静态方法的Session处理

    各个模块将Session的存储和获取方法集中在下记得静态类中,结果导致发布后的个客户段数据混乱。

    public class SessionManager
    {
    private static SearchSession _searchSession;
    private static User _userSession;
    static log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private static HttpSessionStateBase BaseSession => _session;

    private static HttpSessionStateBase _session;

    public static void SetSessionManager(SType type, Object value)
    {
    switch (type)
    {
    case SType.Search:
    if (value != null)
    _searchSession = (SearchSession)value;
    break;
    case SType.User:
    if (value != null)
    _userSession = (User)value;
    break;
    }
    }

    public static void SetSessionBase(HttpSessionStateBase sessionBase)
    {
    _session = sessionBase;
    BaseSession["OrgName"] = GetUserSession().OrgName;
    BaseSession["UserLevel"] = GetUserSession().UserLevel;
    }

    public static Object GetSession(string key)
    {
    return BaseSession[key];
    }

    public static void SetSession(string key, Object value)
    {
    BaseSession[key] = value;
    }

    public static void SetPageSession(SearchView sv)
    {
    _searchSession.itemsPerPage = sv.itemsPerPage;
    _searchSession.pageNumber = sv.pageNumber;
    }

    public static void ClearPageSession()
    {
    _searchSession.itemsPerPage = null;
    _searchSession.pageNumber = null;
    }

    public static void SetSession(SearchView sv)
    {
    DefaultValues.CopyValues<SearchView, SearchSession>(sv, _searchSession);
    }

    public static void SetSearchConditionSession(SearchView sv)
    {
    //The name list of the member of the class of SearchView.
    List<string> listNames = new List<string>(){"licenseName",
    "licenseNo" ,
    "firstName",
    "familyName",
    "extictJpType",
    "extictJpYear",
    "registerPlaceGen",
    "registerPlaceName",
    "certificateJpType",
    "certificateGetYear",
    "PRE_ID_POST",
    "ORG_ID_POST",
    "itemsPerPage",
    "CertifInfosIQuery",
    "sosu",
    "sortList1Selected",
    "sortList2Selected",
    "sortList3Selected"};
    List<string> errorNames = new List<string>();

    listNames.ForEach(name => { if (DefaultValues.CheckMemberName<SearchView>(name) == false) errorNames.Add(name); });

    if (errorNames.Count > 0) errorNames.ForEach(m => logger.Info("SearchView クラス中に下記の違うメンバーを使っています:" + m + " "));

    //DefaultValues.CopyValuesByType<SearchView, SearchSession>(sv, _searchSession, listNames);
    }

    public static void GetSession(ref SearchView sv)
    {
    //他の画面に遷移するとき、検索画面の入力値をセッションに保存して
    DefaultValues.CopyValues<SearchSession, SearchView>(_searchSession, sv);
    }

    public static void GetSelectListInfoBySession(SearchView searchView)
    {
    SearchBase listInfo = new SearchBase();

    //Copy ListInfo data into listInfo from searchSession
    DefaultValues.CopyValues<SearchSession, SearchBase>(_searchSession, listInfo, false);

    //Copy data into searchView
    DefaultValues.CopyValues<SearchBase, SearchView>(listInfo, searchView);

    }

    public static SearchSession GetSearchSession => _searchSession;

    public static User GetUserSession()
    {
    if (_userSession != null)
    return _userSession;
    else
    return (User)(BaseSession["UserInfoSession"]);
    }

    public static void ClearUserSession()
    {
    BaseSession?.Clear();
    //BaseSession["UserInfoSession"] = null;
    _session = null;
    }

    public static int GetItemsPerPage()
    {
    int items = 0;
    if (_searchSession?.itemsPerPage != null && _searchSession.itemsPerPage >= 0)
    {
    items = _searchSession.itemsPerPage.Value;
    }
    return items;
    }

    public static int GetUserLevel()
    {
    int level = 3;

    if(_userSession != null)
    {
    level = _userSession.UserLevel;
    }

    return level;
    }
    }

    后将代码整理,成普通的方法类,Session问题解决。各Client内独立的Session工作正常

    public class SessionManager
    {
    private static SearchSession _searchSession;
    private static User _userSession;
    static log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private static HttpSessionStateBase BaseSession => _session;

    private static HttpSessionStateBase _session;

    public static void SetSessionManager(SType type, Object value)
    {
    switch (type)
    {
    case SType.Search:
    if (value != null)
    _searchSession = (SearchSession)value;
    break;
    case SType.User:
    if (value != null)
    _userSession = (User)value;
    break;
    }
    }

    public static void SetSessionBase(HttpSessionStateBase sessionBase)
    {
    _session = sessionBase;
    BaseSession["OrgName"] = GetUserSession().OrgName;
    BaseSession["UserLevel"] = GetUserSession().UserLevel;
    }

    public static Object GetSession(string key)
    {
    return BaseSession[key];
    }

    public static void SetSession(string key, Object value)
    {
    BaseSession[key] = value;
    }

    public static void SetPageSession(SearchView sv)
    {
    _searchSession.itemsPerPage = sv.itemsPerPage;
    _searchSession.pageNumber = sv.pageNumber;
    }

    public static void ClearPageSession()
    {
    _searchSession.itemsPerPage = null;
    _searchSession.pageNumber = null;
    }

    public static void SetSession(SearchView sv)
    {
    DefaultValues.CopyValues<SearchView, SearchSession>(sv, _searchSession);
    }

    public static void SetSearchConditionSession(SearchView sv)
    {
    //The name list of the member of the class of SearchView.
    List<string> listNames = new List<string>(){"licenseName",
    "licenseNo" ,
    "firstName",
    "familyName",
    "extictJpType",
    "extictJpYear",
    "registerPlaceGen",
    "registerPlaceName",
    "certificateJpType",
    "certificateGetYear",
    "PRE_ID_POST",
    "ORG_ID_POST",
    "itemsPerPage",
    "CertifInfosIQuery",
    "sosu",
    "sortList1Selected",
    "sortList2Selected",
    "sortList3Selected"};
    List<string> errorNames = new List<string>();

    listNames.ForEach(name => { if (DefaultValues.CheckMemberName<SearchView>(name) == false) errorNames.Add(name); });

    if (errorNames.Count > 0) errorNames.ForEach(m => logger.Info("SearchView クラス中に下記の違うメンバーを使っています:" + m + " "));

    //DefaultValues.CopyValuesByType<SearchView, SearchSession>(sv, _searchSession, listNames);
    }

    public static void GetSession(ref SearchView sv)
    {
    //他の画面に遷移するとき、検索画面の入力値をセッションに保存して
    DefaultValues.CopyValues<SearchSession, SearchView>(_searchSession, sv);
    }

    public static void GetSelectListInfoBySession(SearchView searchView)
    {
    SearchBase listInfo = new SearchBase();

    //Copy ListInfo data into listInfo from searchSession
    DefaultValues.CopyValues<SearchSession, SearchBase>(_searchSession, listInfo, false);

    //Copy data into searchView
    DefaultValues.CopyValues<SearchBase, SearchView>(listInfo, searchView);

    }

    public static SearchSession GetSearchSession => _searchSession;

    public static User GetUserSession()
    {
    if (_userSession != null)
    return _userSession;
    else
    return (User)(BaseSession["UserInfoSession"]);
    }

    public static void ClearUserSession()
    {
    BaseSession?.Clear();
    //BaseSession["UserInfoSession"] = null;
    _session = null;
    }

    public static int GetItemsPerPage()
    {
    int items = 0;
    if (_searchSession?.itemsPerPage != null && _searchSession.itemsPerPage >= 0)
    {
    items = _searchSession.itemsPerPage.Value;
    }
    return items;
    }

    public static int GetUserLevel()
    {
    int level = 3;

    if(_userSession != null)
    {
    level = _userSession.UserLevel;
    }

    return level;
    }
    }

    Love it, and you live without it
  • 相关阅读:
    精妙Sql语句
    andrid向sdcard写入文件
    mysql的编码问题以及购物网站的数据库设计
    (转)ASP.NET MVC最佳实践(3)
    (转)Asp.Net MVC 体验 4 Unity使用
    (转)Visual Studio 2008 单元测试
    (转)ASP.NET MVC最佳实践(1)
    (转)[EntLib]微软企业库5.0 学习之路——第十步、使用Unity解耦你的系统—PART3——依赖注入
    (转)[EntLib]微软企业库5.0 学习之路——第十步、使用Unity解耦你的系统—PART2——了解Unity的使用方法(1)
    (转)
  • 原文地址:https://www.cnblogs.com/tomclock/p/6064652.html
Copyright © 2011-2022 走看看