2013-12-11 26 views
2

protobuf-net是否支持嵌套類?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服務。

+0

它應該做的 - 你看到一個問題?如果是這樣:究竟是什麼?請注意,「訂單」僅需要在單一類型內唯一 - 地址中可以是1和2,因爲地址不會與人共享任何內容。 –

+0

我還沒有測試過。對不起,我嘗試之前問。我同意「地址中的訂單」類。順便說一句,如果你願意,我準備在幾個相關的問題上進行測試。 1. XmlType的名字應該遵循一些規則? 2. ProtoContact和XmlType屬性之間有什麼區別(如果有)?除了明顯的繼承[ProtoInclude(7,typeof(SomeDerivedType)]。BRB和我的測試 – stefan2410

+0

除非使用GetSchema,Protobuf-net根本不使用名稱。區別在於XML屬性不強制protobuf依賴到DTO,但protobuf屬性給你更具體的控制序列化 –

回答

2

在Marc Gravell的幫助下,我理解了嵌套類如何在protobuf中工作。

我用ServiceStack(ver。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過濾地址列表請與聯繫地址的人員名單,無關使用ProtoBufServiceClient

人員

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客戶端代碼,

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

非常感謝,Marc。