Skip to content

Use Cases

Use Cases implement the Direct Mode architectural pattern, providing a simpler alternative to CQRS for operations without complex business logic.

When to Use

Use Cases suit simple read-only operations (homepage, dashboards), rapid prototyping, and features without domain events. For complex scenarios requiring transactions or events, prefer Bus Mode (CQRS).

Structure

A Use Case consists of three parts:

  • Request - Input data (UseCaseRequestInterface)
  • Use Case - Application logic (UseCaseInterface)
  • Response - Output data (UseCaseResponseInterface)
// Execute use case in controller
$response = $this->useCase->execute(new HomeRequest($userEmail));
$viewModel = $this->presenter->present($response)->getViewModel();

Implementation

final readonly class HomeUseCase implements UseCaseInterface
{
    public function execute(UseCaseRequestInterface $request): UseCaseResponseInterface
    {
        $greeting = $request->userEmail
            ? sprintf('Welcome, %s', $request->userEmail)
            : 'Welcome to CleanShelf';

        return new HomeResponse($greeting, $this->clock->now());
    }
}

Comparison with Bus Mode

Aspect Direct Mode Bus Mode
Events Manual dispatch Automatic
Middleware Not available Supported
Use case Simple reads Complex logic

See Also