1

I want to write the a selector class and have three version of this implementation:

  • one with sharing
  • one without sharing
  • and another one with inherited sharing

I got the design below but I really don't like having to declare the method again and the call the method version in the super class.

Does anybody know how can I achieve the same thing without having the write the methods again in the child classes?

public inherited sharing virtual class ExpensesSelector {

    public virtual List<Expense> selectAll() {
        return [SELECT Amount, TransactionDate FROM Expense];
    }

    public with sharing class WithSharingImplementation
            extends ExpensesSelector {
        public override List<Expense> selectAll() {
            return super.selectAll();
        }
    }

    public without sharing class WithoutSharingImplementation
            extends ExpensesSelector {
        public override List<Expense> selectAll() {
            return super.selectAll();
        }
    }
}

3
  • As shown in the linked question, don't specify a sharing model on the parent class, and probably make it abstract so it can't be directly instantiated, then you can use the child classes as you like without using override methods.
    – sfdcfox
    Mar 23, 2022 at 20:22
  • @sfdcfox I've tried that one before posting it and it doesn't work. The only way I've found to make it work was my example above. I've just created a version of my example with the design in the question you have recommended and it still doesn't work. Check it out here: gist.github.com/sauloefo/bdc7fa0500a6674977e7f8744ff7aac8
    – Saulo
    Mar 24, 2022 at 20:52
  • If it worth to mention, I'm using this selector from a AuraEnabled method declared in a classed defined as inherited sharing.
    – Saulo
    Mar 24, 2022 at 20:57

0

Browse other questions tagged .