processBeforeInstantiation method
Intercepts pod instantiation before the default instantiation process begins.
This method can completely bypass the default instantiation mechanism by returning a pod instance. If a non-null value is returned, the container will use that instance instead of creating one through normal means.
Use Cases
- Providing mock instances in test environments
- Implementing custom instance creation logic
- Integrating with third-party instance management systems
- Implementing sophisticated pooling or caching strategies
Parameters
@param podClass The class of the pod to be instantiated
@param name The name of the pod in the container
@return A pod instance to use instead of default instantiation, or null to proceed normally
Example:
@override
Future<Object?> processBeforeInstantiation(Class podClass, String name) async {
if (podClass == Class<ExpensiveService>()) {
// Return cached instance instead of creating new one
return _cache.getOrCreate(name, () => ExpensiveService());
}
return null; // Use default instantiation
}
Implementation
Future<Object?> processBeforeInstantiation(Class podClass, String name) async => null;