IN 和 OUT DTO 是否有任何命名约定?

Is there any naming convention for IN and OUT DTO?

如果我必须使用 MVC 架构创建某种 API,我将不得不为控制器接收的 DTO 和控制器 产生的 DTO 决定命名约定我说得对吗?

例如,给定以下代码:

    public class InStudentDTO
    {
        public int Id { get; set; }
        public List<int> Grades { get; set; }
    }

    public class OutStudentDTO
    {
        public int Id { get; set; }
        public bool HasApprovedCourse { get; set; }
    }

    [HttpPost]
    public OutStudentDto StudentHasApprovedCourse(InStudentDto dto)
    {
        OutStudentDto outStudentDto = _someService.CalculateStudentApprovedCourse(dto);
        return outStudentDto;
    }

这只是一个愚蠢的例子,但关键是我想在服务中使用 属性 List<int> Grades 执行一些计算,而不是稍后在控制器的输出中显示它。因此,据我所知,我应该只创建一个不公开 List<int> Grades 属性 的全新 DTO,对吧?如果是这样,这个“生成的 DTO”的正确命名约定是怎样的?还是应该将它们命名为 Viewmodels?

谢谢!

没有用于命名 DTO 类型的单一标准或命名约定,因为它是一个实现问题 - 我不知道 ASP.NET Web API 团队认可任何特定的约定(还有在官方 ASP.NET 文档中有很多使用实际 Entity Framework 实体类型作为 DTO 的错误示例(不要那样做 出于多种原因 - 除非你知道什么你在做)).

但是,我注意到 .NET 开发人员社区中的一个普遍趋势,即“In”DTO(如您所说)通常被命名为 ${ResourceName}Request,而“out”DTO 通常被命名为 output/response命名为 ${Resource/Action}Response - 将“Dto”作为类型名称后缀也很常见。

但是,在命名约定和编码风格方面,通常保持一致比“正确”更重要 - 所以如果您现有的项目使用Dto 作为后缀然后这样做,但是如果你的项目 没有 使用后缀那么不要开始使用后缀(没有充分的理由)。

此外,避免使用像 Id 这样模棱两可的名称 - 请改用全名 (StudentId)。

在我的主观意见中,以你的例子为例,我会这样命名它们:

    public class StudentCourseApprovalRequestDto
    {
        public int       StudentId { get; set; }
        public List<int> Grades    { get; set; }
    }

    public class StudentCourseApprovalResponseDto
    {
        public int  StudentId         { get; set; }
        public bool HasApprovedCourse { get; set; }
    }

    [HttpGet]
    public StudentCourseApprovalResponseDto StudentHasApprovedCourse( StudentCourseApprovalRequestDto req )
    {
        StudentCourseApprovalResponseDto resp  = _someService.CalculateStudentApprovedCourse( req );
        return resp;
    }