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 + "\n"));

//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 + "\n"));

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

posted @ 2016-11-15 10:22  tomclock  阅读(1766)  评论(0编辑  收藏  举报