Search code examples
domain-driven-designrepository-patternlayerdtoddd-repositories

In which layer should I put a custom repository parameter


I am developing an application according to DDD. So I have my repositories' interfaces in the Domain layer.

My requirement is to make a searchByParams method. My question is : is it correct that this method will take a DTO as a param ? (because I read in this thread that DTO should be in the application layer)

This is an example to better illustrate my question :

I have a model object let say Person :

public class Person {
    private String firstName;
    private String lastName;
    private Date birthDate;
    private Date inscriptionDate;
    // Getters and setters ...
}

I want to get a list of persons by firstName, lastName, birthDateBefore, birthDateAfter, inscriptionDateBefore, inscriptionDateAfter.

What I thought to do is to make some sort of DTO containing theses fields

public class PersonDTO { // I can also name it PersonResearchFilters
    private String firstName;
    private String lastName;
    private Date birthDateBefore;
    private Date birthDateAfter;
    private Date inscriptionDateBefore;
    private Date inscriptionDateAfter;
    // Getters and setters ...
}

and pass it to : List<Person> searchByParams(PersonDTO filters)

But this implies that my DTO will be in the domain layer.

  • Is it OK ?
  • Is there a better way to design that requirement ?
  • Am I misunderstanding something ?

Solution

  • What you have is an object with no behaviour.

    When the application layer receives commands from clients and returns results to clients that object with no behaviour is referred to as a DTO (Data Transfer Object).

    Within a domain, you can also have an object with no behaviour. That would normally be classified as a Value Object. A Value Object can have behaviour but does not need to.

    The short answer to your question is to use a behaviour-less object declared in your domain to store parameter information for a repository query is fine.

    Alternatively, you may want to explore the "Specification Pattern" for filtered queries.

    https://gunnarpeipman.com/ef-core-query-specification/