如何命名存储库和服务接口? [英] How to name repository and service interfaces?

查看:161
本文介绍了如何命名存储库和服务接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

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

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

推荐答案

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

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

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

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

QuestionsRepository, TaxCalculatingService etc

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

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.

实现类的名称类似于 SqlQuestions InMemoryQuestions

Implementation classes would be named like SqlQuestions or InMemoryQuestions.

我试过了两个但我现在更喜欢第二个选项,因为它似乎更符合DDD心态。它看起来更具可读性,并且具有更好的信噪比。以下是来自PhilCalçado的关于存储库的精彩文章的引用:

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.

在指导许多团队采用无所不在的语言和相关模式之后,我发现了制作人的最好方法请记住,存储库不是类似DAO的类,而是从命名方式开始。

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.

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

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.

这篇关于如何命名存储库和服务接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆