实体框架 – 我们如何使用Breeze的本地时区生存

前端之家收集整理的这篇文章主要介绍了实体框架 – 我们如何使用Breeze的本地时区生存前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在写这个来收集对我们的方法的意见,并希望帮助别人(和我的记忆).

脚本

>我们所有的数据库都使用没有时区信息的DateTime数据类型.
>在内部我们知道数据库中的所有日期/时间都在本地(新西兰)时间,而不是UTC.对于Web应用程序,这不是理想的,但是我们不支持所有这些数据库的设计,因为它们支持其他系统(会计,工资单等).
>我们正在使用实体框架(模型第一)进行数据访问.

我们的问题

>没有具体的时区信息,Breeze / Web Api / Entity Framework栈似乎赞成时间是UTC而不是本地的假设,这可能是最好的,但不适合我们的应用程序.
> Breeze喜欢以标准的UTC格式将日期传回服务器,特别是在查询字符串中(例如where子句).想象一下一个微风控制器,它直接从数据库中暴露出一个表作为IQueryable. Breeze客户端将以UTC格式将任何日期过滤器(where)子句传递到服务器.实体框架将忠实地使用这些日期来创建SQL查询,完全不知道数据库表日期在我们本地时区.对我们来说,这意味着结果与我们想要的结果相差12到13个小时(取决于夏令时).

我们的目标是确保我们的服务器端代码(和数据库)始终使用本地时区中的日期,并且所有查询返回所需的结果.

解决方法

我们的解决方案第1部分:实体框架

当实体框架从数据库获取DateTime值时,将其设置为DateTimeKind.Unspecified.换句话说,本地或UTC.我们特意将日期标记为DateTimeKind.Local.

为了实现这一点,我们决定调整实体框架的生成实体类的模板.而不是我们的日期是一个简单的属性,我们引入了一个后台存储日期,并使用属性设置器将日期设置为本地,如果它是未指定的.

在模板(.tt文件)中,我们更换了…

  1. public string Property(EdmProperty edmProperty)
  2. {
  3. return string.Format(
  4. CultureInfo.InvariantCulture,"{0} {1} {2} {{ {3}get; {4}set; }}",Accessibility.ForProperty(edmProperty),_typeMapper.GetTypeName(edmProperty.TypeUsage),_code.Escape(edmProperty),_code.SpaceAfter(Accessibility.ForGetter(edmProperty)),_code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
  5. }

…与…

  1. public string Property(EdmProperty edmProperty)
  2. {
  3. // Customised DateTime property handler to default DateKind to local time
  4. if (_typeMapper.GetTypeName(edmProperty.TypeUsage).Contains("DateTime")) {
  5. return string.Format(
  6. CultureInfo.InvariantCulture,"private {1} _{2}; {0} {1} {2} {{ {3}get {{ return _{2}; }} {4}set {{ _{2} = DateKindHelper.DefaultToLocal(value); }}}}",_code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
  7. } else {
  8. return string.Format(
  9. CultureInfo.InvariantCulture,_code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
  10. }
  11. }

这造成了一个相当丑陋的一线设置者,但它完成了工作.它使用帮助函数将日期默认为本地,如下所示:

  1. public class DateKindHelper
  2. {
  3. public static DateTime DefaultToLocal(DateTime date)
  4. {
  5. return date.Kind == DateTimeKind.Unspecified ? DateTime.SpecifyKind(date,DateTimeKind.Local) : date;
  6. }
  7.  
  8. public static DateTime? DefaultToLocal(DateTime? date)
  9. {
  10. return date.HasValue && date.Value.Kind == DateTimeKind.Unspecified ? DateTime.SpecifyKind(date.Value,DateTimeKind.Local) : date;
  11. }
  12. }

我们的解决方案第2部分:IQueryable过滤器

下一个问题是Breeze在将where子句应用于IQueryable控制器操作时通过UTC日期.在审查了Breeze,Web API和实体框架的代码后,我们决定最好的选择是拦截对我们的控制器操作的调用,并在本地日期的QueryString中交换UTC日期.

我们选择使用我们可以应用于我们的控制器操作的自定义属性来执行此操作,例如:

  1. [UseLocalTime]
  2. public IQueryable<Product> Products()
  3. {
  4. return _dc.Context.Products;
  5. }

实现此属性的类是:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Http;
  6. using System.Web.Http.Filters;
  7. using System.Text.RegularExpressions;
  8. using System.Xml;
  9.  
  10. namespace TestBreeze.Controllers.api
  11. {
  12. public class UseLocalTimeAttribute : ActionFilterAttribute
  13. {
  14. Regex isoRegex = new Regex(@"((?:-?(?:[1-9][0-9]*)?[0-9]{4})-(?:1[0-2]|0[1-9])-(?:3[0-1]|0[1-9]|[1-2][0-9])T(?:2[0-3]|[0-1][0-9]):(?:[0-5][0-9]):(?:[0-5][0-9])(?:\.[0-9]+)?Z)",RegexOptions.IgnoreCase);
  15.  
  16. public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
  17. {
  18. // replace all ISO (UTC) dates in the query string with local dates
  19. var uriString = HttpUtility.UrlDecode(actionContext.Request.RequestUri.OriginalString);
  20. var matches = isoRegex.Matches(uriString);
  21. if (matches.Count > 0)
  22. {
  23. foreach (Match match in matches)
  24. {
  25. var localTime = XmlConvert.ToDateTime(match.Value,XmlDateTimeSerializationMode.Local);
  26. var localString = XmlConvert.ToString(localTime,XmlDateTimeSerializationMode.Local);
  27. var encoded = HttpUtility.UrlEncode(localString);
  28. uriString = uriString.Replace(match.Value,encoded);
  29. }
  30. actionContext.Request.RequestUri = new Uri(uriString);
  31. }
  32.  
  33. base.OnActionExecuting(actionContext);
  34. }
  35. }
  36. }

我们的解决方案第3部分:Json

这可能更有争议,但是我们的网络应用程序受众也完全是本地的:).

我们希望Json默认发送到客户端以包含本地时区的日期/时间.此外,我们还希望从客户端收到的Json中的任何日期转换为本地时区.为此,我们创建了一个自定义的JsonLocalDateTimeConverter并交换出了Json转换器Breeze的安装.

转换器如下所示:

  1. public class JsonLocalDateTimeConverter : IsoDateTimeConverter
  2. {
  3. public JsonLocalDateTimeConverter () : base()
  4. {
  5. // Hack is for the issue described in this post (copied from BreezeConfig.cs):
  6. // https://stackoverflow.com/questions/11789114/internet-explorer-json-net-javascript-date-and-milliseconds-issue
  7. DateTimeFormat = "yyyy-MM-dd\\THH:mm:ss.fffK";
  8. }
  9.  
  10.  
  11. // Ensure that all dates go out over the wire in full LOCAL time format (unless date has been specifically set to DateTimeKind.Utc)
  12. public override void WriteJson(JsonWriter writer,object value,JsonSerializer serializer)
  13. {
  14. if (value is DateTime)
  15. {
  16. // if datetime kind is unspecified then treat is as local time
  17. DateTime dateTime = (DateTime)value;
  18. if (dateTime.Kind == DateTimeKind.Unspecified)
  19. {
  20. dateTime = DateTime.SpecifyKind(dateTime,DateTimeKind.Local);
  21. }
  22.  
  23. base.WriteJson(writer,dateTime,serializer);
  24. }
  25. else
  26. {
  27. base.WriteJson(writer,value,serializer);
  28. }
  29. }
  30.  
  31.  
  32. // Ensure that all dates arriving over the wire get parsed into LOCAL time
  33. public override object ReadJson(JsonReader reader,Type objectType,object existingValue,JsonSerializer serializer)
  34. {
  35. var result = base.ReadJson(reader,objectType,existingValue,serializer);
  36.  
  37. if (result is DateTime)
  38. {
  39. DateTime dateTime = (DateTime)result;
  40. if (dateTime.Kind != DateTimeKind.Local)
  41. {
  42. result = dateTime.ToLocalTime();
  43. }
  44. }
  45.  
  46. return result;
  47. }
  48. }

最后,我们创建了一个CustomBreezeConfig类:

  1. public class CustomBreezeConfig : Breeze.WebApi.BreezeConfig
  2. {
  3.  
  4. protected override JsonSerializerSettings CreateJsonSerializerSettings()
  5. {
  6. var baseSettings = base.CreateJsonSerializerSettings();
  7.  
  8. // swap out the standard IsoDateTimeConverter that breeze installed with our own
  9. var timeConverter = baseSettings.Converters.OfType<IsoDateTimeConverter>().SingleOrDefault();
  10. if (timeConverter != null)
  11. {
  12. baseSettings.Converters.Remove(timeConverter);
  13. }
  14. baseSettings.Converters.Add(new JsonLocalDateTimeConverter());
  15.  
  16. return baseSettings;
  17. }
  18. }

就是这样欢迎所有意见和建议.

猜你在找的asp.Net相关文章