M
M
Mikhail Shatilov2018-11-05 03:07:17
PHP
Mikhail Shatilov, 2018-11-05 03:07:17

Is it good to use specs for queries?

I realized that in the repository to fence a bunch of methods for searching is not an option (I will immediately note that in my understanding the repository is not a collection). It remains either to use some kind of universal method (where the conditions will be scattered throughout the project) or ... specifications. I'll explain in more detail.
Initially, the design pattern involves checking objects for compliance with a condition.

Specification
Constructs a clear description of the business rules against which objects can be checked. The composite class specification has one method called isSatisfiedBy that returns true or false depending on whether the given object satisfies the specification.

designpatternsphp
I thought that it can be used not for checking already prepared samples, but for future ones. Simply put, classes are created that take predefined data and are passed to the find method in the repository.
This way we still keep the advantage of methods (all conditions in one place), but also have only 1 method in the repository.
I'm sure this could have been invented 30 years ago. I just want to make sure this is the way to go. It would also be interesting to know about its shortcomings.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
index0h, 2018-11-05
@iproger

You are trying to create your own DSL for requests from outside - this is very abstract garbage, while difficult to maintain and expand.
There is an alternative way: in the controller, from user input, create a dto that you send to the repository. At the same time, this very search dto will contain all possible conditions for the selection, createdAtFrom, createdAtTo, nameStartWith, ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question