2

It appears most of the WebAPI examples are returning some models (either domain models or particular view models).

When using domain models, we actually request more data than needed in the view from ajax calls, and then build our view models using JavaScript (assuming we are building a web app).

I tried using different view models for each page (view), which allow me to reduce the network footprint and return only the fields in need. But in the ApiController I would have too many GET methods. And it is impossible for us to predict the future need and build an API returning all kinds of view models.

I would like to mimic the Facebook Graph API and build a uri like:

http://... api/games/333?fields=id, name, price, imageUrl

And our user should be able to update the record with only these few fields.

A detailed description can be found in a google code blog entry: Making APIs Faster: Introducing Partial Response and Partial Update.

Some other posts here suggest this is beyond the current ability of ASP.NET WebAPI. Will ServiceStack or some other package help us achieve the goal?

Community
  • 1
  • 1
Blaise
  • 21,314
  • 28
  • 108
  • 169

3 Answers3

2

Try this project: https://github.com/AnthonyCarl/ServiceStack.PartialResponse for the partial response side of the question

ServiceStack.PartialResponse.ServiceModel

Google Style Partial Responses for ServiceStack.Net. Currently only the following Content types are supported:

  • application/json
  • application/jsv
  • text/html
  • application/xml is NOT currently supported.

I wanted to implement this as a ServiceStack IPlugin, but I was unable to figure out how to get the access I needed to the response DTO for my approach. Currently, this is implemented as an IRequestContext extension.

Providing Field Selectors

Field Selectors can be passed using the header or query string. By default field selectors are combined form both. Duplicate field selectors are reduced. The field selector is applied to all entries in a list if the selector refers to a list.

Pauli Price
  • 4,187
  • 3
  • 34
  • 62
  • Thanks for the reply. But is this `PartialResponse` project a mature product? There is only 1 contributor and 51 commits at this moment. Is there any tutorial or sample code now? – Blaise Aug 26 '13 at 19:52
  • It's not my project, so I can't really give you a definitive answer. I suggest logging an issue on github with your questions. – Pauli Price Aug 26 '13 at 20:29
2

There are a couple of options to implementing partial updates in ServiceStack. See this question about implementing PATCH requests for an approach that uses a request DTO with nullable values, and the PopulateWithNonDefaultValues and similar extension methods in ServiceStack, to take a PATCH-style request where the client can send any subset of fields in the request body. If a given field is not present in the request body, then that property of your domain object will not be updated.

If you really need to use a query string to specify the subset of fields that should be updated, then you can still use the approach described above, but add some code that first nulls out any values in the incoming request DTO object that are not named in the query string. Then you can again use PopulateWithNonDefaultValues to copy the remaining values to the domain object.

Also, to comment on another part of your post that is closely related to the recommendations I just gave:

When using domain models, we actually request more data than needed in the view from ajax calls...

Here is where a message-based design is helpful: model your request/response messages as separate DTO classes, instead of reusing and exposing your internal domain model objects. Among over benefits, you'll eliminate the problem of exposing unnecessary fields in your request/response models. Message-based design is one of the core concepts that drives ServiceStack's implementation. You could, though, achieve similar results with Web API or MVC. I highly recommend reading this article that discusses how this design works in ServiceStack.

Community
  • 1
  • 1
Mike Mertsock
  • 11,825
  • 7
  • 42
  • 75
1

You can use OData Protocol,look this example. It's can use key:$select,$expand,$filter for search,select some fileds. Most important,the ASP.NET WEB API has a SDK for support this.

Zack Yang
  • 399
  • 2
  • 11
  • Thanks! That solution looks closest to what we need. Probably that is why WebAPI would not include the `PartialResponse` function. I tried to search for the WebAPI SDK you mentioned, but couldn't find any. Can you please post a link for us? But I did find a bunch of examples I can look into. [http://bit.ly/19WIUuE] Thank you for pointing me to a whole new direction. – Blaise Aug 27 '13 at 15:40
  • Glad to help you.The SDK you can get it on Nuget,search `oData`.This is the [tutorials](http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api) for OData,and their [source code](https://aspnetwebstack.codeplex.com/) at codeplex. I develop a site use OData,the [source](https://github.com/TossShinHwa/CMS) at github. – Zack Yang Aug 28 '13 at 03:20