【问题标题】:What is correct route with OData v4?OData v4 的正确路线是什么?
【发布时间】: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


    【解决方案1】:

    我取得了一些很好的经验,使用the recommended way。如果您想与 DTO 一起工作,this 可能会对您有所帮助。

    关于您的问题,我会采取标准的 ODataAction:

    [HttpPost]
    [ODataRoute("Events({eventKey})/Bookings")]
    public async Task<IActionResult> PostBooking([FromODataUri] Guid eventKey, [FromBody] Booking booking)
    {
        // ...
    }
    

    您的 JSON 有效负载是什么样的并不重要。如果您只需要发送RegistrationUserId,您可以随意发送:

    {
        "Student":{
            "Id:"1"
        }
    } 
    

    或者您存储 UserId 的任何位置...

    如果您不需要来自客户的额外数据,您也可以选择第二种方式。

    如果没有理由(例如性能问题或要隐藏的敏感信息),我不会使用 DTO。只要不需要额外的复杂性,整个 API 将保持简单。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      • 2016-10-31
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多