Controllers and Extentions

Standard Controller – built-in controller – provides a lot of basic functionality like reading, writing, and creating new objects. It can handle both standard and custom objects. 
Standard List Controller (recordSetVar) – holds the objects for standard controller.

Custom controller - is an Apex class that implements all of the logic for a page without leveraging a standard controller. It is uses the default, no-argument constructor for the outer, top-level class. You cannot create a custom controller constructor that includes parameters. Use when page need to be run entirely in system mode, which does not enforce the permissions and field-level security of the current user.

A custom and standard controller cannot be referenced in the same page.

Controller extension is any Apex class containing a constructor that takes a single argument of type ApexPages.StandardController or CustomControllerName, where CustomControllerName is the name of a custom controller you want to extend.
------
Sharing setting is applied on standard object/extension by default; In case we don’t want to apply sharing setting in our code then Custom controller is only option.
It is possible that the functionality of page does not required any Standard object or may require more than one standard object, then in that case Custom controller is required.
*   Standard Controller – One Object, user mode.

*   Custom Controller – More than one Object and in System Mode (using with sharing keyword we can make it work in user mode)


  
If the controller extensions classes are defined then first the extensions will be searched for the methods in the order specified in the coma separated list in the page attribute ‘extensions’ and if not found it will be searched in the controller class.

If a method is in both controller and also in the extensions the method in the extension will get executed, and if the method is in multiple extensions then the method in the first extension in the coma separated list will be executed.
Example:
Page
<apex:page Controller="AccountController" extensions="ExtOne,ExtTwo" showHeader="false">
    <apex:outputText value="{!foo}"/>
    <br/>
     <apex:outputText value="{!poo}" />
</apex:page>
controller
public with sharing class AccountController {
    public String getPoo() {
        return 'poo controller';
    }
public String getFoo() {
        return 'foo-Controller';
    }
First Extension
public class ExtOne {
    public ExtOne(AccountController controller) {
    }
public String getPoo() {
        return 'poo One';
    }
    public String getFoo() {
        return 'foo-One';
    }
}
Second Extension   
public with sharing class ExtTwo {
    public ExtTwo(AccountController controller) {
    }
    public String getFoo() {
         return 'foo-Two';
            }
}

Output:
foo-One 
poo One 

Comments

Popular posts