gpt4 book ai didi

java - 如何命名存储库和服务接口(interface)?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:42:16 26 4
gpt4 key购买 nike

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

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

阅读这些帖子后: Java Interfaces/Implementation naming conventionInterface naming in Java我重新考虑了我已经做过的事情:)

最佳答案

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

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

QuestionsRepository, TaxCalculatingService etc

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

Questions (or AllQuestions), TaxCalculator etc.

实现类将被命名为 SqlQuestionsInMemoryQuestions

我都尝试过,但我现在更喜欢第二个选项,因为它似乎更符合 DDD 思维方式。它看起来更具可读性并且具有更好的信噪比。以下是来自 great article 的引述关于 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...

整篇文章非常值得阅读和 Collection 。

关于java - 如何命名存储库和服务接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9400387/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com