at4dx icon indicating copy to clipboard operation
at4dx copied to clipboard

How to get a selector without sharing

Open wimvelzeboer opened this issue 2 years ago • 4 comments

What is the best way to have one selector implementation and be able to choose to have a with sharing and a without sharing selector, with the minimal amount of code duplication.

I currently have something like this, but how do I implement this in AT4DX:

public interface IContactsSelector extend fflib_ISObjectSelector
{
    List<Contact> selectById(Set<Id> ids);
 }
pubic virtual inherited sharing class ContactsSelector extends fflib_SObjectSelector implements IContactsSelector
{
   public static IContactsSelector newInstance()
   {
       return (IContactsSelector) Application.Selector.newInstance(Schema.Contact.SObjectType);
   }
   public static IContactsSelector newWithoutSharingInstance()
   {
       return (IContactsSelector) Application.WithoutSharingSelector.newInstance(Schema.Contact.SObjectType);
   }
   public static IContactsSelector newWithSharingInstance()
   {
       return (IContactsSelector) Application.WithSharingSelector.newInstance(Schema.Contact.SObjectType);
   }
   public ContactsSelector()
   {
       super();
   }
   public ContactsSelector(Boolean includeFieldSetFields, Boolean enforceCRUD, Boolean enforceFLS)
   {
       super(includeFieldSetFields, enforceCRUD, enforceFLS);
   }
   public List<Contact> selectById(Set<Id> ids);
   {
      ...
   }
   
   public with sharing class WithSharing extends ContactsSelector
   {
       public WithSharing()
       {
           super(true, true, true);
       }
       public override List<Contact> selectById(Set<Id> idSet)
       {
           return super.selectById(idSet);
       }
   }
   public with sharing class WithoutSharing extends ContactsSelector
   {
       public WithSharing()
       {
           super(true, false, false);
       }
       public override List<Contact> selectById(Set<Id> idSet)
       {
           return super.selectById(idSet);
       }
   }
}

wimvelzeboer avatar Oct 21 '21 11:10 wimvelzeboer