Considering advice given for DAO objects or Service pattern from What is a good set of coding conventions for Salesforce development?, I'm wondering how can they be made generic enough?
Some generic DAOs from Java will solve that with an interface that has
search(ISearch) and so on. The themes are that:
- the object is always returned in its entirety
- filters can be passed to search interfaces which is passed to the DAO to limit the wanted records
Now, consider the following code. It is a one-off selection to get some settings.
List<Foo_Settings__c> fooSettings = [ Select Id, Name from Foo_Settings__c where Status__c = 'Active' ];
Just some fields are selected. I'm worried that reproducing the Service pattern from Java might have a negative impact on:
- governor limits (I sometimes need only a couple of fields)
- flexibility (it is not clear in advance which fields and conditions will be needed. It would be great to have a generic DAO from which other classes would inherit)
- security (I imagine a lot of dynamic SOQL would have to be used)
for loops to balance heap consumption (http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_VLSQ.htm):
for (Account a : [SELECT Id, Name FROM Account WHERE Name LIKE 'Acme%'])
How can a flexible and generic DAO class be implemented with Apex?