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

前端之家收集整理的这篇文章主要介绍了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…

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

原文链接:https://www.f2er.com/java/121475.html

猜你在找的Java相关文章