Is it okay to bypass the repository pattern for complex queries?

前端 未结 3 663
无人及你
无人及你 2020-12-24 02:28

This is my understanding about DDD at the moment:

  • The strict repository pattern should only implement get(), delete() and create(), and maybe variants of get()
3条回答
  •  醉话见心
    2020-12-24 03:17

    First of all, queries are rarely done against Aggregate Roots. They are done against data and return just data. Repositories are very handy abstractions of persistence for using in application layer (commands and such) code. We need them there because we want to be able to test this layer without need for a database. That's why the smaller the repository the better -- it's easier to mock it.

    I tend to use specialized Finder objects that allow my UI to query the data store. I even place my Finders in the UI layer. The thing is, they tend to change every time UI changes so it's better to put them together. Another good reason why you don't want to put query methods on the repository is, repository is part of your domain, your ubiquitous language. You don't want to pollute them with UI concepts that tend to be short living and rapidly changing.

    I've written a blog post explaining this concept some time ago. You can find it here.

提交回复
热议问题