Asp.net Strong type Session
using HttpContext = System.Web.HttpContext;
using System.Threading;
namespace CRMWeb.eDAS.Util
{
public sealed class EDasSession<TClass> where TClass : class ,new()
{
#region member
private readonly ReaderWriterLockSlim _lockSlim = new ReaderWriterLockSlim();
#endregion
#region methods
public TClass Current
{
get
{
return HttpContext.Current.Session[Key] == null
? null
: (TClass)HttpContext.Current.Session[Key];
}
}
public TClass Set(TClass value)
{
var sessionObj = HttpContext.Current.Session[Key];
if (sessionObj == null)
{
_lockSlim.EnterWriteLock();
if (sessionObj == null)
{
HttpContext.Current.Session[Key] = value;
}
_lockSlim.ExitWriteLock();
}
else
{
_lockSlim.EnterWriteLock();
HttpContext.Current.Session[Key] = value;
_lockSlim.ExitWriteLock();
}
return (TClass)sessionObj;
}
public void Remove()
{
HttpContext.Current.Session.Remove(Key);
}
#endregion
#region Private
private string Key
{
get { return typeof(EDasSession<TClass>).FullName; }
}
#endregion
}
}
You can create a Website context like this :
using CRMWeb.eDAS.Entities.SessionEntity;
namespace CRMWeb.eDAS.Util
{
public sealed class EdasContext
{
public static EDasSession<SessionBranchInfo> BranchLoginSession;
public static EDasSession<SessionUserInfo> SalesPersonSession;
public static EDasSession<SessionCustomerQueueInfo> CustomerQueueSession;
public static EDasSession<SessionCustomerPersonalDetails> CustomerDetailsSession;
public static EDasSession<SessionPaymentInfo> CurrentPaymentSession;
public static EDasSession<SessionCheckoutInfo> CheckoutInfo;
public static EDasSession<SessionInfoVoucherDetails> VoucherDetailsSession;
public static EDasSession<SessionServiceInfo> CurrentServiceSession;
public static EDasSession<SessionTicketInfo> TicketInfoSession;
public static EDasSession<SessionShoppingCart> ShoppingCartSession;
static EdasContext()
{
if(BranchLoginSession == null) BranchLoginSession = new EDasSession<SessionBranchInfo>();
if(SalesPersonSession == null) SalesPersonSession = new EDasSession<SessionUserInfo>();
if(CustomerQueueSession == null) CustomerQueueSession = new EDasSession<SessionCustomerQueueInfo>();
if(CustomerDetailsSession == null) CustomerDetailsSession = new EDasSession<SessionCustomerPersonalDetails>();
if(CurrentPaymentSession == null) CurrentPaymentSession = new EDasSession<SessionPaymentInfo>();
if(CheckoutInfo == null) CheckoutInfo = new EDasSession<SessionCheckoutInfo>();
if(VoucherDetailsSession == null) VoucherDetailsSession = new EDasSession<SessionInfoVoucherDetails>();
if(CurrentServiceSession == null) CurrentServiceSession = new EDasSession<SessionServiceInfo>();
if(TicketInfoSession == null) TicketInfoSession = new EDasSession<SessionTicketInfo>();
if(ShoppingCartSession == null) ShoppingCartSession = new EDasSession<SessionShoppingCart>();
}
public static void ClearAll()
{
BranchLoginSession.Remove();
SalesPersonSession.Remove();
TicketInfoSession.Remove();
ClearCustomerSession();
}
public static void ClearCustomerSession()
{
CustomerQueueSession.Remove();
CustomerDetailsSession.Remove();
CurrentPaymentSession.Remove();
CheckoutInfo.Remove();
VoucherDetailsSession.Remove();
CurrentServiceSession.Remove();
ShoppingCartSession.Remove();
}
}
}
相關文章
- ASP.NET:EntityFramework實現SessionASP.NETFrameworkSession
- asp.net session的應用ASP.NETSession
- 在asp.net handler 中 使用 sessionASP.NETSession
- ASP.NET Session簡單介紹ASP.NETSession
- v$sql.command_type and v$session.commandSQLSession
- ASP.NET Session的七點認識ASP.NETSession
- oracle taf type型別為session和select的區別Oracle型別Session
- Asp.Net 中使用HttpModule 做Session驗證ASP.NETHTTPSession
- Asp.Net處理Session失效解決方案ASP.NETSession
- asp.net Session造成請求阻塞現象ASP.NETSession
- Asp.net中多專案共享Session (轉)ASP.NETSession
- iOS strong weak unowned引用iOS
- Swift中的Weak/Strong DanceSwift
- ASP.NET將Session儲存到資料庫中ASP.NETSession資料庫
- ASP.NET中Session的sessionState 4種mode模式ASP.NETSession模式
- 使用SQL Server儲存ASP.NET Session變數SQLServerASP.NETSession變數
- asp.net 一般處理程式接收SessionASP.NETSession
- asp.net core 實現支援自定義 Content-TypeASP.NET
- 對 Strong-Weak Dance的思考
- iOS strong和copy的區別iOS
- 深入理解 weak-strong dance
- ORACLE SQL and SQL*PLUS (strong recommend)OracleSQL
- 對 Strong-Weak Dance 的思考
- B,strong,I,em的區別
- ASP.NET Core 中簡單Session登入校驗ASP.NETSession
- 【ASP.NET Core】使用最熟悉的Session驗證方案ASP.NETSession
- ASP.NET MVC使用Filter解除Session, Cookie等依賴ASP.NETMVCFilterSessionCookie
- ASP.NET常見物件之Cookie、Session與ApplicationASP.NET物件CookieSessionAPP
- Asp.net 2.0 Session 丟失的幾種情況ASP.NETSession
- iOS中copy和strong的個人理解iOS
- IOS @proporty 關鍵字(一)retain strongiOSAI
- ASP.NET Core Web Api之JWT VS Session VS Cookie(二)ASP.NETWebAPIJWTSessionCookie
- ASP.NET Core 使用 Redis 和 Protobuf 進行 Session 快取ASP.NETRedisSession快取
- 在ASP.NET中使用Session常見問題集錦ASP.NETSession
- asp.net Session丟失的解決辦法小結ASP.NETSession
- iOS之property裡的copy、strong區別iOS
- ASP.NET一般處理程式訪問Session問題ASP.NETSession
- variable: Type 與 Type variable