欢迎来到飞鸟慕鱼博客,开始您的技术之旅!
当前位置: 首页知识笔记正文

使用外观设计模式管理ASP.Net会话变量

终极管理员 知识笔记 69阅读

ASP.NET提供了HttpSessionState类来存储会话状态的值。每个HTTP请求都包含一个HttpSessionState类的实例,可以通过HttpContext获得。当前会话属性。正在开发的会话的集中管理使得维护和修改更加容易。下面是一个使用设计模式管理ASP.Net会话变量的例子。1使用系统;2使用系统。Web34//abstract 5//会话管理器的抽象描述6/6//abstract 7 span style=' color : rgba(0,0,255,1)' publistaticcallissionmanager 8 { 9 privateconststringOwned _ enterprise=' enterprise '10 privateconstringstart _ DATE=' start DATE '11私人消费

1)"> string STOP_DATE = "StopDate";
12 
13     /// <summary>
14     /// 获取当前用户名称
15     /// </summary>
16     public static string Username
17     {
18         get { return HttpContext.Current.User.Identity.Name; }
19     }
20 
21     /// <summary>
22     /// 获取或设置当前用户所属企业的信息。
23     /// </summary>
24     public static Enterprise OwnedEnterprises
25     {
26         get
27         {
28             Enterprise result =
                    (Enterprise)HttpContext.Current.Session[OWNED_ENTERPRISE];
29 
30             if (result == null)
31             {
32                 result = Enterprise.GetEnterprise (Username);
33                 OwnedEnterprises = result;
34             }
35 
36             return result;
37         }
38         private set
39         {
40             HttpContext.Current.Session[OWNED_ENTERPRISE] = value;
41         }
42     }
43 
44     /// <summary>
45     /// 获取或设置业务开始时间
46     /// </summary>
47     public static DateTime StartDate
48     {
49         get
50         {
51             if (HttpContext.Current.Session[START_DATE] == null)
52                 return DateTime.MinValue;
53             else
54                 return (DateTime)HttpContext.Current.Session[START_DATE];
55         }
56         set { HttpContext.Current.Session[START_DATE] = value; }
57     }
58 
59     /// <summary>
60     /// 获取或设置业务停止时间
61     /// </summary>
62     public static DateTime StopDate
63     {
64         get
65         {
66             if (HttpContext.Current.Session[STOP_DATE] == null)
67                 return DateTime.MaxValue;
68             else
69                 return (DateTime)HttpContext.Current.Session[STOP_DATE];
70         }
71         set { HttpContext.Current.Session[STOP_DATE] = value; }
72     }
73 }
74 

标签:
声明:无特别说明,转载请标明本文来源!