How to name repository and service interfaces?

前端 未结 2 1629
独厮守ぢ
独厮守ぢ 2021-02-20 17:53

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

2条回答
  •  执念已碎
    2021-02-20 18:20

    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:

    QuestionsRepository, TaxCalculatingService etc
    

    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.
    

    Implementation classes would be named like SqlQuestions or InMemoryQuestions.

    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.

    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 getOrdersFor(Account a){...}
    }
    

    He promotes this:

    class AllOrders {
       List belongingTo(Account a){...}
    }
    

    It looks like a small change but it helps a lot...

    The whole article is well worth reading and bookmarking.

提交回复
热议问题