1

The answer in this question doesn't work so please don't close this question!!

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();
        }
    }
}

EDIT: The design below is a simplified version of the design accepted as correct answer in the question I mentioned before. An implementation of the same design, just changing the sObject, can be found in this gist.

public class ExpensesSelector {
    
    public abstract class ExpensesDataService {
        public List<Expense> selectAll() {
            return [SELECT Amount, TransactionDate FROM Expense];
        }
    }

    public with sharing class WithSharing
            extends ExpensesDataService {

    }

    public without sharing class WithoutSharing
            extends ExpensesDataService {

    }
}
4
  • what if you do this: public inherited sharing abstract class ExpensesDataService { ?
    – cropredy
    Mar 24, 2022 at 21:48
  • I tried and it also didn't work.
    – Saulo
    Mar 24, 2022 at 21:52
  • worth noting this repo that shows with and without sharing using fflib Selector pattern - which is what you're trying to avoid
    – cropredy
    Mar 24, 2022 at 21:56
  • The repo you refer to belongs to a friend of mine and we come up with with this working design: notice my design and the one in the repo duplicates the methods signatures calling the same methods in the super class. We do this in order to don't duplicate the method's body in two different places so to avoiding incur in DRY violation.
    – Saulo
    Mar 24, 2022 at 22:01

0

You must log in to answer this question.

Browse other questions tagged .