静态字段VS会话变量 [英] Static fields vs Session variables

查看:256
本文介绍了静态字段VS会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直在使用会话的一些变量传递从一个网页到另一个。例如用户角色。当用户在登录到Web应用程序的用户的角色ID保持在会话和该角色是在应用的不同部分进行检查。我最近开始思考为什么不能用静态成员代替。我可以(在任何地方以及在其中的静态字段位于包含的命名空间。)存储相同的信息在一个静态字段,方便地访问它的任何地方在我的应用程序,我知道使用会话变量来得心应手,有时,使得:

So far I've been using Session to pass some variables from one page to another. For instance user role. When a user logs in to the web application the role id of the user is kept in Session and that role is checked at different parts of the application. I've recently started thinking why not use static members instead. I can store the same information in a static field and easily access it anywhere in my application (well anywhere the namespace in which that static field resides is included.) I know that using Session variables comes handy sometimes, such that:

  1. 任何类型的数据可以被存储在会话。然后,它必须铸造however.But静态字段接受正确的数据类型的数据而已。
  2. 会话变量将经过一定的时间,这是我们需要在许多情况下,行为过期。

除上述外,还有什么其他的原因,我不应该使用静态字段来存储数据,并把它随处可得?

Apart from the above, are there any other reasons why I should not use static fields to store data and have it available everywhere?

推荐答案

没有,使用静态变量,这是的没有的路要走:

No, using static variables for this is not the way to go:

  • 如果您的AppDomain回收,所有静态变量将是复位
  • 在静态变量不发生横向扩展 - 如果负载平衡应用程序时,谁打一个服务器,然后不同的人会不会看到的第一台服务器的静态变量的数据sotre用户
  • 最重要的是,静态变量将被共享的所有的访问该服务器......它不会在每个用户的基础上在所有...然而,从你的描述,你不会告发'T希望用户X看看用户Y的信息。
  • If your AppDomain is recycled, all your static variables will be "reset"
  • Static variables don't scale horizontally - if you load-balance your application, a user who hits one server then a different one won't see the data sotre in the static variables in the first server
  • Most importantly, static variables will be shared by all access to that server... it won't be on a per-user basis at all... whereas from your description, you wouldn't want user X to see user Y's information.

从根本上说,你有两个选择周边的应用程序传播的信息:

Fundamentally, you have two choices for propagating information around your application:

  • 在保持它的客户端,所以每个请求给从previous步骤的信息。 (这可以变得难以处理具有大量信息,但也可以是简单的情况下是有用的。)
  • 保持它的服务器端,理想地在一些持久的方式(如数据库)与客户端提供的会话标识符。
  • Keep it client-side, so each request gives the information from the previous steps. (This can become unwieldy with large amounts of information, but can be useful for simple cases.)
  • Keep it server-side, ideally in some persistent way (such as a database) with the client providing a session identifier.

如果的,您可以使用负载平衡,以保持连接到同一个服务器上的所有用户,而如果的,你不介意的AppDomain回收时丢失会话< SUP> 1 或服务器下去的时候,你可以把它在内存中,通过会话ID键控......但要小心。

If you can use load-balancing to keep all users going to the same server, and if you don't mind sessions being lost when the AppDomain is recycled1 or a server going down, you can keep it in memory, keyed by session ID... but be careful.

1 有可能是在ASP.NET机制的生存之本,传播会话信息从一个AppDomain中的另一个 - 我不知道

1 There may be mechanisms in ASP.NET to survive this, propagating session information from one AppDomain to another - I'm not sure

这篇关于静态字段VS会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆