@@ -34,11 +34,22 @@ global inherited sharing class Cmdt {
3434 */
3535 global Cmdt.Repository ofType (SObjectType objectType ) {
3636 String objectApiName = objectType ?. toString ();
37- Cmdt .Repository repo = Cmdt .repos ?. get (objectApiName ) ?? new Cmdt . RepositoryBuilder (objectType );
37+ Cmdt .Repository repo = Cmdt .repos ?. get (objectApiName ) ?? this . initRepository (objectType );
3838 Cmdt .repos ?. put (objectApiName , repo );
3939 return repo ;
4040 }
4141
42+ private Cmdt.Repository initRepository (SObjectType objectType ) {
43+ // Maximize tradeoffs between heap size storage & query limits
44+ if (this .includesLongTextFields (objectType ) == true ) {
45+ // CMDT objects w/Long Text fields count towards query limits; query once and then cache
46+ return new CacheBasedRepository (objectType );
47+ } else {
48+ // All other CMDT objects are "free" to query; avoid cacheing to minimize heap usage
49+ return new QueryBasedRepository (objectType );
50+ }
51+ }
52+
4253 private Boolean includesLongTextFields (SObjectType objectType ) {
4354 // Returns true if the SObjectType includes long-text fields
4455 String objectApiName = objectType ?. toString ();
@@ -92,68 +103,25 @@ global inherited sharing class Cmdt {
92103 }
93104
94105 // **** INNER **** //
95- global class ConfigurationException extends Exception {}
96-
97- global virtual class RepositoryBuilder implements Cmdt .Repository {
98- protected Boolean alreadyQueried ;
99- protected SObjectType objectType ;
100- protected Cmdt.Repository repo ;
101-
102- protected RepositoryBuilder (SObjectType objectType ) {
103- this .alreadyQueried = false ;
104- this .repo = this .initInnerRepository ();
105- }
106-
107- global Map <String , SObject > getAll () {
108- this .alreadyQueried = true ;
109- return this .repo ?. getAll ();
110- }
111-
112- global SObject getInstance (String key ) {
113- this .alreadyQueried = true ;
114- return this .repo ?. getInstance (key );
115- }
116-
117- protected virtual Cmdt.Repository initInnerRepository () {
118- // TODO:
119- /* Maximize tradeoffs between heap size storage & query limits
120- if (this.includesLongTextFields(objectType) == true) {
121- // CMDT objects w/Long Text fields count towards query limits; query once and then cache
122- return new CacheBasedRepository(objectType);
123- } else {
124- // All other CMDT objects are "free" to query; avoid cacheing to minimize heap usage
125- return new QueryBasedRepository(objectType);
126- } */
127- }
128-
129- private void validateIfalreadyQueried () {
130- if (this .alreadyQueried ) {
131- String msg = objectType + ' : Cmdt.Repository cannot be modified once queried' ;
132- throw new Cmdt .ConfigurationException (msg );
133- }
134- }
135- }
136-
137106 /**
138107 * @description Repository implementation that caches query results to avoid repeated SOQL queries.
139108 * Use this for Custom Metadata Types that include long-text area fields, as queries for
140109 * these objects count towards SOQL governor limits.
141110 * https://help.salesforce.com/s/articleView?language=en_US&id=platform.custommetadatatypes_limits.htm&type=5
142111 */
143- @TestVisible
144- private inherited sharing virtual class CacheBasedRepository extends Cmdt .QueryBasedRepository {
112+ global inherited sharing virtual class CacheBasedRepository extends Cmdt .QueryBasedRepository {
145113 protected transient Map <String , SObject > records ;
146114
147115 protected CacheBasedRepository (SObjectType objectType ) {
148116 super (objectType );
149117 }
150118
151- protected override Map <String , SObject > getAll () {
119+ global override Map <String , SObject > getAll () {
152120 this .retrieve ();
153121 return this .records ;
154122 }
155123
156- protected override SObject getInstance (String qualifiedApiName ) {
124+ global override SObject getInstance (String qualifiedApiName ) {
157125 return this .getAll ()?. get (qualifiedApiName );
158126 }
159127
@@ -171,7 +139,7 @@ global inherited sharing class Cmdt {
171139 * unless the object contains a long-text area field
172140 * https://help.salesforce.com/s/articleView?language=en_US&id=platform.custommetadatatypes_limits.htm&type=5
173141 */
174- private inherited sharing virtual class QueryBasedRepository implements Cmdt .Repository {
142+ global inherited sharing virtual class QueryBasedRepository implements Cmdt .Repository {
175143 protected transient SObjectType objectType ;
176144
177145 protected QueryBasedRepository (SObjectType objectType ) {
@@ -182,7 +150,7 @@ global inherited sharing class Cmdt {
182150 * @description Retrieves all Custom Metadata Type records as a map keyed by DeveloperName.
183151 * @return Map of DeveloperName to SObject record
184152 */
185- protected virtual Map <String , SObject > getAll () {
153+ global virtual Map <String , SObject > getAll () {
186154 List <SObject > records = this .queryAll ();
187155 return Cmdt .mapByKey (records );
188156 }
@@ -192,7 +160,7 @@ global inherited sharing class Cmdt {
192160 * @param qualifiedApiName The DeveloperName of the record to retrieve, including Namespace (if applicable)
193161 * @return The SObject record, or null if not found
194162 */
195- protected virtual SObject getInstance (String qualifiedApiName ) {
163+ global virtual SObject getInstance (String qualifiedApiName ) {
196164 return this .initQuery ()?. addWhere (DEVELOPER_NAME , Soql .EQUALS , qualifiedApiName )?. toSoql ()?. queryFirst ();
197165 }
198166
0 commit comments