简体   繁体   English

单独的 DTO 用于发布和放置请求 & 嵌套 json 作为发布 model

[英]Seperate DTO for post and put request & nested json as post model

I have two questions about RESTful api.我有两个关于 RESTful api 的问题。

1. Can we have separate DTO for GET, POST and PUT request? 1.我们可以为 GET、POST 和 PUT 请求提供单独的 DTO 吗? Is it good practice是好习惯吗
or can I make one abstract class with common properties and then inherit from it?或者我可以制作一个具有公共属性的抽象 class 然后继承它吗?

2. Is sending nested json in POST request a good practice? 2.在 POST 请求中发送嵌套的 json 是一种好习惯吗?

As did in following article:正如以下文章中所做的那样:
https://code-maze.com/net-core-web-development-part6/ https://code-maze.com/net-core-web-development-part6/

  1. Yes, and more to the point, you should .是的,更重要的是,您应该. A DTO is a representation of a particular group of data in a particular scenario. DTO 是特定场景中特定数据组的表示。 That data can and will be different between different request types.该数据在不同的请求类型之间可以并且将会有所不同。 Any time there's different data being transferred, there should be a different DTO to represent it.任何时候传输不同的数据时,都应该有不同的 DTO 来表示它。

    You can utilize inheritance if you like and it makes sense.如果您愿意,可以使用 inheritance,这很有意义。 However, be aware that due to the way modelbinding works, you will still need to use your concrete, derived classes as params, not some base class.但是,请注意,由于模型绑定的工作方式,您仍然需要使用具体的派生类作为参数,而不是一些基本的 class。 The modelbinder will instantiate the class specified by the param (so it can't be abstract), bind any request values represented on that class, and discard the rest. modelbinder 将实例化参数指定的 class(因此它不能是抽象的),绑定class 上表示的任何请求值,并丢弃 rest。 So if you bind to BaseClass , all you will have is BaseClass , not DerivedClass , even if the request body was a representation of DerivedClass .因此,如果您绑定到BaseClass ,您将拥有的只是BaseClass ,而不是DerivedClass ,即使请求正文是DerivedClass的表示。 If you then attempted to downcast to DerivedClass , all the properties specific to DerivedClass would be null/default.如果您随后尝试向下转换为DerivedClass ,则特定于DerivedClass的所有属性都将为空/默认值。

  2. It is neither good nor bad practice.这既不是好的做法,也不是坏的做法。 JSON is an object representation format. JSON 是一种 object 表示格式。 If your object has nested objects, then your JSON would have nested JSON.如果您的 object 有嵌套对象,那么您的 JSON 将嵌套 JSON。

  1. Can we have separate DTO for GET, POST and PUT request?我们可以为 GET、POST 和 PUT 请求提供单独的 DTO 吗?

DTOs are not created based on the request type, instead DTOs are created for entities. DTO 不是根据请求类型创建的,而是为实体创建 DTO。 For example you have a Customer entity, for the Customer entity you will create a Customer DTO and the same Customer DTO can be used for GET, POST and PUT requests of Customer entity.例如,您有一个Customer实体,对于Customer实体,您将创建一个Customer DTO,并且相同的Customer DTO 可用于Customer实体的 GET、POST 和 PUT 请求。

Can I make one abstract class with common properties and then inherit from it?我可以制作一个具有公共属性的抽象 class 然后继承它吗?

Yes you can use inheritance while creating your DTOs but I would say thats over complication for no added value and keep in mind that inheritance, if not used properly can increase coupling in system.是的,您可以在创建 DTO 时使用 inheritance,但我会说这过于复杂而没有附加价值,请记住 inheritance,如果使用不当会增加系统中的耦合。

  1. Is sending nested json in POST request a good practice?在 POST 请求中发送嵌套的 json 是一种好习惯吗?

As Chris mentioned in his answer, if your object has a nested object then your JSON will have nested JSON.正如克里斯在他的回答中提到的那样,如果您的 object 有一个嵌套的 object 那么您的 JSON 将有嵌套的 Z0ECD18C1D7A287401DBB.8A23

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM