Structured read-side pattern using Query Objects and Query Handlers for complex data retrieval with optional pagination support.
A structured read-side pattern for handling complex data retrieval using Query Objects and Query Handlers.
This pattern is intended for scenarios where data fetching becomes complex — such as multiple filters, sorting, searching, joins, aggregates, or conditional pagination.
It is conceptually similar to the transactional-action-pattern, but focused on reading data instead of modifying it.
Use this pattern when:
Avoid using this pattern for simple Model::all() type reads.
A simple DTO-like class that:
Example responsibilities:
Responsible for:
The handler performs execution logic, not validation or authorization.
All queries are stored under:
app/
└── Queries/
└── Tasks/
└── ListTasksQuery.php
└── ListTasksHandler.php
Group by domain/module inside `app/Queries`.
---
## 🏗 Example Structure
### ListTasksQuery.php
```php
class ListTasksQuery
{
public function __construct(
public ?string $search = null,
public ?string $status = null,
public ?string $sortBy = 'created_at',
public string $direction = 'desc',
public bool $paginated = false,
public int $perPage = 15,
) {}
}
class ListTasksHandler
{
public function handle(ListTasksQuery $query)
{
$builder = Task::query();
if ($query->search) {
$builder->where('title', 'like', "%{$query->search}%");
}
if ($query->status) {
$builder->where('status', $query->status);
}
$builder->orderBy($query->sortBy, $query->direction);
if ($query->paginated) {
return $builder->paginate($query->perPage);
}
return $builder->get();
}
}
Each handler must have:
| Pattern | Purpose | | ---------------------------- | ----------------------------------------------- | | Transactional Action Pattern | Handles write operations (create/update/delete) | | Query Handler Pattern | Handles complex read operations |
npx skills add techysavvyy/query-handler-pattern下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer