Xml RESTAPI中的类层次结构
我正在设计一个REST服务,它可以被许多类型的客户端使用,最有可能是.Net、PHP、Flex和JavaScript。我正在使用WCF和REST初学者工具包构建服务。我的主要目标之一是使所有客户机使用API尽可能简单 假设API处理的是动物园。当客户端创建一个新的动物园时,如果他们可以传递最初的动物集,那么他们只需要对API进行一次调用就好了,例如Xml RESTAPI中的类层次结构,xml,wcf,api,rest,Xml,Wcf,Api,Rest,我正在设计一个REST服务,它可以被许多类型的客户端使用,最有可能是.Net、PHP、Flex和JavaScript。我正在使用WCF和REST初学者工具包构建服务。我的主要目标之一是使所有客户机使用API尽可能简单 假设API处理的是动物园。当客户端创建一个新的动物园时,如果他们可以传递最初的动物集,那么他们只需要对API进行一次调用就好了,例如 <Zoo> <Name>My Zoo</Name> <Animals>
<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>
我的动物园
直率的
2.5米
阿尔弗雷德
10米
然后我想将XML反序列化为C类,如下所示:
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; } }
<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>
列出动物{get;set;}
类{公共字符串名称{get;set;}}
蛇类:动物{public float Length{get;set;}
长颈鹿类:动物{公共漂浮高度{get;set;}
WCF不喜欢这样,因为它希望XML格式如下:
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; } }
<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>
我的动物园
直率的
2.5米
阿尔弗雷德
10米
这看起来在没有高端工具支持的情况下,在客户机中使用会更加棘手,这会引发很多问题:
- 这个API有更好的设计吗
- 对于非WCF客户端,第二种格式会有问题吗
- 我可以用第一种方式使WCF格式化此数据吗
- 我应该在XML中使用这些类型的层次结构吗
另外,不要忘记这一点。您的问题似乎是关于在REST中使用WFC和在REST中建模概念。我对你的模特问题有个想法: 这个API有更好的设计吗 引进一种新的资源来模拟动物的大规模进口怎么样?你可以称之为“装运” 装运可能被定义为将要引入动物园的新动物的清单。通过将运输作为一级资源公开,您将能够管理它们,跟踪哪种动物进入哪种运输(例如,跟踪疾病爆发),并使运输能够在任何时候进行,而不仅仅是在创建动物园期间
还有,别忘了。回答我自己的部分问题:
- 我可以用第一种方式使WCF格式化此数据吗
[XmlArrayItem(typeof(Animal))]
[XmlArrayItem(typeof(Snake))]
[XmlArrayItem(typeof(Giraffe))]
List<Animal> Animals { get; set; }
[XmlArrayItem(typeof(Animal))]
[XmlArrayItem(typeof(Snake))]
[XmlArrayItem(typeof(Giraffe))]
列出动物{get;set;}
回答我自己的部分问题:
- 我可以用第一种方式使WCF格式化此数据吗
[XmlArrayItem(typeof(Animal))]
[XmlArrayItem(typeof(Snake))]
[XmlArrayItem(typeof(Giraffe))]
List<Animal> Animals { get; set; }
[XmlArrayItem(typeof(Animal))]
[XmlArrayItem(typeof(Snake))]
[XmlArrayItem(typeof(Giraffe))]
列出动物{get;set;}
很酷,我会试试。。。但我应该吗?我问错人了。我将不再使用WCF进行RESTAPI。如果我是你,我会试试OpenRasta。它将引导您走上构建RESTful API的正确道路。你可以用WCF做一个RESTful API,但你会一路与之抗争。如果你想要第二个意见,请在堆栈溢出上查看这里标记为REST和WCF的问题。他们中的大多数人都没有回答。很酷,我会试试。。。但我应该吗?我问错人了。我将不再使用WCF进行RESTAPI。如果我是你,我会试试OpenRasta。它将引导您走上构建RESTful API的正确道路。你可以用WCF做一个RESTful API,但你会一路与之抗争。如果你想要第二个意见,请在堆栈溢出上查看这里标记为REST和WCF的问题。他们中的大多数人没有得到答复。