I am a newby of Apex code programming. Can someone please help me on
whether Apex code supports MVC architecture? If Yes, How we can write Models, Views and Controllers in Apex Code?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityFrom what I think, MVC in SFDC development roughly means:
View: Visualforce page
Model: SObject
Controller: Apex class
Referece: http://www.salesforcetutorial.com/model-view-controller-mvc/
Lance's answer is correct but it looks like things may still be unclear.
Model: sObjects. To view/create/edit these via the web interface:
Now, to access these via an Apex class you will call them by their API. By Convention the platform will add '__c' to the name of the object or field. If you enter an object 'Duck' with a text field 'Color,' your code to work with ducks could look like this:
public class DuckController{
public void createAndInsertGreenDuck(){
Duck__c thisDuck = new Duck__c(); //create a duck
thisDuck.Color__c='green'; //set the color
insert thisDuck; //insert the duck into the database
}
public List<Duck__c> getAllDucksByColor(String desiredColor){
//gets all the green ducks in the database (up to 10,000 per query)
List<Duck__c> duckList = [Select Id, Color__c FROM Duck__c WHERE Color__c == :desiredColor];
return duckList;
}
public void deleteAllGreenDucks(){
//use the earlier method to get the green ducks from the Database.
List<Duck__c> greenDucks = getAllDucksByColor('green');
delete greenDucks; //delete the Ducks
}
}
Hopefully that helps you understand how the Model(SObject) and the Controller(Apex CLass) interact with each other. Let me know if this is unclear.
.object
files in apex application right. can we treat them as models?