Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In our webapplication we have a list of questions that have to be answered by the user. These questions are served to the user one by one and will be saved once the last question has been answered.

The problem we faced was saving all the 'help'-data that goes with this: storing the index of the last question, returning whether or not you're at the last question, returning the answered questions for the overview, etc.

Initially we stored this data each into its own session. This worked, but it also meant we had about 5 different session variables for each type of question list and a bunch of casts. I've removed these session variables by creating a few extra fields in the viewmodel and storing the viewModel in its entirety inside a session. This made sure we had our temporary data persisted troughout requests (each question solved meant a new request), removed a great deal of sessions and made the code more readable.

Another example of usage: our local user object gets overwritten every request because it's being obtained from a repository/databasecontext that's re-created every request (ninject). This also meant that we couldn't just keep a temporary list in our user that holds the already answered questions their answers, since it'd be emptied every request. Using this approach we can save this list in the session object, write it to the local user at the start of the action method, perform the action (save a new answer) and afterwards obtain this list and write it to the viewmodel. It's a bit of a workaround, but it made sure we could keep this data.

I believed this to be a decent solution, but now one of the project members (it's a school project due tomorrow) expressed his doubt about this method and said it was a very dirty solution (no alternative provided though).

We're using ASP.NET MVC 4.

Have I approached this the right way? How should I have solved it differently?

share|improve this question
2  
What's wrong with the sessions? It's exactly their purpose: storing small data over multiple HTTP requests. –  Florian Margaine May 19 '13 at 11:29
    
That's what I was thinking as well, but since I'll be defending this method in front of 4 teachers during the presentation, I'd like to be prepared for any comments they might have about this. –  Jeroen Vannevel May 19 '13 at 12:27
    
I might have misinterpreted the question: are you saying I should've left the sessions as they were? I'm talking about 15 session variables here, both controllers and views were stuffed with it. –  Jeroen Vannevel May 19 '13 at 15:02
    
Only the controller should bother with sessions. But a few bytes of session shouldn't hurt. –  Florian Margaine May 19 '13 at 15:56

1 Answer 1

up vote 2 down vote accepted

Go with the ASP.NET Session object - it was made exactly for "persisting temporary data over HTTP requests" and it sounds like it was working for you.

Your project mates should recognize that a "dirty" solution which is working is better than a "clean" solution which is not. Real-world software development is about iteration and shipping working software.

15 session variables is ok. If you're worried about performance - remember never to optimize prematurely. If you're worried about cleaner abstractions - remember you can store strongly-typed objects in Session.

My suggestion is to model the answers/progress i.e. as a class and store that in Session.

p.s. hope your presentation went well!

share|improve this answer
    
I believe I see what you're saying: instead of putting everything in my viewmodel on the top layer, I should've created a wrapper class for the answers and the progress and stored this as an object in the viewModel. Rather than storing the entire viewModel in a session, I would use this wrapper object, store it in session and work on this sessio object. The viewmodel would then just be used to provide the general information of the questionnaire. Have I got this right? The presentation is friday so I still have a few days to work on my arguments. Thanks! –  Jeroen Vannevel May 20 '13 at 11:17
1  
Sounds like you've got it right. C# being object-oriented, you always want to encapsulate data and logic as objects but also follow the single-responsibility principle. Good luck! –  jkoreska May 20 '13 at 12:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.