I am designing a REST service that could be used by many types of clients, most likely .Net, PHP, Flex and JavaScript. I am building the service using WCF and the REST starter kit. One of my main goals is to make it as simple as possible for all those clients to use the API.
Let's say that the API deals with zoos. When the client creates a new zoo it would be nice if they could pass along the initial set of animals so they only need to make a single call to the API, e.g.
<Zoo>
<Name>My Zoo</Name>
<Animals>
<Snake>
<Name>Frank</Name>
<Length>2.5m</Name>
</Snake>
<Giraffe>
<Name>Alfred</Name>
<Height>10m</Height>
</Giraffe>
</Animals>
</Zoo>
I then want to deserialize the XML into C# classes like this:
List<Animal> Animals { get; set; }
class Animal { public string Name { get; set; } }
class Snake : Animal { public float Length { get; set; } }
class Giraffe : Animal { public float Height { get; set; } }
WCF doesn't like this as it wants the XML formatted like this:
<Zoo xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Name>My Zoo</Name>
<Animals>
<Animal i:type="Snake">
<Name>Frank</Name>
<Length>2.5m</Name>
</Animal>
<Animal i:type="Giraffe">
<Name>Alfred</Name>
<Height>10m</Height>
</Animal>
</Animals>
</Zoo>
This looks like it is going to be trickier to work with in client without high end tooling support which raises lots of questions:
- Is there a better design for this API?
- Is the second format going to be a problem for non-WCF clients?
- Can I make WCF format this data the first way?
- Should I use these types of hierachies in the XML at all?