Asp.net MVC overiding default getter and setters for linq to sql, for doing UTC conversions
I'm getting to a point with my app where I'm about to try to roll out utc support.
I've already got it all working and have written myself two utility classes, called convertToUtc and convertFromUtc. I think you can guess what they do.
What I was thinking though, could I开发者_如何转开发 build these into the getter and setter methods for my date property on the linq-to-sql created object model, or should I just go round the app everywhere re assigning how the value is saved (adding an extra line to the controller like
task.taskDeadline = Utility.ConvertToUtc(aspnet_Repository.GetUserGuid(User.Identity.Name), task.taskDeadline.Value);
If anyone can tell me I'm doing something horrible by going back to the db each time I need the user Guid, that would be cool. I guess down the line somewhere I would cache it, but this would require holding it in session or something I presume.
Thanks all.
You should always store datetimes in your database as UTC values (if you know the true UTC time for the object) and NEVER as local time values.
The conversion from UTC time to localtime is [mostly] a rendering issue that should be dealt with in your UI layer.
One exception to this latter rule is when you are trying to GROUP by datetime for the user (e.g. what happened yesterday or last month) in which case the UI needs to pass the timezone down to the business layer, and maybe even to the database in order to execute an efficient query grouped by a local representation of the datetime.
精彩评论