简体   繁体   English

我应该为同一个实体创建多个 DTO 吗?

[英]Should I create multiple DTOs for the same entity?

(Sorry my English is so bad). (对不起,我的英语太差了)。

The user entity in my spring boot project has the following basic fields: id, name, email, role, password.我的 spring 启动项目中的用户实体有以下基本字段:id、name、email、角色、密码。 Admin can create new employee (including password field), update employee (except for password field), view employee information (except for password field).管理员可以创建新员工(包括密码字段),更新员工(密码字段除外),查看员工信息(密码字段除外)。 Employees can edit their information(except for role field).员工可以编辑他们的信息(除了角色字段)。 If I create only one DTO, will it be used for all of the above actions?如果我只创建一个 DTO,它会用于上述所有操作吗? Should I create a DTO for each of those actions?我应该为每个操作创建一个 DTO 吗?

If I create only one DTO, will it be used for all of the above actions?如果我只创建一个 DTO,它会用于上述所有操作吗?

Not unless you write code to make that happen.除非您编写代码来实现这一点,否则不会。 You could have a single DTO for any number of actions if you wanted, but Spring does not impose that.如果您愿意,您可以为任意数量的操作设置一个 DTO,但 Spring 并没有强制执行此操作。 Creating a DTO will not make it be used for all of the actions.创建 DTO 不会使其用于所有操作。

Should I create a DTO for each of those actions?我应该为每个操作创建一个 DTO 吗?

Generally, no.一般来说,没有。

(I am french so please excuse my english if it is incorrect, I do my best). (我是法国人,所以如果我的英语不正确,请原谅我,我会尽力而为)。

I would create one DTO per "display" case.我会为每个“展示”案例创建一个 DTO。 But it is not an obligation.但这不是义务。

You can use Single DTO for all those operations and exclude that particular field which performing the operation.您可以对所有这些操作使用单个 DTO,并排除执行该操作的特定字段。 Like when Employee is editing their information then exclude the input of role property while saving/updating the record.就像员工在编辑他们的信息时一样,在保存/更新记录时排除角色属性的输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM