If the properties of the data transfer object extend foreign keys or simply expose their primary keys

I have an EmployeeDTO that represents an Employee entry in a database. The Employee table is relevant to the Department and has a one-to-many relationship to Permission.

In my entities, they are represented as a fully extended property of the Department and a List of fully extended permission objects.

The question is, should the DTO fully extend the DepartmentDTO DepartmentId property? Should the DTO have a list of fully extended PermissionDTO properties of the PermissionId list?

+4
source share
2 answers

Like everything in design, it depends on your needs.

  • If you need to often see and bind to child properties, and you want to make it as easy as possible for developers to use your DTOs, you may want to use explicit factory methods to give you fully expanded child properties.
  • If you want simplicity of code, do not expand the properties of the foreign key and just let the developers get the child of the object / collection that they want by key if necessary.

There may be problems in recursion; Do you also extend all the foreign key properties of the Department object? What if there is a link to another EmployeeDTO in the department subclass?

Microsoft Entity Framework, as well as other popular business objects, process this concept with lazy loading - they only retrieve the full extended child property if it is called by code. This is probably the most flexible solution, but has a small overhead / delay, since child properties cannot be selected in the same database call as the parent. This, of course, is not a pure DTO.

+3
source

Yes and no. It depends on the call, and if you need all the additional properties in each call. It also depends on the ORM technology you use, which can do lazy loading and can affect your decision (if you are passing objects with a direct entity, although not recommended ).

Usually, one DTO case is created containing all the necessary properties and one or more DTO objects that reveal more functionality and use other methods. For example, I have a BasicUser class that contains only UserName and DisplayName , and I have a User that contains more, including Permissions and inherits from BasicUser.

+2
source

Source: https://habr.com/ru/post/1344775/


All Articles