Searchcriteria – Use With Caution!
SearchCriteria pattern in service layer find methods is problematic because it leads to a lot of code duplication. Even if we consign the pattern to the building-block services, all the higher level services interacting with building-block services will have to create their own set of SearchCriteria objects. These SearchCriteria objects will be duplicated time and time again.
A better solution is to create actual parameterized method signatures on the building-block layer. At least this way all the queries will be tightly locked down in this domain-service/building-block service layer instead of spread up all over the code base.
SearchCriteria is convenient from the point of view of the developer creating building-block services, but from a user of the API point of view and architecture it’s a disaster. Although SearchCriteria pattern reduces the proliferation of find methods in APIs, it produces lots of code duplication. The only place for the SearchCriteria pattern is when there is a need for creating dynamic searches from the UI.
Using the SearchCriteria, service APIs can be reduced to the following:
Instead of this, where there might be tens of find methods: