1

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.

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.

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.
  • 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.

Any thoughts?

1 Answer 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.

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.

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.