Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ways to store an object across multiple postbacks

For the sake of argument assume that I have a webform that allows a user to edit order details. User can perform the following functions:

  • Change shipping/payment details (all simple text/dropdowns)
  • Add/Remove/Edit products in the order - this is done with a grid
  • Add/Remove attachments

Products and attachments are stored in separate DB tables with foreign key to the order.

Entity Framework (4.0) is used as ORM.

I want to allow the users to make whatever changes they want to the order and only when they hit 'Save' do I want to commit the changes to the database. This is not a problem with textboxes/checkboxes etc. as I can just rely on ViewState to get the required information. However the grid is presenting a much larger problem for me as I can't figure out a nice and easy way to persist the changes the user made without committing the changes to the database. Storing the Order object tree in Session/ViewState is not really an option I'd like to go with as the objects could get very large.

So the question is - how can I go about preserving the changes the user made until ready to 'Save'.

Quick note - I have searched SO to try to find a solution, however all I found were suggestions to use Session and/or ViewState - both of which I would rather not use due to potential size of my object trees

like image 630
Marek Karbarz Avatar asked Jan 07 '10 21:01

Marek Karbarz


4 Answers

If you have control over the schema of the database and the other applications that utilize order data, you could add a flag or status column to the orders table that differentiates between temporary and finalized orders. Then, you can simply store your intermediate changes to the database. There are other benefits as well; for example, a user that had a browser crash could return to the application and be able to resume the order process.

I think sticking to the database for storing data is the only reliable way to persist data, even temporary data. Using session state, control state, cookies, temporary files, etc., can introduce a lot of things that can go wrong, especially if your application resides in a web farm.

like image 65
Jacob Avatar answered Nov 13 '22 09:11

Jacob


If using the Session is not your preferred solution, which is probably wise, the best possible solution would be to create your own temporary database tables (or as others have mentioned, add a temporary flag to your existing database tables) and persist the data there, storing a single identifier in the Session (or in a cookie) for later retrieval.

like image 32
Jason Berkan Avatar answered Nov 13 '22 10:11

Jason Berkan


First, you may want to segregate your specific state management implementation into it's own class so that you don't have to replicate it throughout your systems.

Second, you may want to consider a hybrid approach - use session state (or cache) for a short time to avoid unnecessary trips to a DB or other external store. After some amount of inactivity, write the cached state out to disk or DB. The simplest way to do this, is to serialize your objects to text (using either serialization or a library like proto-buffers). This helps allow you to avoid creating redundant or duplicate data structure to capture the in-progress data relationally. If you don't need to query the content of this data - it's a reasonable approach.

As an aside, in the database world, the problem you describe is called a long running transaction. You essentially want to avoid making changes to the data until you reach a user-defined commit point. There are techniques you can use in the database layer, like hypothetical views and instead-of triggers to encapsulate the behavior that you aren't actually committing the change. The data is in the DB (in the real tables), but is only visible to the user operating on it. This is probably a more complicated implementation than you may be willing to undertake, and requires intrusive changes to your persistence layer and data model - but allows the application to be ignorant of the issue.

like image 29
LBushkin Avatar answered Nov 13 '22 11:11

LBushkin


Have you considered storing the information in a JavaScript object and then sending that information to your server once the user hits save?

like image 1
Jeremy Bandini Avatar answered Nov 13 '22 11:11

Jeremy Bandini