简体   繁体   English

组织Master-Detail控制器ASP.Net Web API

[英]Organizing Master-Detail Controllers ASP.Net Web API

I am trying to determine the best way to implement the retrieval of detail records based upon the master's ID. 我正在尝试根据主人的ID确定实现详细记录检索的最佳方法。

Obviously you would set up API controllers for both master and detail. 显然,您将为master和detail设置API控制器。

Solutions I've considered: 我考虑过的解决方案:

  • Have API consumers use OData to get all details filtered by a master ID. 让API使用者使用OData来获取由主ID过滤的所有详细信息。 While I don't have an issue with this solution, I kinda feel bad putting that onto the API consumer and feel it is something that should be handled internally by the API 虽然我对这个解决方案没有任何问题,但我觉得把它放到API使用者身上并感觉它应该由API内部处理
  • Go against the convention of just having the Get/Put/Post/Delete methods and create an action of "GetMastersDetails" on the detail controller and make it accessible via routing. 违反只有Get / Put / Post / Delete方法的惯例,并在详细控制器上创建“GetMastersDetails”操作,并通过路由访问它。 While this would certainly work, I feel this gets away from the whole point of Web API (to an extent). 虽然这肯定会起作用,但我觉得这远离了Web API的整个过程(在某种程度上)。
  • Create a 3rd controller named "MastersDetailsController" which would have a Get based upon a master ID with different possible return types: 创建一个名为“MastersDetailsController”的第三个控制器,它将具有基于具有不同可能返回类型的主ID的Get:
    • Which would return a list of detail IDs which would then be used to call a Get on the details controller to get those actual details 这将返回一个详细ID列表,然后将其用于调用详细信息控制器上的Get以获取这些实际详细信息
    • Which would return a list of actual detail objects. 这将返回实际细节对象的列表。 What I don't like about that is having a controller returning a different type than what it is based upon. 我不喜欢的是控制器返回的类型不同于它所基于的类型。

I'd go with either #1 or #2. 我会选择#1或#2。

For #1, OData support enables not only the scenario you describe but offers a lot of additional functionality which might be desired in the future. 对于#1,OData支持不仅可以实现您描述的场景,还可以提供将来可能需要的许多附加功能。

For #2, I don't think it gets away from the point of Web API's. 对于#2,我认为它不会脱离Web API的角度。 Maybe a bit from a true RESTful service, but it's easy to implement and easy to understand. 也许有点来自真正的RESTful服务,但它易于实现且易于理解。

Option 2 will be fine. 选项2没问题。 Option 1 opens up a lot more risk depending on your scenario, and what you want to allow the user to get at. 选项1会根据您的方案以及您希望用户获得的内容开辟更多风险。

It's not really "against convention" to add custom methods to an ApiController. 向ApiController添加自定义方法并不是“违反惯例”。 You can do that however you like. 你可以随心所欲地做到这一点。 It would only be "against convention" if you did so and used the wrong HTTP methods (ie a GET when you're deleting something in your custom method). 如果您这样做并且使用了错误的HTTP方法(即在您的自定义方法中删除某些内容时为GET),它将只是“违反惯例”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM