Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD: the Repository contract

I've read in various places that one important requirement in DDD is to have a bounded contract for the Repository:

findByName(string name)
findByEmail(string email)
etc.

And not provide a generic query interface:

findBySpecification(Specification spec)

I do understand why this is important: to be able to mock the Repository for tests, or change the underlying persistence framework.

While this rule is not that hard to enforce throughout the application, I can't figure out how to enforce it when it comes to provide the user with an "advanced search" form.

Let's say I have a form which allows to search blog posts by keyword, by date, by author, etc.

These criteria being freely combinable, I obviously can't provide a method for each use case:

findByKeyword(string keyword)
findByDateRange(Date from, Date to)
findByKeywordAndDateRange(string keyword, Date from, Date to)
findByDateRangeAndAuthor(Date from, Date to, User author)
etc.

Am I missing something or is it one of the exceptions to the rule?

like image 496
BenMorel Avatar asked Oct 11 '22 02:10

BenMorel


2 Answers

There is nothing wrong with passing Specification as a parameter to a repository. This is actually a very good way of tackling method explosion on the repository interface. Take a look at this answer. 'Filter' maybe more appropriate name than 'Specification' for the 'advanced search' scenario. I think this code will not violate any DDD guidelines:

Filter filter = new FilterBuilder()
    .WithinDateRange(dateRange)
    .IncludingKeywords("politics", "news")
    .ByAuthor("John Smith")
    .Build();

blogs.FindByFilter(filter);

Note that the code that creates the filter can live outside of domain. Because it will not violate any domain rules. What if there is a rule like "Blogs posted by Anonymous author should be approved by moderator"? Although it can be expressed with Filter, doing so will externalize piece of business logic. It will make more sense to put this rule into domain code and have a dedicated repository method like:

blogs.RequireModeratorAttention();
like image 129
Dmitry Avatar answered Oct 14 '22 02:10

Dmitry


While this rule is not that hard to enforce throughout the application, I can't figure out how to enforce it when it comes to provide the user with an "advanced search" form.

Actually, you don't have to pay the cost of all these abstractions if all you need is a search form. Repository (in context of DDD at least) was designed to abstract the nuances of persistence framework from the business logic (application layer).

If you have a command that changes user's address, it's better to have a repository with FindUserById method than to have some magic Hibernate code in the app layer. There are two reasons for this

  • You may want to test the application layer with persistence layer (repositories) mocked
  • Application layer is something you care about because it's business logic

You don't need all of these to just get some data for the UI. I would suggest using specialized 'Finder' classes that may even live in the UI layer. They can operate either on abstract 'specification' or (even better) on bare Hibernate (or your favourite ORM). I've written a blog post about this approach here.

like image 35
Szymon Pobiega Avatar answered Oct 14 '22 01:10

Szymon Pobiega