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
Cindy NormanCindy Norman 

Splitting Source code into multiple files

What is the best way to split large Apex Source Code (for 1 controller) into separate source files?
Best Answer chosen by Cindy Norman
Leslie  KismartoniLeslie Kismartoni
I think every use case/corporate culture is a bit different, but I tend to do the following:
  • I create thin Controllers that only deal with getting variables to-and-from a VF page. These controllers reference one or more...
  • Manager classes which contain the specific business logic details and handle the saving. They rely on...
  • Data Access Classes which essentially wrap SOQL statements/queries for a particular SObject.
This seems to remove a lot of the clutter, keeps files compact/focused and helps create a nice separation of concerns. It also makes testing pretty easy...

To avoid an alphabet soup, since APEX doens't support folder in Eclipse, we prefix files with a naming convention:
  • ctr_myController: CTR for controller classes
  • mgr_accountManager: MGR for manager classes
  • dao_account: DAO for data access... 
Hope that helps?

 

All Answers

Leslie  KismartoniLeslie Kismartoni
I think every use case/corporate culture is a bit different, but I tend to do the following:
  • I create thin Controllers that only deal with getting variables to-and-from a VF page. These controllers reference one or more...
  • Manager classes which contain the specific business logic details and handle the saving. They rely on...
  • Data Access Classes which essentially wrap SOQL statements/queries for a particular SObject.
This seems to remove a lot of the clutter, keeps files compact/focused and helps create a nice separation of concerns. It also makes testing pretty easy...

To avoid an alphabet soup, since APEX doens't support folder in Eclipse, we prefix files with a naming convention:
  • ctr_myController: CTR for controller classes
  • mgr_accountManager: MGR for manager classes
  • dao_account: DAO for data access... 
Hope that helps?

 
This was selected as the best answer
Leslie  KismartoniLeslie Kismartoni
Here's a screen shot of a pretty large project... 
User-added image
Cindy NormanCindy Norman
Superb idea! Thank you.
Here's a silly question but i can't seem to find the syntax for including one file in another...maybe its because i come from the C++ world and am looking for the wrong thing.
 
Cindy NormanCindy Norman
BTW: What developer tool is that in your screen shot. That's another thing i'm struggling with...SF's developer tools are very dangerous and i'd like to find another way to do the development that is solid.
 
Leslie  KismartoniLeslie Kismartoni
That's just the eclipse editor... It works-ish... Not my favorite. Lately, I've just taken to using the Developer console, which has better support for auto completion and testing.

Regarding "includes" - so you're talking like .h file macros and includes... Yeah, that won't work here... APEX is much more like Java.
Just think of each class as publishing API's for use via the methods. Other classes just leverage those methods by calling them directly.

If this is still unclear, ping me back, maybe I can help via a better example... 
 
Cindy NormanCindy Norman
So if in ObjA in FileA i just reference the class name by creating an object (that is defined in ObjB in FileB), APEX will auto load that ObjB file/class? So i can split up everything according to my class definitions into separate files?
Leslie  KismartoniLeslie Kismartoni
Yup. Pretty much.
Cindy NormanCindy Norman
You are so awesome for helping a Newbie! Thank you!!!
 
Leslie  KismartoniLeslie Kismartoni
Cheers - good luck!