Say we have a data grid of car models in our application. In the data grid, many fields were displayed, such as the brand, the model name and the year of production. However there are many more fields of these car model entities stored in the repository (database).
In another place of the application, there is a drop-down list which contains the same collection of car models, but only the model name and its color variants information are used (note the color variants information is not used in the data grid mentioned earlier).
Now what's the best practice to design the contracts?
- Have a
GetListAsync
method which returns paged result ofCarModelDto
s, which includes any field that can be found in theCarModelRepository
. Not a great idea obviously, because we could have many unrelated fields transferred to the frontend, some of which might even bring security vulnerabilities; - The
CarModelDto
only includes the fields that are used by either the data grid or the drop-down list (the union of them). This still introduces unwanted fields for both of them; - Have two
GetListAsync
methods in two different application services: one returns only fields that are concerned by the data grid, and the other returns only fields that are interested by the drop-down list. The downside is we have to write two sets of application services and contracts; plus it's not "future proof": if the frontend requests any fields that are not included in these DTOs, we have to make changes to the application services; - Same as 3, but add some "future proof" fields, i.e. the fields we anticipate that are possibly to be used in the frontend in the future. Now the question is, who gets to decide which "future proof" fields to include, based on what kind of rule?