在一个DTO文件中的几个DTO类

huangapple go评论56阅读模式
英文:

Few DTO classes inside one DTO file

问题

问题很简单:在一个文件中保留几个DTO是否是一种不好的做法?例如,我有一个项目,其中几个类似的DTO用于类型化:

export class CreateGenreDto {
  readonly nameRu: string;
  readonly nameEn: string;
}

export class GenreByIdDto {
  genreId: number;
}

export class UpdateGenreMessageDto extends GenreByIdDto {
  updateGenreDto: CreateGenreDto;
}

export class CreateGenreMessageDto {
  createGenreDto: CreateGenreDto;
}

对我来说,对于每个DTO都有四个独立文件似乎有点让人不舒服。

我没有找到任何相关信息,所以是否可以像这样使用项目结构,将所有类似的DTO存储在一个文件中:

/app
-/src
-/dto
--genres.dto.ts

感谢您的回复!

英文:

My question is simple: is it a bad practice to keep few DTOs inside one file? For example, I have a project, where few similar DTOs are used for typing:

export class CreateGenreDto {
  readonly nameRu: string;
  readonly nameEn: string;
}

export class GenreByIdDto {
  genreId: number;
}

export class UpdateGenreMessageDto extends GenreByIdDto {
  updateGenreDto: CreateGenreDto;
}

export class CreateGenreMessageDto {
  createGenreDto: CreateGenreDto;
}

For me it seems kinda annoying for eyes to have four individual files for each of DTOs.

I didn't found any information about it, so is it OK, to instead of this project structure:

/app
-/src
-/dto
--create-genre.dto.ts
--genre-by-id.dto.ts
--update-genre-message.dto.ts
--create-genre-message.dto.ts

use something like this, storing all similar DTO's in one file?

/app
-/src
-/dto
--genres.dto.ts

Thanks for your replies!

答案1

得分: 1

许多用户界面/用户体验专家都熟悉雅各布定律,该定律规定“用户在其他网站上花费了大部分时间”。

基于这一点,我会认为重要的不是你的看法,也不是我的看法。重要的是大多数开发人员习惯并期望的方式。标准做法是将它们分开存放在不同的文件中。这样做有其原因,这些原因中的一些或全部可能适用于您的项目,但最终重要的不是我们是否遵循了共识,而是我们习惯于什么...以及未来的您因此会期望什么...当浏览代码时。

所以也许你应该将它们放在一个genres子文件夹中,但它们“应该”是分开的文件。

尽管如此,运行良好的代码总比未完成的代码好,无论它有多完美。不要太担心这个问题。它无论如何都会正常运行!

(这是你没有要求的建议...考虑定义类型和接口而不是类。每当我使用类时,我都会想要添加方法,然后我会陷入困境。类型和接口帮助我避免这种情况,并且在运行时没有额外开销。)

英文:

Many experts in user interface / user experience are familiar with Jakob's Law, which states "Users spend most of their time on other sites."

Stealing from this, I would argue that it doesn't matter what you think, or what I think. What matters is what most developers are used to and will expect. The standard is to separate them into separate files. There are reasons for this, and some/all of these reasons might apply to your project, but ultimately whether it's collective wisdom or collective folly doesn't matter nearly as much as what we're used to... and what future-you will therefore learn to expect... when glancing through the code.

So maybe you should put them in a genres subfolder, but they "should" be separate files.

That said, working code beats unfinished code no matter how perfect it may be. Don't worry about it too much. It will run fine either way!

(Advice you didn't ask for... Consider defining types and interfaces instead of classes. Every time I use a class, I start wanting to add methods, and then I paint myself into corners. Types and interfaces help me avoid that, and are zero-overhead at runtime.)

huangapple
  • 本文由 发表于 2023年6月6日 11:49:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定