-
Notifications
You must be signed in to change notification settings - Fork 0
Best Practices
Abdelhakim Mouttaqui edited this page May 16, 2025
·
1 revision
Adhering to best practices when using the Apex Orbit Framework (AOF) will ensure your Salesforce application is robust, maintainable, scalable, and performs well.
-
Triggers: Keep them minimal. Only instantiate and run
AOF_TriggerHandler. -
AOF_TriggerHandler: Focus on orchestrating calls to the Domain layer (or Service layer for complex cross-object logic). Manage the Unit of Work commit. -
Domain Layer (
AOF_Application_Domainextensions): Place SObject-specific business logic, validations, and calculations here. Operate only on the records passed into the domain context. -
Selector Layer (
AOF_Application_Selectorextensions): All SOQL queries must go here. Ensure queries are selective, bulkified, and enforce FLS/CRUD (WITH SECURITY_ENFORCED). -
Service Layer (
AOF_Application_Serviceimplementations): Use for logic that spans multiple SObjects, interacts with external systems, or represents reusable business processes. Services can use multiple Selectors and coordinate Domain logic. -
Unit of Work (
AOF_Application_UnitOfWork): Register all DML operations through the UoW instance provided by theAOF_TriggerHandler. Avoid direct DML in Domain or Service classes.
- Always write your Domain, Service, and Selector methods to operate on collections (
List,Set,Map) of records, not single records. - Avoid SOQL queries or DML statements inside loops.
- Query only the fields you need. Don't use
SELECT *(which isn't valid SOQL anyway, but the principle applies to querying all fields unnecessarily). - Use efficient
WHEREclauses to filter data at the database level. - Leverage indexed fields in your
WHEREclauses where possible.
- Obtain the
AOF_Application_UnitOfWorkinstance from theAOF_TriggerHandler(it needs to be passed down or made accessible to Domain/Service layers). - Register records for DML (
registerNew,registerDirty,registerDeleted). - Let the
AOF_TriggerHandlercallcommitWork()at the end of the transaction. Avoid callingcommitWork()multiple times within a single transaction path unless absolutely necessary and well understood.
- Use
WITH SECURITY_ENFORCEDin all Selector queries. - Explicitly define sharing (
with sharing,without sharing,inherited sharing) for all Apex classes. - Validate user input, especially if it comes from client-side controllers or external systems.
- Use
try-catchblocks for operations that might fail. - Log all caught exceptions using
AOF_ErrorHandlerService.logError(). - Use
SObject.addError()for user-facing validation errors.
- Place reusable utility methods in appropriate helper classes or within the base AOF classes if broadly applicable.
- Design Service layer methods to be reusable across different parts of your application.
- Write comprehensive unit tests for all your AOF components (Domain, Selector, Service classes).
- Test bulk scenarios (e.g., processing 200 records).
- Test positive and negative scenarios, including error conditions.
- Verify DML operations by querying data after
Test.stopTest()and asserting results. - Test FLS/CRUD enforcement in Selectors by running tests as different users with varying permissions (using
System.runAs()).
- Be mindful of Salesforce governor limits (SOQL queries, DML statements, CPU time, heap size, etc.). AOF helps, but complex logic can still hit limits.
- Use asynchronous processing (Queueable, Batch, Future) via the Service layer for long-running or resource-intensive operations.
- Follow consistent naming conventions for your classes and methods (e.g.,
AOF_MyObjectDomain,AOF_MyObjectSelector). - Write clean, well-commented code that is easy for other developers to understand.
- Understand how to use the trigger bypass mechanisms (
AOF_TriggerHandler.bypass(),AOF_TriggerHandler.bypassAllTriggers()) for scenarios like data migrations or specific test setups. Use them judiciously.
- For configurable aspects of your logic (e.g., thresholds, feature flags, specific IDs), consider using Custom Settings or Custom Metadata Types instead of hardcoding values.
By following these best practices, you can leverage the full potential of the Apex Orbit Framework to build high-quality Salesforce applications.

Home
Quick Start
Usage Guide and Examples
Metadata‐Driven Trigger
Best Practices
Core Principles
Framework Architecture and Layers
Trigger Handler Layer
Service Layer
Domain Layer
Selector Layer
Unit of Work Layer
Error Handling Framework
Trigger Execution Flow
Scalability and Performance
Security Considerations
Core Components In-Depth
Error Handling In-Depth
Customization and Extension