Best Practice for Returning a child data contracts as a Rest URL (REST API)

148 views Asked by At

I'm working on an API using WCF-RestAPI. I'm hitting a problem with our GETs are returning too much information contained in child entities (data contracts). We have decided to instead, return a URL which should be accessed to get the child entity.

So for example;

{
  "date": "2014-12-01T00:00:00Z",
  "contractor": {
    "contractorReference": "DEFREF",
    "contractorName": "Default Supplier",
    "mainTelephone": "123456789",
    "mainAddress": {
      "fullAddress": "Default Supplier Street DefaultTown United Kingdom"
    },
    "mainFax": null,
    "webAddress": null,
    "comment": null
  },
  "moreinfo": "data"
}

would become something like

{
  "date": "2014-12-01T00:00:00Z",
  "contractor": "https://rest-api/contractor/{id}",
  "moreinfo": "data"
}

Is there anything built into REST API or a standard way of doing this? I'm considering creating an attribute on the data contract possibly named "IsLinkable" and picking this up on an action filter on serialization. Not sure this is the best solution though.

1

There are 1 answers

0
Shorttylad On BEST ANSWER

I didnt find anything built into the Framework that would do this out of box.. I got round the issue by replacing the child data contracts with string properties for holding the URLs. These would then be built up using reflection and attributes on the data contract themselves.

i.e. I had a property holding the child entity's unique identifier (e.g. UserId) which I specified in the attribute along with the route.. (e.g. Users/{id}) . I then set up an action filter which would execute just before returning the populated data contract to the user, this would build the URLs using the method mentioned above.