加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 05:46:10 所属栏目:Java 来源:网络整理
导读:如何命名存储库和服务接口及其实现类? 例如,我有一个名为Question的模型.您将命名存储库(接口和实现)和服务(接口/实现). 阅读这些帖子后: Java Interfaces/Implementation naming convention 和Interface naming in Java我重新考虑了我已经做过的事:) 解决
如何命名存储库和服务接口及其实现类?

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

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

解决方法

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

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

QuestionsRepository,TaxCalculatingService etc

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

Questions (or AllQuestions),TaxCalculator etc.

实现类将命名为SqlQuestions或InMemoryQuestions.

我尝试了两种,但我现在更喜欢第二种选择,因为它似乎更符合DDD思维模式.它似乎更具可读性,并具有更好的信噪比.以下是PhilCal?ado对great article库存的报价:

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…

整篇文章非常值得阅读和书签.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读