Every business application has this screen somewhere: a table of records, filter boxes above it, and a user who may only see part of the data. The first query is a SELECT with a WHERE on the user's office. Filters arrive one at a time, then a role that sees two offices, then an auditor who sees everything once approved. Eventually the query is a two-hundred-line method nobody wants to touch.
Our argument is that this query was never a string. A search with optional filters and role-based visibility is application logic, and one of its invariants is a security boundary. Built by appending text, it gets the testability and review process of a text file. Built from small units that contribute fragments to a builder that enforces the rules, it gets treated like the rest of the code. The pattern is Strategy, used twice: one family of strategies decides what a user may see, the other what the user asked for, and neither writes the whole query.
The companion repository is search-query-compo
Discussion
Get the discussion rolling
A single comment can start something great.