Skip to content

Instantly share code, notes, and snippets.

@gbutt
Created November 3, 2020 20:00
Show Gist options
  • Save gbutt/9319f8cd9fad007b95de7571d5971847 to your computer and use it in GitHub Desktop.
Save gbutt/9319f8cd9fad007b95de7571d5971847 to your computer and use it in GitHub Desktop.
DependencyResolver.cls
public class DependencyResolver {
@TestVisible
private static Map<Type, Object> cachedDependencies {get; set;}
static {
cachedDependencies = new Map<Type, Object>();
}
// core methods abstract the core logic
public static Object getInstance(Type classType) {
if (cachedDependencies.containsKey(classType)) {
return cachedDependencies.get(classType);
}
return classType.newInstance();
}
public static Object getSingleton(Type classType) {
if (!cachedDependencies.containsKey(classType)) {
cachedDependencies.put(classType, classType.newInstance());
}
return cachedDependencies.get(classType);
}
@TestVisible
private static void mockDependency(Type classType, Object instance) {
cachedDependencies.put(classType, instance);
}
@TestVisible
private static void mockDependency(StubProviderImpl stubProvider) {
cachedDependencies.put(stubProvider.mockType, stubProvider.buildMockInstance());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment