【发布时间】:2019-08-15 08:51:22
【问题描述】:
我正在构建 OData v4 服务,我需要有关我的模型以及 OData 可以做什么和不能做什么的帮助。我也在使用 .NET Core 2.2 和 Entity Framework Core 和 ASP.NET Core。这是我第一个使用 .NET Core 的应用程序。
我有,此时此 POST 请求
[HttpPost]
[ODataRoute("Events({eventKey})/Bookings")]
public async Task<IActionResult> PostBooking([FromODataUri] Guid eventKey, [FromBody] Booking booking)
{
// ...
}
还有我的 POCO(EF 实体)
public class Booking
{
[Key]
public Guid Id { get; set; }
[Required]
public Event Event { get; set; }
[Required]
public User Student { get; set; }
[Required]
public int Position { get; set; } // Position in the registration queue
[Required]
public DateTime ReservationTime { get; set; }
public DateTime? CancelTime { get; set; } // null by default
}
在此 POCO 预订中,仅不需要 CancelTime。在 PostBooking() 期间,系统将设置所有必需的属性:
POST https://www.example.com/odata/Events(84a5c788-4f57-4983-b074-4a03a401484a)/Bookings
BODY
{
// In fact my body is empty because Position and ReservationTime I given by system. (now)
// Id is simply a new guid
// Event is in my oData link (84a5c788-4f57-4983-b074-4a03a401484a). I need to check it can be found
// Position is the number of booking register for this event + 1.
// UserId will be in my header (in a session token I will implement later with all security)
}
我看到了 3 个解决方案
1) 使用 OData 操作? 2) 我不需要在 JSON 中预订来自正文的对象。我可以这样写一个POST方法吗:
[HttpPost]
[ODataRoute("Events({eventKey})/Bookings")]
public async Task<IActionResult> PostBooking([FromODataUri] Guid eventKey)
{
// ...
}
3) 或者像这样
[HttpPost]
[ODataRoute("Events({eventKey})/Bookings")]
public async Task<IActionResult> PostBooking([FromODataUri] Guid eventKey, [FromBody] BookingPostActionDto booking)
{
// ...
}
在哪里
public class BookingPostActionDto() // Is a data tranfert object that I use only for API, not save in database
{
[Required]
public Guid RegistrationUserId { get; set; }
}
根据 OData 标准,这里有哪些可行的解决方案?我不是要最好的,而是要根据标准的有效的。例如,我什至不知道 OData 是否允许我像在我的解决方案 3 中那样使用 Dto 系统。如果我从不验证我的模型状态,我的解决方案 2 正在工作,因为我验证了我的模型状态,它将丢失所有必需的数据。当我可以执行 POST 并且 POST 似乎更合适时,我可以像解决方案 1 中那样创建操作吗?
【问题讨论】:
标签: c# asp.net-core entity-framework-core odata