0

I have a doubt regarding Visualforce.

This is my scenario: I have 3 VF Componentes: A, B and C

Those components are shown as tabs of a parent VF Page, "Container". They share some heavy processing functions, and they also have some own functions.

My first approach was creating a virtual ExtensionClass for all of them, where I could store the main attributes and functions for all of the three VF Components. So basically it remains:

public virtual ExtensionClass{
    attribute1;

    heavyFoo(){
        // ...
    }
}

public class A extends ExtensionClass{ 
    A(){
        // define data using attribute1
    }
}
public class B extends ExtensionClass{ 
    B(){
        // define data using attribute1
    }
}
public class C extends ExtensionClass{
    C(){
        // define data using attribute1
    } 
}

But I realized that when "Container" is constructed, it creates 3 instances for "ExtensionClass", and so heavyFoo() run 3 times. So I thought about moving the common data to the virtual class, and pass the data through params like this

<!-- Container -->
<apex:page controller="ContainerCtr">
    <c:A _attribute="{!attribute1}" _resultOfHeavy="{!getHeavyFoo}" />

    <c:B _attribute="{!attribute1}" _resultOfHeavy="{!getHeavyFoo}" />
    
    <c:C _attribute="{!attribute1}" _resultOfHeavy="{!getHeavyFoo}" />
    
</apex:page>


public class ContainerCtr{
    attribute1{get;set;}

    heavyFoo(){
      // return heavy SOQL data
    }
}

BUT NOW, the problem is that because of the execution order of the constructors, the constructors of A,B and C cannot use the params.

Having this model, how can I achieve a good encapsulation and avoid running several times the same heavy logic?

4
  • You won't be able to use the constructor of the component for any of this but you can likely use the setter in the component that is paired with the assignTo action on the component's _resultOfHeavy attribute. You could implement some logic there but be mindful that the setter could be called multiple times and your code should account for that scenario.
    – Mark Pond
    Dec 10, 2020 at 0:34
  • Maybe I'm just dense but it seems you went a touch too far on your simplifications into pseudo-code for us to pinpoint your struggle.
    – Adrian Larson
    Dec 10, 2020 at 0:43
  • if you make heavyFoo() a lazy load, it will only be called once
    – cropredy
    Dec 10, 2020 at 0:43
  • I added an answer here long ago after playing with passing around references between VF and Components - it may be helpful, or another option for you. salesforce.stackexchange.com/q/9386/660
    – Mark Pond
    Dec 10, 2020 at 1:10

0

You must log in to answer this question.

Browse other questions tagged .