Force-Framework

Mark Brennand

DependencyV1

global with sharing class DependencyV1
Description

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.

If no binding is found and a class named by the interface or class to be injected with Factory appended to it exists, then this class is injected into the registry. The Factory class must implement the TypesV1.Factory interface. If not, an exception is thrown.

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.

Copyright (c) 2025 Mark Brennand, released under MIT License.

AuthorMark Brennand

Methods

  bind

global static void bind(final Type forType, final Type withImpl)
Description

Adds 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)
Description

Adds 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)
Description

If 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)
Description

If 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.

  invalid

global static DependencyV1.ValidationResult invalid(final String errorMessage)
Description

Creates a validation result representing failure.

ParametererrorMessage: The validation failure error message.
ReturnsA validation result representing failure.

  isBound

global static Boolean isBound(final Type forType)
Description

Returns 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)
Description

Returns 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()
Description

Forces the custom bindings to be reloaded.

  valid

global static DependencyV1.ValidationResult valid()
Description

Creates a validation result representing success.

ReturnsA validation result representing success.

  validationResult

global static ValidationResult validationResult( final Boolean isOfType, final Boolean isForTypeAnInterface, final Type forType, final Type withImpl )
Description

Return a validation result based on whether the isOfType parameter indicates that the implementation is of the required type.

ParameterisOfType: True if the implementation is of the required type.
ParameterisForTypeAnInterface: True if the required type is an interface.
ParameterforType: The type the implementation is expected to be of.
ParameterwithImpl: The type of the implementation.
ReturnsThe validation result.

DependencyV1.APIException

global with sharing class APIException extends ForceFwException
Description

Exception thrown when a dependency injection operation fails.

DependencyV1.Prototype

global interface Prototype
Description

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

DependencyV1.ValidationResult

global interface ValidationResult
Description

Result of a binding validation check.

Methods

  getErrorMessage

String getErrorMessage()
Description

Returns the error message for a failed validation.

ReturnsThe error message.

  isValid

Boolean isValid()
Description

Is the result of the validation success.

ReturnsTrue if success, false if not.

DependencyV1.BindingCheck

global interface BindingCheck
Description

Interface 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

DependencyV1.ValidationResult validate(final Type forType, final Type withImpl)
Description

Given 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.