如何命名存储库和服务接口?

发布于 2025-01-08 02:57:11 字数 349 浏览 1 评论 0原文

如何命名存储库和服务接口及其实现类?

例如,我有一个名为 Question 的模型。您将如何命名存储库(接口和实现)和服务(接口/实现)。

阅读这些帖子后: Java 接口/实现命名约定Java 中的接口命名 我重新考虑了我已经做过的事情:)

How do you name repository and service interfaces and their implementing classes?

For example I have a model with the name Question. What would you name the repository (interface and implementation) and the service (interface/implementation).

After reading these posts:
Java Interfaces/Implementation naming convention
and Interface naming in Java I reconsidered what I already had done :)

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

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

发布评论

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

评论(2

可爱咩 2025-01-15 02:57:12

我个人使用 FooServiceFooServiceImplFooRepositoryFooRepositoryImpl

您可能会认为 Impl 后缀是噪音,但

  • 通常只有一个实现,因此没有 FirstFooServiceSecondFooService
  • 具体的 FooXxxImpl除了单元测试之外,代码中不会使用类型:注入依赖项,它们的类型是接口

I personally use FooService, FooServiceImpl, FooRepository and FooRepositoryImpl.

You might argue that the Impl suffix is noise, but

  • there's typically only one implementation, so there's no FirstFooService and SecondFooService
  • the concrete FooXxxImpl types are used nowhere in the code except in unit tests: dependencies are injected, and their type is the interface
绾颜 2025-01-15 02:57:11

我认为 DDD 中的命名方式大致有两种:

1)基于刻板印象。这是您在其名称中包含类构造型的地方。例如:

QuestionsRepository, TaxCalculatingService etc

2) 基于域。在这种方法中,您仅使用域语言并省略类名中的任何构造型。例如:

Questions (or AllQuestions), TaxCalculator etc.

实现类的命名类似于 SqlQuestionsInMemoryQuestions

我尝试了两种方法,但现在我更喜欢第二种选择,因为它似乎更符合 DDD 思维方式。它看起来更具可读性并且具有更好的信噪比。以下引用自 Phil Calçado 撰写的关于存储库的精彩文章

存储库作为对象列表的概念并不难理解,但这些类最终使用与列表根本无关的方法是很常见的。

在指导许多团队采用通用语言和相关模式后,我发现让人们记住存储库不是类似 DAO 的类的最佳方法是从如何命名它们开始。

几年前,Rodrigo Yoshima 告诉我他在命名存储库时的惯例。而不是下面显示的更常见的命名样式:

class OrderRepository {
   List<Order> getOrdersFor(Account a){...}
}

他提倡这一点:

class AllOrders {
   List<Order> belongingTo(Account a){...}
}

看起来只是一个小变化,但它有很大帮助......

整篇文章非常值得阅读和添加书签。

I think that there are roughly two approaches to naming in DDD:

1) Stereotype based. This is where you include class stereotype in its name. For example:

QuestionsRepository, TaxCalculatingService etc

2) Domain based. In this approach you use only domain language and omit any stereotypes in the class names. For example:

Questions (or AllQuestions), TaxCalculator etc.

Implementation classes would be named like SqlQuestions or InMemoryQuestions.

I tried both but I now prefer 2nd option as it seems to be more aligned with DDD mindset. It seems more readable and have a better signal-to-noise ratio. Following is a quote from a great article on repositories by Phil Calçado:

The concept of a Repository as a list of objects is not too hard to understand but it is very common for those classes to end up with methods that are not related to lists at all.

After coaching many teams in the adoption of a Ubiquitous Language and related patterns, I’ve found out that the best way to make people remember that Repositories are not DAO-like classes starts with how you name them.

Years ago Rodrigo Yoshima told me about his convention when naming Repositories. Instead of the more common naming style displayed below:

class OrderRepository {
   List<Order> getOrdersFor(Account a){...}
}

He promotes this:

class AllOrders {
   List<Order> belongingTo(Account a){...}
}

It looks like a small change but it helps a lot...

The whole article is well worth reading and bookmarking.

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