Paul Rooney
Paul Rooney

Reputation: 21609

Express Sequence of items in ServiceStack DTO classes

I am using ServiceStack to create a Soap service. I want to create a soap envelope which includes a structure like this

<Items>
  <Item />
  <Item />
</Items>

I.e. a container with a sequence of the same item.

I am stuck trying to work out how to express this in my DTO classes.

[DataContract]
public class Item // inherit ??
{
    // ??
}

[DataContract]
public class ItemListResponse : ResponseBase
{
    [DataMember]
    public Item[] Items { get; set; }
}

I think it should be expressed in the WSDL like this

<xs:simpleType name="Item">
  <xs:restriction base="xs:string" />
</xs:simpleType>
<xs:element name="Item" nillable="true" type="tns:Item" />
<xs:complexType name="Items">
<xs:sequence>
    <xs:element minOccurs="0" maxOccurs="unbounded" name="Items" nillable="true" type="tns:Item" />
</xs:sequence>
</xs:complexType>
<xs:element name="Items" nillable="true" type="tns:Items" />

but I don't see how I can make my Item class behave just like a string. I can't inherit from string and if I have a string member in Item then that is a different DTO structure to the one I need to have.

How should I structure my DTO class?

Thank you

Upvotes: 0

Views: 83

Answers (1)

mythz
mythz

Reputation: 143339

Try using a CollectionDataContract, e.g:

[CollectionDataContract(ItemName = "Item")]
public class ArrayOfItem : List<Item>
{
    public ArrayOfItem() { }
    public ArrayOfItem(IEnumerable<Item> collection) : base(collection) { }
    public ArrayOfItem(params Item[] collection) : base(collection) { }
}

Upvotes: 2

Related Questions