如何命名存储库和服务接口?
如何命名存储库和服务接口及其实现类?
例如,我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我个人使用
FooService
、FooServiceImpl
、FooRepository
和FooRepositoryImpl
。您可能会认为
Impl
后缀是噪音,但FirstFooService
和SecondFooService
FooXxxImpl除了单元测试之外,代码中不会使用类型:注入依赖项,它们的类型是接口
I personally use
FooService
,FooServiceImpl
,FooRepository
andFooRepositoryImpl
.You might argue that the
Impl
suffix is noise, butFirstFooService
andSecondFooService
FooXxxImpl
types are used nowhere in the code except in unit tests: dependencies are injected, and their type is the interface我认为 DDD 中的命名方式大致有两种:
1)基于刻板印象。这是您在其名称中包含类构造型的地方。例如:
2) 基于域。在这种方法中,您仅使用域语言并省略类名中的任何构造型。例如:
实现类的命名类似于
SqlQuestions
或InMemoryQuestions
。我尝试了两种方法,但现在我更喜欢第二种选择,因为它似乎更符合 DDD 思维方式。它看起来更具可读性并且具有更好的信噪比。以下引用自 Phil Calçado 撰写的关于存储库的精彩文章:
整篇文章非常值得阅读和添加书签。
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:
2) Domain based. In this approach you use only domain language and omit any stereotypes in the class names. For example:
Implementation classes would be named like
SqlQuestions
orInMemoryQuestions
.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 whole article is well worth reading and bookmarking.