I need some advice for my API-Design.As a example, think about the following backend implementation. There is a inheritance structure to define some more specific entities from one (abstract) common base.
Now I need to expose this model through a webapi. This should fullfill the following requirements:
- List all Persons (Base-Class Information)
- Get Details to one Person / specificType
- Create new Student/Person
The basic question is: Should I build up a DTO Structure and rely on the DTO-Type information or should i build in the type-Information in the Route?
These are the two apporaches:
Without specific routes:
The DTO structure could look like:
The API could look like:
api/persons/ [GET] -> PersonDTO GetAllPersons()api/persons/{id} -> PersonDetailsDTO GetDetails(id) api/persons/ [POST] -> CreatePerson(CreatePersonDTO) //Need to switch case the concrete type of the DTO>This relies on the type information of the dto to determine which concrete type needs to be created.
With specific routes:
The DTO structure needs no inheritance:
THE API could look like:
api/persons/ [GET] -> PersonDTO GetAllPersons()api/persons/students/{id} -> StudentDetailsDTO GetStudentDetails(id)api/persons/professors/{id} -> ProfessorDetailsDTO GetProfessorDetails(id)api/persons/students/ [post] -> CreateStudent(StudentDetailsDTO)api/persons/professors/ [post] -> CreateProfessor(CreateProfessorDTO)In my real implementation there are more than two inherited classes. But which approach would you suggest for what reasons?
Thanks a lot.Toni

