prcontra
prcontra

Reputation: 462

ASP.NET - Alternatives to Session Variables in C#

So I've always used session variables when needed (e.g. saving user form data from one registry step to another).

I've been told that a web application should always be stateless and that the problem with the session variables is the more users that access the web application the more memory of the web server is going to be used, which will eventually break the application in terms of scalability.

What I wanted to know is what alternatives are being used today to couple a User to it's data. In my case i have an application that has a registry form with 4 separate steps and i want to allow the users to continue the registry process where they left.

Thanks in advance to all who answer.

Upvotes: 0

Views: 3601

Answers (2)

EdSF
EdSF

Reputation: 12341

In the end, to provide any definition of "persistence" (aka "session"), you will have to use some storage mechanism.

  • Session stores: not just "memory" (of the same app server). You have other options for storage.

  • Client side: Web Storage

    • obviously whatever you store here will be accessible to the client, so caveat emptor (nothing sensistive, don't trust blindly, validate!).

You can come up with more based on both the above and use something entirely different (e.g. some external storage service), but you'll have to manage it on your own (using the same concepts - e.g. CRUD, expire, validation, etc).

Hth.

Upvotes: 1

photo_tom
photo_tom

Reputation: 7342

In our applications, we save all user state data to the database to allow users to return to where they left off. If you save the information in session, when users log off or close their browser window, session variables can be lost.

To make this easy to use, we created a UserStateManager class. It works as follows

  1. To retrieve information, we created the below function. The first time, this is called, it reads the information from the database. It then saves the information in local cache memory. This way, the next time the page is loaded, the information is already present and a database call can be skipped.

    var i = UserStateManger.Get("object name");

  2. To save information, we created the below function/ Whenever this function is called, it writes the current state information to the database and to the local cache memory.

    UserStateManager.Save("object name", "data object");

You should allow any datatype to be saved as "data object". This can be either a value type or class object.

Upvotes: 2

Related Questions