force-framework

back

Types

The Types API is used to instantiate an instance of an Apex Type.

The Type constructed must have a no-op constructor.

It supports a feature not available in the Apex Type.newInstance method. This new feature is the TypesV1.Factory interface. If the Type passed to the TypesV1.newInstance method is an instance of Factory, it will call the newInstance method of the Factory implementation to create the instance.

See the reference Apex docs for the API. It is suggested that you keep a page open with the Apex docs loaded for your reference whilst reading this page.

If you wish to try the Types example code , see Geting Started.

Use Case - Reduce Need For Global Classes

If you’re using the Force Framework package, the code for that package is in the forcefw namespace. Taking Dependency Injection as an example, you would need to make any implementation classes global, otherwise the Dependency class in the forcefw namespace would not be able to construct your class. Making classes global results in several issues including not being able to make any changes to global methods after they’ve been released in a package.

An alternative solution is for your implementation class to implement an interface with public methods and then have a global Factory class that returns an instance of your public implementation class from its newInstance method. The Factory class would be added as a Dependency binding registered for the interface. When the interface is injected into an application, the Factory class’s newInstance method is called and the public implementation is injected.

In the following example, you can see that the Types API can be used to create an implementation class for an interface that can be injected into an application using the Force Framework package without having to make the class global.

// The interface needs to be global, so it can be accessed by the forcefw namespace when creating the binding.
//
global interface ExampleInterface {
    void doSomething();
}

// Public class implementation of the interface. It doesn't need to be global as it will be constructed from the
// global ExampleFactory class which is in the same namespace as this class.
//
public class ExampleInterfaceImpl implements ExampleInterface {
    public void doSomething() {
        System.debug('Did something');
    }
}

// Making the class global means it can be constructed in the forecew namespace.
//
global class ExampleInterfaceFactory implements forcefw.TypesV1.Factory {

    // Note that as the interface is being used, we don't have to make the method global.
    public Object newInstance() {
    
        // A new instance of the public class is returned. All the interface methods coded in this class are
        // accessible in any namespace.
        //
        return new ExampleInterfaceImpl();
    }
}

Example Code

The SimpleAddition class in the examples (or examples.pkg) directory shows how to develop an API which is only accessible to the application through the interface it provides.

After deploying the code to an org, you can run the following Anonymous Apex to test it.

System.debug(SimpleAddition.sum(new List<Integer> { 1, 2, 3 ,4 }));