Gar*_*ely 8 c# asp.net wcf asp.net-web-api
我通过将现有的WCF服务转换为WebAPI来学习WebAPI(以及一般的REST).在这个过程中,我对处理非CRUD操作的最佳方式感到困惑.这是我的服务合同:
[ServiceContract]
public interface IProxyHelper
{
[OperationContract]
List<ProxyInfo> GetUsersCurrentUserCanActAsProxyFor(int positionId, int appId);
[OperationContract]
void DeleteProxy(int id);
[OperationContract]
List<ProxyInfo> GetProxyData(int appId);
[OperationContract]
bool CanPositionProxy(int positionId, int appId);
[OperationContract]
void AddProxy(
string userRacf,
string proxyAsRacf,
int userPositionId,
int proxyPositionId,
string requestedByRacf,
int appId);
[OperationContract]
int GetPositionIdByRacf(string racf);
[OperationContract]
string GetRacfByPositionId(int positionId);
}
Run Code Online (Sandbox Code Playgroud)
一些方法,如DeleteProxy和AddProxy I可以轻松转移到基于CRUD的方法.
问题出现在:
GetProxyData - 代理系统被多个应用程序使用,虽然我可以做api/Proxy/1,但我觉得这是"作弊",因为这应该是获取ProxyId 1,而不是应用程序1的代理.
GetUsersCurrentUserCanActAsProxyFor - 这个对我来说在多个层面上令人困惑.我该如何处理多个参数?并且它也没有完全落入CRUD方法.
这是否意味着我应该放弃WebAPI转换?如果没有,我该如何处理这些非标准方法?
我认为您将 RESTful 服务与 CRUD 混淆了。两者并不相同,尽管很明显将 CRUD 转换为 REST 非常简单(资源和动词都有明确的映射)。
RESTful 架构的最大区别在于它是面向资源的。第二个是您利用传输 (HTTP) 协议对这些资源进行操作 - 对于 REST,即 GET、POST、PUT 和 DELETE。
转向您的示例,您最大的麻烦似乎是决定用于支持此服务的 URI 方案。我可以建议,对于分层信息来说,这应该是简单的。例如,应用程序代理:
/application/<id>/proxies
用户当前用户可以充当以下代理:
/user/<id>/proxy-users或取决于您的风格/user/<id>/proxy/users
或者类似的东西。您会想到关系和基础资源。许多 URI 可以指向同一资源。
请注意,正如 @dtb 在他的评论中提到的那样,URI 和/或(不太优选)cookie 包含每个请求中的所有所需信息。所以CurrentUser有点黑客行为。
随着转换的进展,您可能还会发现以下有趣的阅读内容:RESTful 服务中的非 CRUD 操作
| 归档时间: |
|
| 查看次数: |
4247 次 |
| 最近记录: |