【问题标题】:what attribute for protobuf-net for DbGeography typeDbGeography 类型的 protobuf-net 的什么属性
【发布时间】:2014-09-04 09:43:50
【问题描述】:

我一直在阅读documentation 的 protobuf-net 属性,但我并不精通 protobuf 规范。

我在 MVC 项目中使用它,在反序列化期间,DbGeography 为空。

public class Foo
{
    [ProtoMember(1)]
    public int Id { get; set; }        
    [ProtoMember(2, AsReference = true)]
    public DbGeography Location { get; set; }
}

这就是我的班级目前的样子,我尝试过使用DynamicTypeIsRequired,无论如何都不起作用。我希望有人一直在做类似的事情,而不是试图猜测/混合和匹配。

我在环顾四周时看到的最接近的东西是这个项目,它有一个自定义 readerwriter,它使用 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; }
}

【问题讨论】:

    标签: c# asp.net-mvc serialization protobuf-net


    【解决方案1】:

    重新更新:

    [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));
    

    【讨论】:

    • 所以我应该保留原来的类并删除dynamictype = true?会不会有下游效应?
    • @LeeGary 定义“下游效应”?它与DynamicType 序列化不兼容,但它可能工作,这总是很好。请注意,显示的实现仅是说明性的——我自己不使用DbGeography,所以它来自记忆。关键是:与其尝试序列化定制的、复杂的和我们无法控制的东西:将其更改为简单的东西并且在我们的控制范围内
    • 下游效果如中,因为我们正在改变运行时类型模型。它会影响使用 DbGeography 的代码的其他部分吗?我已经稍微更新了问题以包含一个 List 成员,我使用了 AsReference=True,这也是要走的路吗?
    • @LeeGary 很抱歉您的编辑被阻止;很好;审稿人需要感到羞耻
    • @LeeGary 我会倾向于避免使用AsReference,除非有充分的理由——你想在那里做什么?是经常重复的地理吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 2010-10-31
    • 2015-09-10
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多