function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Stuart WernerStuart Werner 

Using "model" classes for SOQL queries

We would like to use MVC design and would like to decouple our queries from our controller class. Can we create model classes to contain all of our queries and instatiate them in our cotroller classes. Our goal is to have a model class for each object and use them in multiple cotrollers. What is the best practive?
pconpcon
I would beg to say that Salesforce already had an MVC layout with Objects/VisualForce/Apex Controllers.  If your goal is to reduce the redundancy of your code, I would recommend creating Utility classes for your objects and reusing them in your controllers.  For example:

CaseUtils.cls
public class CaseUtils {
     public static List<Case> getCasesForAccount(Id accountId) {
          return [
               select AccountId,
                    CaseNumber,
                    ContactId,
                    Subject
               from Case
               where AccountId = :accountId
          ];
     }
}

Then reuse this code in your controllers.