POST和PUT的不同模型要求 [英] Different model requirements for POST and PUT

查看:101
本文介绍了POST和PUT的不同模型要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个控制器 CatController ,它具有GET,POST和PUT的操作。他们都使用相同的 Cat 资源,看起来像这样:

Say I have a controller CatController with actions for GET, POST and PUT. They all use the same Cat resource which could look like this:

public class CatDto {
  public int Id { get; set; }

  [Required]
  public string Name { get; set; }

  [Required]
  public bool IsFriendly {get; set; }
}

但是,名称 IsFriendly 属性仅在创建新猫(POST)时才是必需的,而在更新它(PUT)以允许仅更新单个属性时是可选的。

However, the Name and IsFriendly properties should only be required when creating a new cat (POST), but optional when updating it (PUT) to allow updating only a single property.

到目前为止,我处理此问题的方式只是拥有两个类,分别是 CreateCat UpdateCat 具有相同的属性,但数据注释不同。但是,我不想维护两个几乎相同的类。

The way I've handled this until now is simply having two classes, a CreateCat and UpdateCat which have the same properties but different data annotations. However I don't want to have to maintain two almost identical classes.

我当然可以在每个操作中手动验证模型,但是数据注释对于诸如全局模型验证器和自动生成Swagger模式之类的事情非常有用。

I could of course validate the model manually in each action, but data annotations are very useful for things like global model validators and automatic generation of Swagger schemas.

我还使用Swagger模式自动生成SDK(使用 ApiMatic ),这会导致生成两个重复的类( CreateCat UpdateCat ),而这些类实际上只能是一个资源( Cat )。

I'm also using the Swagger schema to automatically generate SDK's (using ApiMatic), and that results in having two duplicate classes generated (CreateCat and UpdateCat) for what really should only be a single resource (Cat).

是否有另一种方法可以实现我仅尝试使用的功能

Is there an alternative way to achieve what I'm trying to do with only a single class?

推荐答案

老实说,我更喜欢保留单独的模型。
您可以有一个具有所有公共属性的基本抽象(或没有)模型,尽管这不是必需的,只需添加第三类即可。有需要吗?我会说不。

I prefer to keep separate models to be honest. You could have a base abstract ( or not ) model with all the common properties although this is not required and simply adds a third class. Is there a need for it? I'd say no.

POST和PUT之间存在细微差别。如果您已经在PUT端点中拥有ID属性,则POST和PUT都不需要Id属性。这样就不需要检查URL中的ID是否与模型中的ID相匹配。

There are slight differences between POST and PUT.Neither POST nor PUT require the Id property if you already have that in the PUT endpoint. This negates the need of checking if that Id in URL matches the Id in the model.

您的示例不会显示出差异,但是在许多情况下,您需要输入一些字段真的不想更新。例如,假设您有一个创建日期和更新日期字段,例如,您不想通过PUT更改创建日期。不想通过PUT更新的数据越多,模型之间的差异就越明显,也就越有价值。

Your example does not make the difference visible, but in many cases there are fields you don't really want to update. For example let's say you have a Created and Updated date fields, you would not want to change your Created date via a PUT for example. The more data you have that you do not want to update via a PUT, the more obvious and worthwhile the differences between the models become.

在您的情况下,即使拥有这两个属性,我仍然会创建2个不同的模型,即使它们实际上是相同的,也可以期望API的工作方式和创建方式在其他所有人的头脑中进行清晰的设计。

In your case even with those 2 properties I would still create 2 different models, even if they are virtually the same, this sets the expectation on how the API works and creates a clear design in the mind of everyone else who is working on it.

这篇关于POST和PUT的不同模型要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆