Craft Context

Crafting Context Objects. Parameter encapsulation for complex operations.

attac-t 713adc5 2 files · 4.1 KB Updated

File contents

Skill: Craft Context

"When an action needs too many parameters, the parameters become the object."

The Standard

  1. Private Constructor: Direct instantiation blocked. Forces use of named factories.
  2. Named Factories: ::forProvider(), ::forBatch(). Intent in the name.
  3. Self-Validating: Validate in constructor. Invalid context throws immediately.
  4. Immutable: readonly class. Set once, read many. No setters.

The Anti-Patterns

❌ Don't ✅ Do Why
5+ action parameters Context object Clarity, maintainability
Generic constructor Named constructors Intent is explicit
Mutable context Immutable properties Prevents bugs in long chains
Array of options Typed properties IDE support, validation

Real-World Examples

See examples.md.

attac-t/the-foundry/tree/main/plugins/laravel-ddd/skills/craft-context commit 713adc5034

Frequently asked questions

npx skillmds@latest add attac-t/craft-context