asp.net-web-api2 - Using a Web API to perform 2 DB operations
问题描述
I am doing my first tries with Web API and I have the following situation.
I need to perform 2 operations in a DB which must run inside a transaction:
1) Insert a record in one table (POST).
2) Update a record in another table (PUT).
Is it possible to do both in one call to a Web API? If so, what verb should I specify: POST, PUT or another one?
With respect,
Jorge Maldonado
解决方案
首先,您的 Web API 根本不应该直接调用您的数据库。Web API 应该调用一个服务层,然后调用您的数据库或调用 repos。
服务层可以根据需要对数据库进行尽可能多的调用,以完成任何业务规则。
HTTP 动词通常不是一个动作的硬性规则,但一般来说,POST 用于创建,PUT 用于更新。在这种情况下,如果您正在创建一条记录并更新另一条记录,我会默认使用 POST。
推荐阅读
- google-chrome - 如何通过参数将自定义标头设置为 chrome 应用程序
- php - 如何优化查询以更快地运行(查询中的查询)
- excel - 如何使用linest函数计算多项式趋势线
- c - 链接错误未定义符号:__aeabi_uldivmod
- angular - 违反 UNIQUE KEY 约束实体框架
- python - 如何在python中解析/操作字符串值?
- firebase - Firestore,数组与文档列表
- android - 在 rxjava 中,在 android studio kotlin 代码中出现错误
- python - 在 str 范围内的 pandas 数据框中使用 find
- php - 如何防止用户在php中下载视频