Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Web API 2 and partial updates

We are using ASP.NET Web API 2 and want to expose ability to partially edit some object in the following fashion:

HTTP PATCH /customers/1
{
  "firstName": "John",
  "lastName": null
}

... to set firstName to "John" and lastName to null.

HTTP PATCH /customers/1
{
  "firstName": "John"
}

... in order just to update firstName to "John" and do not touch lastName at all. Suppose we have a lot of properties that we want to update with such semantic.

This is quite convenient behavior that is exercised by OData for instance.

The problem is that default JSON serializer will just come up with null in both cases, so it's impossible to distinguish.

I'm looking for some way to annotate model with some kind of wrappers (with value and flag set/unset inside) that would allow to see this difference. Any existing solutions for this?

like image 819
Eugene D. Gubenkov Avatar asked Jul 05 '17 13:07

Eugene D. Gubenkov


People also ask

What is partial page updates?

Partial page updates are the result of an AJAX request that returns new HTML for portions of the page, most often the content of UpdatePanel controls.

What is ASP Net Web API 2?

ASP.NET Web API is a framework for building HTTP services that can be accessed from any client including browsers and mobile devices. It is an ideal platform for building RESTful applications on the . NET Framework. However it can also use to create non RESTful services.


1 Answers

I know that answers which are already given cover all aspects already, but just want to share concise summary of what we ended up doing and what seems to work for us pretty well.

Created a generic data contract

[DataContract]
public class RQFieldPatch<T>
{
    [DataMember(Name = "value")]
    public T Value { get; set; }
}

Created ad-hoc data cotnracts for patch requests

Sample is below.

[DataContract]
public class PatchSomethingRequest
{
    [DataMember(Name = "prop1")]
    public RQFieldPatch<EnumTypeHere> Prop1 { get; set; }

    [DataMember(Name = "prop2")]
    public RQFieldPatch<ComplexTypeContractHere> Prop2 { get; set; }

    [DataMember(Name = "prop3")]
    public RQFieldPatch<string> Prop3 { get; set; }

    [DataMember(Name = "prop4")]
    public RQFieldPatch<int> Prop4 { get; set; }

    [DataMember(Name = "prop5")]
    public RQFieldPatch<int?> Prop5 { get; set; }
}

Business Logic

Simple.

if (request.Prop1 != null)
{
    // update code for Prop1, the value is stored in request.Prop1.Value
}

Json format

Simple. Not that extensive as "JSON Patch" standard, but covers all our needs.

{
  "prop1": null, // will be skipped
  // "prop2": null // skipped props also skipped as they will get default (null) value
  "prop3": { "value": "test" } // value update requested
}

Properties

  • Simple contracts, simple logic
  • No serialization customization
  • Support for null values assignment
  • Covers any types: value, reference, complex custom types, whatever
like image 176
Eugene D. Gubenkov Avatar answered Sep 20 '22 10:09

Eugene D. Gubenkov