mardi 1 octobre 2019

Should DTOs represent nested entity strutctures, or should I have my pathing set up to have an endpoint for each nested object?

For example, lets say I have an entity that looks like this.

public class PersonEntity {
    public String firstName;
    public String lastName;
    public List<CarEntity> cars;
}

Option 1
GET /person/1

{
    "firstName": "Bob",
    "lastName": "Sagget,
    "cars": [
        (could be just IDs or the full Car DTOs)
    ]
}

Option 2
GET /person/1

{
    "firstName": "Bob",
    "lastName": "Sagget"
}

GET /person/1/cars

[
    {
        "make": "Honda",
        "model": "Accord",
        "year": 1992
    },
]

I feel like option 2 is more RESTful. But I also wonder about instances where you will need the full nested set of objects in every scenario. Should I still design it this way in that case?

Aucun commentaire:

Enregistrer un commentaire