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 {
}
}
public inherited sharing abstract class ExpensesDataService {? – cropredy Mar 24 '22 at 21:48