Salesforce Decode
Salesforcedecode
Back to questions
ApexAdvanceddomaindddpatterns

Design Domain layer for business logic on SObject collections

Real World Scenario

Account business rules scattered between trigger, Flow, and LWC controller.

Expected Answer

• Accounts domain class constructed with List<Account> • onBeforeInsert/onAfterUpdate methods encapsulate rules • Trigger handler delegates to domain only • Unit test domain without trigger context • Domain enforces invariants before DML • Cross-object logic via service layer not domain cross-talk

Follow-Up Questions & Answers

Click to expand — each follow-up includes a direct, interview-ready answer

Main difference: use case and scale. Accounts domain class constructed with List<Account>. onBeforeInsert/onAfterUpdate methods encapsulate rules. Pick based on your integration pattern and team capability. Domain layer pays off when same object rules invoked from API, trigger, and batch. Optimize for scale and operational observability.

Architect Perspective

Domain layer pays off when same object rules invoked from API, trigger, and batch.