将会话处理包装在静态类中
我将所有直接会话交互分离到一个单独的类中并将其设为静态,因为我不想多次创建新对象。 但是,我希望确保不存在并发问题或其他不稳定的意外。
这是代码:
public static class HttpHelper
{
public static string Get(string key)
{
object value = HttpContext.Current.Request.QueryString[key];
return (value == null) ? null : value.ToString();
}
public static string Post(string key)
{
object value = HttpContext.Current.Request.Form[key];
return (value == null) ? null : value.ToString();
}
public static string Session(string key)
{
object value = HttpContext.Current.Session[key];
return (value == null) ? null : value.ToString();
}
public static void ClearSession(string key)
{
HttpContext.Current.Session[key] = null;
}
public static void StoreInSession(string key, object value)
{
HttpContext.Current.Session[key] = value;
}
}
I separated all direct session interaction into a separate class and made it static, because I didn't want to create a new object several times. However, i wish to make sure that there are not concurrency issues or other wonky suprises.
Here is the code:
public static class HttpHelper
{
public static string Get(string key)
{
object value = HttpContext.Current.Request.QueryString[key];
return (value == null) ? null : value.ToString();
}
public static string Post(string key)
{
object value = HttpContext.Current.Request.Form[key];
return (value == null) ? null : value.ToString();
}
public static string Session(string key)
{
object value = HttpContext.Current.Session[key];
return (value == null) ? null : value.ToString();
}
public static void ClearSession(string key)
{
HttpContext.Current.Session[key] = null;
}
public static void StoreInSession(string key, object value)
{
HttpContext.Current.Session[key] = value;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果存在任何功能问题,许多应用程序早就失败了:)
但是,我要指出的是,这个模型不太适合单元测试。 您可能需要考虑使方法成为实例成员并将 Session 提供程序传递到此 HttpHelper 对象的构造函数中。
If there were any functional problems with this, many applications would have failed long ago :)
However, I would point out that this model is not very good for unit testing. You might want to consider making the methods instance members and passing a Session provider into the constructor of this HttpHelper object.
这是一个类似问题的答案,可能会对您有所帮助 - 它将使您能够避免使用密钥来完全访问会话值并为您提供类型安全的属性:
如何从 ASP.NET 中的任何类访问会话变量?
Here's an answer from a similar question that may help you - it will enable you to avoid using keys to access the session values altogether and give you type-safe properties:
How to access session variables from any class in ASP.NET?
从概念上讲,你应该没问题。 即使在部分回发(EG、AJAX)场景中,您可能会遇到竞争条件,也应该没问题。 会话状态使用读取器/写入器锁来保持你安全了。
我倾向于在我的项目中做类似的事情,尽管我喜欢将实际有效的会话条目(键等)封装到属性中。 我发现它使应用程序代码更加一致,而且更重要的是,消除了魔术键字符串上的拼写错误的可能性,该拼写错误将使应用程序以极其意想不到的方式运行。
如果您对应用程序状态执行类似的操作,那么您必须确保在设置值之前锁定和解锁值。
Conceptually, you should be fine. Even in a partial-postback (EG, AJAX) scenario where you might expect race conditions, you should be OK. Session state uses a reader/writer lock to keep you safe.
I tend to do something similar in my projects, though I'm a fan of encapsulating out the actual valid session entries (keys, etc.) into properties. I find it keeps the app code more consistent, and -- more importantly -- removes the possibility of a typo on a magic key string that would make the app behave in wildly unexpected ways.
If you do something similar for app state, then you do have to make sure you lock and unlock values before setting them.
我不确定这个包装器是否有用,但我认为您可以进行以下改进
相反,
您可以使用
相同的 Post 方法。
如果您可以通过 StoreInSession 方法在会话中存储任何对象,那么您的 Session 方法是否只返回 string ?
I am not sure is this wrapper usefull,but I think you could do the following improvements
Instead of
you can use
The same for Post method.
And is it true that your Session method returns only string , if you can store any object in session by StoreInSession method?