开发者

.Net Session Scoped Variables?

I'm new in .net and i'm using MVC2 + linQ and C#

I want to know if is possible to use session scoped variables or clases and how to do that? Do i Need to store it in a session object ? or just to declare a global variable ?

I need this to开发者_开发知识库 store data from diferent sql tables associates to the logged in user

Thanks in advance


Yes, you should be able to use the Session object from within your Controller's action methods to store and retrieve session data.

Session["MyData"] = myData;
MyDataType myData = (MyDataType)Session["MyData"];

Obviously be careful with the size of objects you are storing and how Session data is handled i.e. in process, SQL Server or application server. If you are not storing your session data in memory then you will need to ensure that it is serializable.


Yes since it's session scoped data, the session would be to the place to put that data, there's nothing wrong with that.

The only down side I can see is that if the session times out you lose that data - so if it's data that you want to keep (instead of i.e. just read-only or temporary data), you probably want to save the state in your DB instead.


Controllers are re-created for every request, so if you want to store something per session, Yes, you need to store it in the Session["key"].

Another option that might make sense (depending upon what you're doing) would be to use a DI Container to inject the session scoped variables via the constructor, and configure their scope with the DI tool to be per session.


As others have said the syntax is straightforward, you can simply treat Session like a hashmap or Dictionary if you want.

Session["userData"] = userData;
UserInfo userData = Session["userData"] as UserInfo;

Note (since you're new to C#) that the "as" keyword is basically a nullable cast; if the cast fails your userData variable will be set to null.

For extra credit, (and depending on your app's requirements) you could wrap all data access in a DAO or repository-like object, and have that object use Session under the hood. The location of data storage is an implementation detail that the controller itself doesn't need; fetching it from the Session vice the database (or an xml file, or static in-memory objects, or whatever) should be irrelevant to the controller (whose main job should be controlling application flow).


The short answer is that you should not use global variables - they only work during a single request. So you would have to use the Session property.

But you should avoid using it anyway IMHO.

And you always may use the UserData member of the forms ticket if you want to have custom data associated to the logged in user.

Edit: I am not from an English speaking country, so sometimes I use "cannot" when I should be using "should not". To avoid any more downvoting, let's make something clear here - of course you can use global variables in a web, multithreaded application. That's just not very smart, since all referenced objects will be lost at the end of the request. But feel free to do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜