Controlling Acceptable Use of the Session Variable in ASP.NET

I tried to wean myself from using everything in the Session variable in ASP.NET (I came from the Windows programming background), and I completely stopped explicitly storing anything in the Session variable. Can someone give some recommendations regarding what you think is an acceptable use of a session variable?

Here is an example ... I load a business object from a database and fill and edit the screen. User can edit values ​​and save. The old way to load a business object, load a form, and save the business object into a session variable. If the user clicked the Save button, I would extract the business object from the session variable, replace the edited values, and save it. A new way to load a business object from the database and load my form. The user will edit the values ​​and click "Save." I would reload my business object from the database, replace the edited values ​​and save them. I am not a web programming expert, but I feel that the first way is wrong due to the bad stigma of using session variables, and I feel that the second way is wrong.because it just feels like a shitty way to do this (loading a business object twice). Do not consider any form of caching here. How would I handle this?

+3
source share
3 answers

I am not offended at all by reloading a business object from the database in the mail in order to save user changes.

This object must come from somewhere in this postback, and the limited overhead associated with quickly calling a database, such as capturing a specific object, is probably the best way.

Your options for returning this business object to memory in response:

  • Get it from the database again. Cons: some (small) additional DB overhead
  • . : - ( ) ( ) , , , , ASP.NET - .
  • . : , , , , , .
  • ViewState. Viewstate ( , ). : , . ViewState , . , ViewState - .
+7

?

- , .

SQL Server , - .

, , , . -, -, . , , . .

:)

+1

. concurrency.

, :

  • 1 1
  • 1 2
  • 1 1
  • 1 2

, (4) - concurrency, , Computer 2 . , , .

So, for such a situation, I feel that putting the original object into the session (or in a hidden field in the form) is absolutely correct if you don't care about concurrency at all.

Not to mention the fact that many people don't like hitting the database again for extra reading ...

0
source

Source: https://habr.com/ru/post/1707829/


All Articles