Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4802

Web-API and polymorphic DTOs

$
0
0

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.

Entity Structure

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:

DTO with inheritance

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:DTO without 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


Viewing all articles
Browse latest Browse all 4802

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>