+ Start a Discussion
TheRealistTheRealist 

why extensions when we have custom controllers?

Hi,
we use standard controllers when we want to inherit the standard behaviour of those objects ,and custom controllers if we want to use our own logics on a VF page,but i wonder, why we need extensions along with custom controllers,can't we include all the logics within a custom controller itself instead of defining all the logics both in controller and extension.

Both controllers and extensions are apex classes,so what is the difference between them and how to know which of them is a controller or an extension when we look at apex classes,
 
Vigneshwaran GurunathanVigneshwaran Gurunathan
We can differentiate the Controller and extension through the Constructors.

The controller will have non parameterised constructor. The extenstion will have parameterised constructor with parameter of the Controller or Stand Controller which it extends.

For the usage part, its best practice to extend the custom controller than to implement more logic in it.

If page 1 needs 1 logic and page 2 needs 2 logics, instead of putting the second logic into controller, we can create a extension to controller and put the second logic there.

So page 1 will have controller only and page 2 will have controller and extension.

Hope it helps.
crazyforcercrazyforcer
For modularity and maintainability. 

If you need to implement different, unrelated logic for one or different pages, extensions with custom controllers are useful.