Find your content:

Search form

You are here

Generic Controller class?

 
Share

I have a custom Visualforce extension which accesses information stored in custom settings and exposes it as variables which can be referenced from Visualforce. I'd like to be able to use this extension from a variety of pages, including those with standard controllers and custom controllers. In other words, I want to be able to do all of the following:

<apex:page standardController="Account" extensions="SettingsExtension">
<apex:page controller="CustomController1" extensions="SettingsExtension">
<apex:page controller="CustomController2" extensions="SettingsExtension">

To do this, I've had to write the following constructors:

public SettingsExtension(ApexPages.StandardController stdController) {
    // code goes here
}

public SettingsExtension(CustomController1 controller) {
    // code goes here
}

public SettingsExtension(CustomController2 controller) {
    // code goes here
}

/* Etc. - each custom controller I use needs its own constructor */

Is there a generic Controller class or Interface I can use in the constructor to avoid this Constructor proliferation? I imagine it would look something like:

public CustomExtension(ApexPages.BaseController controller) {
    // code goes here
}

Attribution to: Benj

Possible Suggestion/Solution #1

You can move the utility code into a parent class and use the "extends" functionality to allow each controller to inherit the utilities:

public with sharing virtual class MyBaseClass {
    public Boolean checkIt(){
        return true;
    }
}

public with sharing virtual class MySecondClass extends MyBaseClass{
    Boolean isItTrue = checkIt();
}

Attribution to: Sergej Utko
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4947

My Block Status

My Block Content