Asp.net Strong type Session

mybwu_com發表於2014-01-06

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();
        }

    }
}



相關文章