apex-enterprise-patterns / fflib-apex-common

Common Apex Library supporting Apex Enterprise Patterns and much more!
BSD 3-Clause "New" or "Revised" License
913 stars 517 forks source link

Declaration dependency order within Application.cls #219

Closed cropredyHelix closed 6 months ago

cropredyHelix commented 5 years ago

A minor point but worthy of an enhancement

If you define your Application class with Application.Domain before Application.Selector, when you attempt to use this construction:

fflib_ISObjectDomain domain = Application.Domain.newInstance(sObjIds);

such as in the ComplianceService example in the 2nd edition book Step 8 page 190, you will get a null pointer exception in inner class fflib_Application.DomainFactory, method newInstance(Set<Id> recordIds)

This is because the Domain factory depends on the Selector factory and as these are static variables, they are initialized in the order of appearance. If declared out of order, the SelectorFactory will be null for this use case.

As folks may populate the Application class from scratch; it is easy to overlook this declaration order dependency (ask me how I know!)

Suggestion: throw a more meaningful exception with a hint as to why this might be happening.