apex-enterprise-patterns / force-di

Generic DI library with support for Apex, Triggers, Visualforce and Lightning

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for Sub-Class

wimvelzeboer opened this issue · comments

I was wondering if there is also support for sub-classes in force-di, like in the example below:

public static IContactsSelector newWithoutSharingInstance()
{
    return (IContactsSelector) di_Injector.Org.getInstance(ContactsSelector.WithoutSharing.class));
}

I am able to execute the code but I am not able to create a Custom Metadata record for this in di_Binding__mdt, as a "." is not allowed in the Binding Name.

Or is there another way of doing this?

The main use case is that I have a (sub)package within the application that requires a selector in an elevated context. To avoid code duplication we added a sub class in the main selector class..

public virtual inherited sharing class ContactsSelector extends fflib_SObjectSelector implements IContactsSelector 
{
   public ContactsSelector()
   { 
	super();
   } 

   public ContactsSelector(Boolean includeFieldSetFields, Boolean enforceCRUD, Boolean enforceFLS)
   {
	super(includeFieldSetFields, enforceCRUD, enforceFLS);
   }
   ...
   public without sharing class WithoutSharing extends ContactsSelector
   {
	public WithoutSharing()
	{
		super( true, false, false 	);
	}

	public override List<Contact> selectById(Set<Id> idSet)
	{
		return super.selectById(idSet);
	}
	...
   }
}

@wimvelzeboer, normally the mapping to a selector is keyed off of the SobjectType. I assume that you want to keep this approach and have it map to the main selector class. With that in mind, in order to separate implementations, you could use a second interface selector type in a similar way to what is seen in AT4DX.

Bindings in Force-DI can be addressed by two criteria; the SobjectType and the Apex type. Those criteria can be used in conjunction to have implement various combinations. For example, in AT4DX, the Selectors are found in the bindings using two criteria; the IApplicationSObjectSelector and the SObjectType.

An example would be the AccountSelector in the AT4DX-Samplecode project. The newInstance() method calls the AT4DX Application.Selector Factory class' newInstance() method. It is right there where the Force-DI framework is called with two parameters: the SObjectType and the Apex type of IApplicationSObjectSelector.class interface.

You could easily setup an additional interface; call it ISObjectWithoutSharingSelector that would extend the primary fflib_ISObjectSelector interface. If you setup another series of Force-DI bindings, then you could access these selectors by executing the following:

(ISObjectWithoutSharingSelector) di_Injector.Org.getInstance( ISObjectWithoutSharingSelector.class, SObjectType );