在实体被认为臃肿之前,我们可以合理地在实体中放入多少种方法?

发布于 2024-12-03 07:52:15 字数 102 浏览 2 评论 0原文

通过所有有关单一责任原则、分解等的阅读,很难了解实体变得臃肿的警报信号应该是什么。

是否有一些好的建议/阅读某处关于我们应该考虑最多多少种方法,或者是否有一些客观的其他标准?

With all those readings about single responsibility principle, decomposition, etc., it's difficult to get an idea of what should be the alarm signal that an entity gets bloated.

Is there some good advice/reading somewhere about how many methods we should consider a maximum, or if there are some objective other criteria?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

口干舌燥 2024-12-10 07:52:15

在我看来,SOLID原则应该被视为指导方针,而不是严格的规则。 SOLID 违规通常是设计问题的可靠指标。但有时它们是不可避免的。

有相当多的代码指标。来自 NDepends

NbMethods:(为应用程序、程序集、命名空间、类型定义)
方法的数量。方法可以是抽象的、虚拟的或
非虚方法、接口中声明的方法、构造函数、
类构造函数、终结器、属性/索引器 getter 或 setter,
事件添加器或移除器。第三方程序集中声明的方法
不被考虑。

建议:NbMethods > 的类型20 可能很难
理解并维护,但在某些情况下可能是相关的
使 NbMethods 具有较高的价值。例如,
System.Windows.Forms.DataGridView第三方类有1000多个
方法。


20 是 NDepends 作者认为的合理阈值。以下是来自objecteering的另一个观点:

该值应保持在 3 到 7 之间。这表明
类有操作,但不是太多。大于 7 的值可能
表明需要进一步面向对象分解,或者
班级没有一致的目的。值为 2 或更小
表明这不是一个真正的类,而只是一个数据
施工。

正如您所看到的,这些指标非常主观。因此,您必须决定什么对您和您的团队有意义。从 DDD 的角度来看,我发现使用 值对象 是减少实体上 SRP 违规的一种非常好的方法。

In my opinion SOLID principles should be treated as guidelines, not as strict rules. SOLID violation is usually a reliable indicator of a design issue. But they are unavoidable sometimes.

There are quite a few code metrics. From NDepends:

NbMethods: (defined for application, assemblies, namespaces, types)
The number of methods. A method can be an abstract, virtual or
non-virtual method, a method declared in an interface, a constructor,
a class constructor, a finalizer, a property/indexer getter or setter,
an event adder or remover. Methods declared in third-party assemblies
are not taken account.

Recommendations: Types where NbMethods > 20 might be hard to
understand
and maintain but there might be cases where it is relevant
to have a high value for NbMethods. For example, the
System.Windows.Forms.DataGridView third-party class has more than 1000
methods.

20 is what NDepends authors considered to be a reasonable threshold. Here is another opinion from objecteering:

This value should remain between 3 and 7. This would indicate that a
class has operations, but not too many. A value greater than 7 may
indicate the need for further object-oriented decomposition, or that
the class does not have a coherent purpose. A value of 2 or less
indicates that this is not truly a class, but merely a data
construction.

As you can see these metrics are quite subjective. So you have to decide what makes sense for you and your team. From DDD perspective I found that using Value Objects is a very good approach to reduce SRP violations on Entities.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文