REST API和延迟加载对象

时间:2023-01-15 22:51:50

I have come across this answer which makes reference to Hypermedia. My question is similar.

我遇到过这个引用超媒体的答案。我的问题很相似。

I am writing a REST API that will be predominantly consumed by mobile devices. For this reason I want to try and keep a handle on efficient network I/O.

我正在编写一个主要由移动设备使用的REST API。出于这个原因,我想尝试保持高效的网络I / O。

Say for example I have a Book object that contains a TableOfContents (collection) property. In my app I want to display a list of all books to the user. Then when a user selects a book I want to show them the table of contents.

比方说,我有一个包含TableOfContents(集合)属性的Book对象。在我的应用程序中,我想向用户显示所有书籍的列表。然后,当用户选择一本书时,我想向他们展示目录。

Now, when requesting the list of books I am only interested in say, the Title and thumbnail, because nothing else is display in the list view item. I only want to pull down other items as the books are selected. This is because most probably, not all books will be selected. And so on..

现在,在请求我感兴趣的书籍列表时,标题和缩略图,因为列表视图项目中没有显示任何其他内容。我只想在选择书籍时下拉其他项目。这是因为很可能并非所有书籍都会被选中。等等..

Some possible solutions to a lazy loading approach:

延迟加载方法的一些可能解决方案:

  • Have two object types. BookLight and Book. BookLight only has title and thumbnail. Book has everything. This is not lazy loading and seems like a crap approach.
  • 有两种对象类型。 BookLight和Book。 BookLight只有标题和缩略图。书有一切。这不是延迟加载,看起来像垃圾方法。

  • One object which the Rest API will return all fields for, unless there is a query in the request which states which fields are required, and not to return any others. Then, when the object is selected and the full object is required a new query is made with the remaining fields. Alternatively, leave the query blank to refresh the entire object.
  • Rest API将返回所有字段的一个对象,除非请求中有查询说明需要哪些字段,而不返回任何其他字段。然后,当选择对象并且需要完整对象时,将使用其余字段进行新查询。或者,将查询留空以刷新整个对象。

Any thoughts?

1 个解决方案

#1


0  

I eventually discovered the most appropriate solution for this common problem.

我最终为这个常见问题找到了最合适的解决方案。

I use custom ViewModels and InputModels for each entity that gets communicated over my REST API. I will also be keeping the DTOs for each entity as these can be useful.

我为通过我的REST API传递的每个实体使用自定义ViewModels和InputModels。我还将为每个实体保留DTO,因为这些可能很有用。

Continuing with the Book example from the question. I would create a BookSummaryViewModel. This object would only contain items such as Title and Thumbnail etc. I could also add some other fields such as CountReviews. CountReviews works here because the ViewModel has the context of summarising the book. It would not fit into the BookDTO as that is strictly to represent the actual book.

继续问题中的Book示例。我会创建一个BookSummaryViewModel。此对象只包含标题和缩略图等项目。我还可以添加一些其他字段,如CountReviews。 CountReviews在这里工作,因为ViewModel具有汇总本书的上下文。它不适合BookDTO,因为它严格地代表实际的书。

I use AutoMapper for mapping to my ViewModels and it works great.

我使用AutoMapper映射到我的ViewModels,效果很好。

Thats an overview. For a great article hit this link.

这是一个概述。有一篇好文章点击此链接。

#1


0  

I eventually discovered the most appropriate solution for this common problem.

我最终为这个常见问题找到了最合适的解决方案。

I use custom ViewModels and InputModels for each entity that gets communicated over my REST API. I will also be keeping the DTOs for each entity as these can be useful.

我为通过我的REST API传递的每个实体使用自定义ViewModels和InputModels。我还将为每个实体保留DTO,因为这些可能很有用。

Continuing with the Book example from the question. I would create a BookSummaryViewModel. This object would only contain items such as Title and Thumbnail etc. I could also add some other fields such as CountReviews. CountReviews works here because the ViewModel has the context of summarising the book. It would not fit into the BookDTO as that is strictly to represent the actual book.

继续问题中的Book示例。我会创建一个BookSummaryViewModel。此对象只包含标题和缩略图等项目。我还可以添加一些其他字段,如CountReviews。 CountReviews在这里工作,因为ViewModel具有汇总本书的上下文。它不适合BookDTO,因为它严格地代表实际的书。

I use AutoMapper for mapping to my ViewModels and it works great.

我使用AutoMapper映射到我的ViewModels,效果很好。

Thats an overview. For a great article hit this link.

这是一个概述。有一篇好文章点击此链接。