tags:

views:

304

answers:

2

Suppose each user has certain preferences that is saved in database for that user. For example: UnitSystem, UILanguage, TimeZone,...

When an http-request is made we need to have access to user preferences (e.g UnitSystem, TimeZone, ...) to correctly process the data and render the view in the correct language.

What is the correct way to save/access user preferences during program execution?

  1. Read the user preferences from database for each http-request
  2. Read user preferences once when user logs into the application and save it in session variables.
  3. Read user preferences once when user logs into the application and save it in a cookie.

How do you handle global settings in your MVC based applications?

A: 

In your case, I would probably store the setting in the session and cookie and check them in this order:

  1. Check the session for the variable.
  2. If not in the session, check the cookie and store cookie value in session.
  3. If not in cookie, check database and store in cookie and session.

That way you should be able to handle session timeouts & users with cookies turned off pretty transparently while still maximizing performance by hitting the DB only when absolutely necessary.

Of course you'll need some mechanism to update the cookie and session as well if the user changes their preferences in the DB. Assuming these preferences are set in the same application, that shouldn't be too big of a deal.

Eric Petroelje
Thanks for the advice. CasperOne suggested to use profile providers which sounds interesting and I might use for other purposes but for this case I prefer to handle it in my own code.
xraminx
+1  A: 

This is what the Profile providers are for in ASP.NET. Take a look at the section of the MSDN documentation titled "ASP.NET Profile Properties Overview", located at:

http://msdn.microsoft.com/en-us/library/2y3fs9xs.aspx

You could then create your own profile provider (if one of the provided ones doesn't suit your needs) which would load/save the profile data.

casperOne
Thanks for the great tip. Sounds interesting but I prefer to avoid automatic tools that handle things behind the scenes if I can code the same functionality within a reasonable time. I will have more control over the details and maintenance if easier.
xraminx
@xraminix: The profile class is not a tool, it's an inherent part of the ASP.NET model, like the response, the request, etc, etc. It just has a plug in model that makes it extensible.
casperOne