4

What are some use cases in having a custom controller with extensions? Does it make sense to have something like this?

EDIT2 Edited out the error I was receiving - apparently in my extension I was asking for a standardController in my constructor argument when in fact I was using a customController. I just used the class as the parameter instead

2
  • You might use an extension if you want the functionality to be reusable across different pages. One example I have used is a page count.
    – Girbot
    Commented Sep 7, 2014 at 15:02
  • @Girbot You should write this up as an answer...
    – sfdcfox
    Commented Sep 7, 2014 at 15:09

1 Answer 1

6

It is possible to have an extension for a custom controller.

Let's say my custom controller is this:

public with sharing class MyController {
...
}

My extension could then have a contructor that is called when this class is the controller for a page:

public with sharing class MyExtension {
...
  public MyExtension(MyController ctrlParam) {

  }
}

You could have the same extension work for both ApexPage.StandardController and some custom controller, but you'd need to overload the constructor:

public with sharing class MyExtension {
...
  public MyExtension(MyController ctrlParam) {

  }

  public MyExtension(StandardController ctrlParam) {

  }
}

If you want your extension to be reused for a lot of controllers, you might need to plan ahead and use inheritance either from a superclass or an interface so that you don't end up with too many constructors.

I suspect that most customer implementations do not get to this degree of sophistication. Where I would expect this model to crop up more often is in AppExchange app architecture.

1
  • Thank you! but I just found out what was wrong with it myself and you were spot on. As for your answer to my original question: Thank you for your response and input.
    – Kurusu
    Commented Sep 7, 2014 at 15:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .