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
Marty Y. ChangMarty Y. Chang 

Extend a Controller? Or Create a Controller Extension?

Does anyone have any thoughts or guiding philosophies on when to extend a controller and when to create a controller extension?

 

One example that I'm working on right now:

 

  • I have a customer portal for which I am creating a Visualforce tab to enable the customer to update his/her own contact record.
  • I have created a custom controller called CustomerPortalUserCtrl that uses SOQL to grab AccountId and ContactId for the current user.

 

 

My question:  For the Visualforce tab to edit the contact record, should I extend CustomerPortalUserCtrl or write an extension for it?

 

 

public class CustomerPortalUserContactCtrl
        extends CustomerPortalUserCtrl {
    public Contact contact { get; set; }
    
    private Boolean editing;
    public Boolean getIsEditing() {
        return editing;
    }
    
    public CustomerPortalUserContactCtrl() {
        super();
        
        List<Contact> contacts =
                [SELECT Id, FirstName, LastName,
                        HomePhone,
                        OtherPhone
                 FROM Contact WHERE Id = :user.ContactId];
        
        if (contacts.size() > 0) {
            contact = contacts.get(0);
        }
    }   // public CustomerPortalUserContactCtrl()
}

 

public class CustomerPortalUserContactExt {
    public Contact contact { get; set; }
    
    private Boolean editing;
    public Boolean getIsEditing() {
        return editing;
    }
    
    public CustomerPortalUserContactExt(CustomerPortalUserCtrl ctrl) {
        List<Contact> contacts =
                [SELECT Id, FirstName, LastName,
                        HomePhone,
                        OtherPhone
                 FROM Contact WHERE Id = :ctrl.user.Id];
        
        if (contacts.size() > 0) {
            contact = contacts.get(0);
        }
    }   // public CustomerPortalUserContactExt(CustomerPortalUserCtrl)
}

 

One of my thoughts is this:  Extend the controller if I need to access the controller members in my new page; Create a controller extension if the only reason I need the controller is to initialize some internal variables for the new page.

 

 

Note:  My goals are complicated by the fact that we deal with Person Accounts, so using an apex:detail component would not work.  An apex:detail component with subject="{!$User.ContactId}" would render an Account detail page, which is not editable.

Jon Mountjoy_Jon Mountjoy_

I love your thinking here Marty.  I think you're on the right track too - and don't believe there's only one answer here.

 

One question: what do you mean by "if I need to access the controller members in my new page". I can access extension stuff from a page too can't I?

 

I think one critical factor that you don't cover here is reuse.  If I put something in an extension, I could potentially reuse it across multiple controllers.  Perhaps even across multiple controllers of different types. That, for me, is a critical factor on "should I use an extension". 

Marty Y. ChangMarty Y. Chang

Thanks for the great insight about extension reusability and compatibility with multiple controllers.  That definitely makes sense, considering that a subclass would only "extend" a single controller, while an extension class could work with different controllers.

 

What i meant by "if I need to access..." is whether the controller has any properties that I want to display or whether it has any action methods I want to use.  But now that I think about it some more, if I don't need any of the controller properties or action methods, I probably don't need the controller at all.

 

It sounds like cross-controller compatibility is the best reason for creating an extension class.  I wonder what other philosophies are out there.

Jon Mountjoy_Jon Mountjoy_

Yep, certainly if you aren't using any of the controller functionality there's no point in extending it/using it at all.

 

Of course, that brings up another place to put your stuff - in a controller of your own (one that doesn't extend any other) - versus in an extension.   Again, I think reuse/modularisation would drive your choice.