Note that there are some explanatory texts on larger screens.

plurals
  1. POClass hierarchies in a REST API
    text
    copied!<p>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.</p> <p>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.</p> <pre><code>&lt;Zoo&gt; &lt;Name&gt;My Zoo&lt;/Name&gt; &lt;Animals&gt; &lt;Snake&gt; &lt;Name&gt;Frank&lt;/Name&gt; &lt;Length&gt;2.5m&lt;/Name&gt; &lt;/Snake&gt; &lt;Giraffe&gt; &lt;Name&gt;Alfred&lt;/Name&gt; &lt;Height&gt;10m&lt;/Height&gt; &lt;/Giraffe&gt; &lt;/Animals&gt; &lt;/Zoo&gt; </code></pre> <p>I then want to deserialize the XML into C# classes like this:</p> <pre><code>List&lt;Animal&gt; 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; } } </code></pre> <p>WCF doesn't like this as it wants the XML formatted like this:</p> <pre><code>&lt;Zoo xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt; &lt;Name&gt;My Zoo&lt;/Name&gt; &lt;Animals&gt; &lt;Animal i:type="Snake"&gt; &lt;Name&gt;Frank&lt;/Name&gt; &lt;Length&gt;2.5m&lt;/Name&gt; &lt;/Animal&gt; &lt;Animal i:type="Giraffe"&gt; &lt;Name&gt;Alfred&lt;/Name&gt; &lt;Height&gt;10m&lt;/Height&gt; &lt;/Animal&gt; &lt;/Animals&gt; &lt;/Zoo&gt; </code></pre> <p>This looks like it is going to be trickier to work with in client without high end tooling support which raises lots of questions:</p> <ul> <li>Is there a better design for this API?</li> <li>Is the second format going to be a problem for non-WCF clients?</li> <li>Can I make WCF format this data the first way?</li> <li>Should I use these types of hierarchies in the XML at all?</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload