views:

105

answers:

1

In my Webforms 3.5 application, I have a GridView of users that displays the last time they logged in. This value is stored in the Users table in UTC. When an Administrator views the grid, I want them to view it the time zone the Administrator has selected in their preferences (also stored in the Users table).

I have this working properly for the GridView:

<ItemTemplate>
    <%# TimeZoneInfo.ConvertTimeFromUtc(Eval("LastLoginDateTimeUTC"), TimeZoneInfo.FindSystemTimeZoneById(UserService.GetUser(HttpContext.Current.User.Identity.Name).DisplayTimeZone))%>
</ItemTemplate>

So in this case, every time a row is created, a user object is created.

What I would like to do is determine the best way to handle returning commonly used User specific data (such as user.DisplayTimeZone). I have several ideas in mind, but I wanted to get some input on how others are doing it. I was looking at using a User Specific Cache, but didn't want to implement until I had more ideas.

I would like to avoid the Session object, but it's not a requirement. I would prefer to use the HttpRuntime.Cache.

Also, once this is determined, where should the code for this go? In a BasePage class? In the MasterPage? In an MVP BasePresenter?

Thanks ~S

+2  A: 

I've done the same using a user specific cache as you mentioned. I actually implemented it in a separate namespace though as static get properties. For example:

namespace MyWeb.Session
  public static class CurrentUser {
    public static int DisplayTimeZone {
      get {
        // Check cache first.
        ...

        // Cache miss, load from database and store in cache.
        ...
      }
    }
  }
}

That way every time I needed the value I simply call MyWeb.Session.CurrentUser.DisplayTimeZone.

DavGarcia
Since your class is static, there's no need to use the cache. Just store the data in a local variable (which must be static since the class is static), and it will remain in memory.
Robert C. Barth
Just storing the class in memory doesn't work since the calling page wouldn't know which user the call coming from. There would have to be user specific caching, which is handled by the static property
Steve Wright