DbGeography类型的protobuf-net的什么属性
本文关键字:属性 什么 protobuf-net DbGeography 类型 | 更新日期: 2023-09-27 18:20:33
我一直在阅读protobuf-net属性的文档,但我并不精通protobuf规范。
我在MVC项目中使用它,在反序列化期间,DbGeography为空。
类别
public class Foo
{
[ProtoMember(1)]
public int Id { get; set; }
[ProtoMember(2, AsReference = true)]
public DbGeography Location { get; set; }
}
这就是我的类目前的样子,我已经尝试使用DynamicType
&IsRequired
,无论如何都没用。而不是试图猜测/混合&match,我希望有人也做过类似的事情。
我环顾四周时看到的最接近的东西是这个项目,它有一个自定义的读取器和写入器,它使用protobuf来处理空间数据类型,但它是一个自定义类。
更新
public class Foo
{
[ProtoMember(1)]
public int Id { get; set; }
[ProtoMember(2, DynamicType= true)] //<-- this works
public DbGeography Location { get; set; }
}
但我仍然想知道给这些成员贴标签的最佳方式是什么,比如性能与尺寸的权衡
更新2
[ProtoContract]
public class Foo
{
[ProtoMember(1)]
public int Id { get; set; }
public string Address { get; set; }
[ProtoMember(2, AsReference = true)]
public DbGeography Location { get; set; }
[ProtoMember(8, AsReference = true)] //<-- What is the cost of using this for List<Bar>? since im using a non-primitive type
public List<Bar> Bars { get; set; }
}
重新更新:
[ProtoMember(2, DynamicType= true)] //<-- this works
我不建议这样做。至于正确的事情;我推荐一种代理类型:
[ProtoContract]
sealed class DbGeographyPoco
{
[ProtoMember(1)]
public string Value { get; set; }
public static implicit operator DbGeographyPoco(DbGeography value)
{
return value == null ? null : new DbGeographyPoco {
Value = value.WellKnownValue.WellKnownText };
}
public static implicit operator DbGeography(DbGeographyPoco value)
{
return value == null ? null : DbGeography.FromText(value.Value);
}
}
然后您可以在应用程序启动期间通过以下方式注册:
RuntimeTypeModel.Default.Add(typeof(DbGeography),false)
.SetSurrogate(typeof(DbGeographyPoco));