DTO to Json-API response in asp.net

85 views Asked by At

I'm currently working on an asp.net web api that is supposed to adhere to the json-api standard for its responses but I'm relatively new to REST APIs and not quite sure how to do that.

I have DTOs, which are subsets of our domain models, for example:

class EmployeeDto : IDto
{
   public Guid Id { get; }
   public string FirstName { get; }
   public string LastName { get; }
}

And the responses should look something like this:

{
  "data": {
    "type": "EmployeeDto",
    "id": "*SomeId*",
    "attributes": {
      "FirstName": "John"
      "LastName": "Doe"
    },
    "links": {
      "self": "*url to endpoint*"
    }
  }
}

Now, for the links part I took inspiration from this and right now I just have a links property on the base dto class that gets populated by the appropriate enricher.

The type, id and main data object I can also figure out.

But for the attributes I'm at abit of a loss. I thought about wrapping the DTOs in a response object by getting every property (except the id) through reflection and putting them into a dictionary named "attributes" but surely there is a better way?

0

There are 0 answers