-
Hello! Salesforce provides a default query langue: SOQL. Developers can use it inline in Apex code. I wonder about the pros and cons of the Selector Layer in Apex. Considerations:
I realize, that much depends on Selector implementation. PS.: My questions are kind of critical, but I like Selectors, and see a lot of pros to using it. :) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This topic has been discussed many times -- both publicly and within countless engineering teams. The short answer to the lengthy discussion is that Selectors promote consistency. That is, without a centralized layer to access your data, inconsistencies can creep into your application because different conditions are used for filtering records. And, in the Salesforce case, you retrieve different "shapes" of records (different fields are selected) and find yourself with runtime bugs when fields are accessed without being explicitly queried for. Fowler's Enterprise Patterns book is a good read if you are looking for the non-Salesforce explanation: https://martinfowler.com/eaaCatalog/tableDataGateway.html To answer your bullet points in order:
|
Beta Was this translation helpful? Give feedback.
-
I'll add one additional (IMHO, huge) benefit: Selector classes enable you to mock their return values in unit tests. For example, the apexmocks framework is nicely folded into the AEP framework. The 3rd edition of Fawcett's book, chapter 12, gives examples
To @daveespo answer to #1, 100% agree. The computer is meant to be our servant and let it burn electrons rather than the developer burning neurons; optimizing only when needed. I love the consistency that every query is wrapped in a selector, even if initially it takes me a bit longer to set them up. |
Beta Was this translation helpful? Give feedback.
This topic has been discussed many times -- both publicly and within countless engineering teams.
The short answer to the lengthy discussion is that Selectors promote consistency. That is, without a centralized layer to access your data, inconsistencies can creep into your application because different conditions are used for filtering records. And, in the Salesforce case, you retrieve different "shapes" of records (different fields are selected) and find yourself with runtime bugs when fields are accessed without being explicitly queried for. Fowler's Enterprise Patterns book is a good read if you are looking for the non-Salesforce explanation: https://martinfowler.com/eaaCatalog/tableDa…