Search code examples
ormdomain-driven-designrepositoryddd-repositoriesdesign-by-contract

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?


Solution

  • 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();