I have simple set of resources, not more than 10 types, with some one-to-many relationships. For example
- An order has many entries
- An entry has many comments
- A machine can have multiple entries associated
- A material can have multiple orders associated
All the model objects are identified by a unique Primary Key
I communicate using REST HTTP calls. How should the objects be modeled?
Option 1: Nesting objects
The relationships can result in nested objects:
entry{
PK: '_',
prop1: '_',
prop2: '_',
machine:{
PK: '_',
prop3: '_'
},
order: {
PK: '_',
material: {
PK: '_',
prop4: '_'
}
}
}
And similarly for the other objects
Option 2: Reference by PK
This approach "flattens" the objects:
entry{
PK: '_',
prop1: '_',
prop2: '_',
machinePK: '_',
orderPK: '_'
}
What is the best approach? obviously both have clear advantages and disadvantages. Is an hybrid approach a viable solution? If so, which criteria should I follow to chose between "flat" or "nested" objects? Could it possibly depend by the technology I use to implement that (Changing the model because of the technologies smells like bad practice though...)?
To give some context, I am using ASP.NET WebApi 2 for the backend and AngularJS in Typescript for the frontend