Force-Framework

Mark Brennand

DependencyV1

global with sharing class DependencyV1
DescriptionCopyright (c) 2025 Mark Brennand, released under MIT License.

Class defining the external Dependency API.

Dependencies can be injected into an application by their interface or abstract class.

Variable and member variable values can also be injected into a class using the variable's class and an action.

An optional action argument is supported to allow for the binding of the same interface or class with different implementations.

By default, the implementation created when injected will be a singleton. If the binding is required to create a new implementation for each use an application, the implementing class should implement the Prototype interface.

All bound Types are stored in a registry. Each bound Type must have a BindingCheck implementation associated with it. To associate the Type with the BindingCheck, create and deploy a BindingCheck__mdt metadata record. Any attempt to add a binding to the registry for a Type with no associated BindingCheck will throw an Exception.

See README for full details of the Dependency Injection API.
AuthorMark Brennand

Methods

  bind

global static void bind(final Type forType, final Type withImpl)
DescriptionAdds a binding to the registry.
ParameterforType: The type to be bound. Must be either an interface or abstract class.
ParameterwithImpl: The implementation for the type and action. Must be a concrete class with a no-op constructor.

  bind

global static void bind(final Type forType, final String action, final Type withImpl)
DescriptionAdds a binding to the registry.
ParameterforType: The type to be bound. Must be either an interface or abstract class.
Parameteraction: The action used in combination with the type to uniquely identify the binding.
ParameterwithImpl: The implementation for the type and action. Must be a concrete class with a no-op constructor.

  inject

global static Object inject(final Type forType)
DescriptionIf the registry contains a binding for the given type, its implementation is returned.

Should no binding exist, an exception is thrown.
ParameterforType: The type to be bound.
ReturnsThe implementation of the given type.

  inject

global static Object inject(final Type forType, final String action)
DescriptionIf the registry contains a binding for the given type and action, its implementation is returned.

Should no binding exist, an exception is thrown.
ParameterforType: The type to be bound.
Parameteraction: The action specific to the binding to be bound.
ReturnsThe implementation of the given type.

  isBound

global static Boolean isBound(final Type forType)
DescriptionReturns true if registry contains a binding for the given type and action, false if not.
ParameterforType: The type to be checked.
ReturnsTrue if binding exists, false if not.

  isBound

global static Boolean isBound(final Type forType, final String action)
DescriptionReturns true if registry contains a binding for the given type and action, false if not.
ParameterforType: The type to be checked.
Parameteraction: The action specific to the binding to be checked.
ReturnsTrue if binding exists, false if not.

  reload

global static void reload()
DescriptionForces the custom bindings to be reloaded.

DependencyV1.APIException

global with sharing class APIException extends Exception
DescriptionException thrown when a dependency injection operation fails.

DependencyV1.Prototype

global interface Prototype
DescriptionA class implementing this interface will have a new instance of itself created each time it is in injected into an application.

DependencyV1.ValidationResult

global with sharing class ValidationResult
DescriptionClass representing result of a binding validation check.

Methods

  ValidationResult

global ValidationResult(final Boolean isValid, final String errorMessage)
DescriptionConstructs a validation result object which is used to inform the caller of the BindingCheck.validate method of the result of the validation.
ParameterisValid: True if the validation passed. False if not.
ParametererrorMessage: The error message to be recorded in the Exception thrown to record the failure.

DependencyV1.BindingCheck

global interface BindingCheck
DescriptionInterface to be implemented to check a binding in the registry.

Each checker must be defined in a record in the Binding Check custom metadata.

Methods

  validate

ValidationResult validate(Type forType, Type withImpl)
DescriptionGiven a type to be bound, a class implementing this method must check that the given implementation class can be bound to it.

If the for type is an interface then an implementation of this method must check that the implementation class implements the interface.

If the for type is an abstract class then an implementation of this method must check that the implementation class extends the abstract class.

If the for type is a class then an implementation of this method must check that the implementation class is of the same class or a super class of it.
ParameterforType: The type to be bound.
ParameterwithImpl: The implementation to bind to the type.
ReturnsThe result of the validation.