Created
November 3, 2020 20:00
-
-
Save gbutt/9319f8cd9fad007b95de7571d5971847 to your computer and use it in GitHub Desktop.
DependencyResolver.cls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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