Skip to content

Instantly share code, notes, and snippets.

@sauloefo
Created March 24, 2022 20:45
Show Gist options
  • Save sauloefo/bdc7fa0500a6674977e7f8744ff7aac8 to your computer and use it in GitHub Desktop.
Save sauloefo/bdc7fa0500a6674977e7f8744ff7aac8 to your computer and use it in GitHub Desktop.
public class ExpensesSelector {
public static Map<Id, Expense> selectAll() {
return dataService.selectAll();
}
private static ExpensesDataService dataService {
get {
if (null == dataService) {
// use with sharing by default
dataService = new WithSharing();
}
return dataService;
}
set;
}
public static void switchSharing(Boolean useSharing) {
// this method is used to switch sharing model
if (useSharing) {
dataService = new WithSharing();
} else {
dataService = new WithoutSharing();
}
}
private abstract class ExpensesDataService {
public Map<Id, Expense> selectAll() {
return new Map<Id, Expense>([SELECT Amount, TransactionDate FROM Expense]);
}
}
private with sharing class WithSharing
extends ExpensesDataService {
}
private without sharing class WithoutSharing
extends ExpensesDataService {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment