使用 Facade 设计模式管理 ASP.Net Session 变量
ASP.NET 提供了
HttpSessionState 类用来存储
session-state 的值。每一个 HTTP 请求都包含一个 HttpSessionState 类的实例,可以通过
HttpContext.Current.Session 属性来获取。
在开发中对 Session 进行集中管理,更便于维护和修改。这里给出一个使用 Facade 设计模式管理 ASP.Net Session 变量的示例。
1 using System;
2 using System.Web;
3
4 /// <summary>
5 /// SessionManager 的摘要说明
6 /// </summary>
7 public static class SessionManager
8 {
9 private const string OWNED_ENTERPRISE = "Enterprise";
10 private const string START_DATE = "StartDate";
11 private const 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
2 using System.Web;
3
4 /// <summary>
5 /// SessionManager 的摘要说明
6 /// </summary>
7 public static class SessionManager
8 {
9 private const string OWNED_ENTERPRISE = "Enterprise";
10 private const string START_DATE = "StartDate";
11 private const 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
posted on 2006-12-06 16:24 Easy Company 阅读(374) 评论(0) 编辑 收藏 举报