【发布时间】:2020-09-18 12:49:48
【问题描述】:
我正在使用 Ignite .NET 瘦客户端。
在 ignite 文档中写道,为了支持平台互操作性,我们应该以 UTC 格式存储 DateTime。我还可以以本地格式存储 DateTime 值,但在这种情况下无法在 DBeaver 中查看数据。 当我将数据放入缓存或从缓存中获取数据时,我必须手动将 DateTime 值从 UTC 或本地格式转换。对于转换,我使用 DateTime.SpecifiyKind 或 DateTime.ToUniverslaTime、DateTime.ToLocalTime 方法,如下例所示。 是否有任何通用解决方案可以在 UTC 和本地格式之间自动转换所有 DateTime 值,而不是为应用程序中每个模型的每个属性编写自定义逻辑? 也许它可能是某种通用的 DateTime 序列化器。
public class Person
{
public string FirstName { get; set; }
public DateTime Birthday { get; set; }
}
[STAThread]
public static void Main()
{
var configuration = new Core.Client.IgniteClientConfiguration
{
Endpoints = new List<string> { "127.0.0.1" },
BinaryConfiguration = new BinaryConfiguration
{
Serializer = new BinaryReflectiveSerializer { ForceTimestamp = true }
}
};
using (var ignite = Ignition.StartClient(configuration))
{
var person = new Person { FirstName = "John", Birthday = new DateTime(1990, 08, 17) };
var cacheConfiguration = new CacheClientConfiguration("people", new QueryEntity(typeof(int), typeof(Person)));
//person.Birthday = person.Birthday.ToUniversalTime();
person.Birthday = DateTime.SpecifyKind(person.Birthday, DateTimeKind.Utc);
var cache = ignite.GetOrCreateCache<int, Person>(cacheConfiguration);
cache.Put(0, person);
var personFromCache = cache.AsCacheQueryable().Select(x => x.Value).FirstOrDefault();
//personFromCache.Birthday = personFromCache.Birthday.ToLocalTime();
personFromCache.Birthday = DateTime.SpecifyKind(personFromCache.Birthday, DateTimeKind.Local);
}
}
【问题讨论】: