gpt4 book ai didi

servicestack - protobuf-net 嵌套类支持 ?订单注释?

转载 作者:行者123 更新时间:2023-12-04 05:29:21 25 4
gpt4 key购买 nike

protobuf-net 是否支持嵌套类?

属性类型名称和顺序是否正确?

如下面的代码,使用替代属性 XmlType/XmlElement 代替 ProtoContract/ProtoInclude

     using System.Xml.Serialization;

[XmlType("Person")]
public class Person
{
[XmlElement(ElementName = "Display Name",Order = 1)]
public string Name { get; set; }

[XmlElement(ElementName = "Age", Order = 2)]
public byte Age { get; set; }

[XmlType("Person.Address")]
public class Address
{
[XmlElement(ElementName = "Street",Order = 1)]
// before I had Order=3 to be unique, but as Mark says
// the "Order only needs to be unique inside a single type"
public string Street { get; set; }

[XmlElement(ElementName = "ZIP",Order =2)]
public string Zip { get; set; }

}
}

更新,在我下面的回答中,我写了最后一个类,用 protobuf 实现了一个 ServiceStack 服务。

最佳答案

在 Marc Gravell 的帮助下,我了解了嵌套类在 protobuf 中的工作方式。

我使用 ServiceStack(版本 3.9.71)服务对其进行了测试,它运行良好。

这是模型。

using System.Xml.Serialization;

[XmlType("Person")]
public class Person
{
[XmlElement(ElementName = "Display Name", Order = 1)]
public string Name { get; set; }

[XmlElement(ElementName = "Age", Order = 2)]
public byte Age { get; set; }

[XmlElement(ElementName = "Contact Address", Order =3)] // the address instance
public Address ContactAddress { get; set; }

[XmlElement(ElementName = "Person Id", Order = 4)]
public int Id { get; set; }

[XmlType("Person.Address")]
public class Address
{
[XmlElement(ElementName = "Street", Order = 1)]
// before I had Order=5 to be unique, but as Mark says
// the "Order only needs to be unique inside a single type"
public string Street { get; set; }

[XmlElement(ElementName = "ZIP", Order = 2)]
public string Zip { get; set; }

}
}

ServiceStack 请求

     public class PersonRequest
{
public string Zip { get; set; }
public byte Age { get; set; }
public int Id { get; set; }
}
public class AddressRequest
{
public string Zip { get; set; }

}

AppHost.Configure 中的 ServiceStack 路由。 (自托管服务)

   Routes        
.Add<PersonRequest>("/Person/{Id}", "GET, OPTIONS")
.Add<PersonRequest>("/Persons/{Zip}", "GET, OPTIONS")

.Add<AddressRequest>("/Addresses/{Zip}", "GET, OPTIONS");

服务

第一种情况,我们使用 Zip 过滤器询问具有联系地址的人员列表

第二种情况,使用 Zip 过滤器的地址列表,与人员无关

public List<Person>  Get(PersonRequest request)
{
List<Person> persons=new List<Person>();
persons.Add(new Person()
{ Name = "stefan", ContactAddress = new Person.Address(){Street="North Pole"}});
return persons;
}
public List<Person.Address> Get(AddressRequest request)
{ // it returns only addresses filtered by Zip
List<Person.Address> addresses=new List<Person.Address>();
addresses.Add( new Person.Address() { Street = "North Pole" } );
return addresses;
}

ServiceStack 客户端代码,使用 ProtoBufServiceClient

 using ServiceStack.Plugins.ProtoBuf;
...
var client = new ProtoBufServiceClient(serverUri);
List<Person> persons = client.Get<List<Person>>(serverUri + @"/Persons/600617");
List<Person.Address> addresses =
client.Get<List<Person.Address>>(serverUri + @"/Addresses/600617");

非常感谢,马克。

关于servicestack - protobuf-net 嵌套类支持 ?订单注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20526631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com