Tuesday, July 23, 2013

No database temp table needed

In the application I work on we allow users to upload photos when they create a work assignment. Those photos are getting stored in a temp table in the database so that as the form for creating assignments is reloaded we have a record of the photos that have been uploaded. I don't know why we do it this way, but recently the database team asked us to stop using that table so they could remove it and stop maintaining it. The reason it's an issue is because someone can upload files for an assignment and then never send the assignment. When that happens, being a web app, it's difficult on the server to recognize that the user left the page without finishing it, so those files become orphaned entries in the table which the database team has to clean up.

So my job was to find a way to maintain that file information in the session instead of in the database. Initially I thought it would be simple to just store the files in an object in the session, but as I started working on that I realized it would need to be cleared when a new assignment was started. Initially I thought oh that's easy, I'll just clear the temp files anytime we hit the code path that gets hit when the assignment is started, unfortunately that code path is hit when the page is reloaded after uploading a file. So my next thought was that instead of storing directly to the session I could use the object that represents the assignment and just count on that resetting the list when a new assignment is started. That seems to be working great.

This solution isn't ideal but it gets the job done. The reason I say it's not ideal is because I now have boiler plate code in several locations to get the assignment object, update it, then store it back in the session. I think the direction we want to move as we have time in the future is some kind of managed session object that moves the boiler plate stuff into a function I can call. For now though what I have gets the job done and it's fairly simple to deal with.