5454public virtual class fflib_SObjectUnitOfWork
5555 implements fflib_ISObjectUnitOfWork
5656{
57+ /*
58+ * Ways a unit of work can handle unresolved relationships where child is inserted at same time or after the parent
59+ *
60+ * ATTEMPT_RESOLVE_OUT_OF_ORDER - Set the child lookup post-insert and update
61+ * IGNORE_OUT_OF_ORDER - leave the lookup null (default behaviour)
62+ */
63+ public enum UnresolvedRelationshipBehavior { ATTEMPT_RESOLVE_OUT_OF_ORDER , IGNORE_OUT_OF_ORDER }
64+
5765 protected List <Schema .SObjectType > m_sObjectTypes = new List <Schema .SObjectType >();
5866
5967 protected Map <String , List <SObject >> m_newListByType = new Map <String , List <SObject >>();
@@ -70,7 +78,8 @@ public virtual class fflib_SObjectUnitOfWork
7078
7179 protected IDML m_dml ;
7280
73- protected Boolean m_attemptToResolveOutOfOrderRelationships = false ;
81+ protected UnresolvedRelationshipBehavior m_unresolvedRelationshipBehaviour =
82+ UnresolvedRelationshipBehavior .IGNORE_OUT_OF_ORDER ;
7483
7584 /**
7685 * Interface describes work to be performed during the commitWork method
@@ -113,37 +122,39 @@ public virtual class fflib_SObjectUnitOfWork
113122 * Constructs a new UnitOfWork to support work against the given object list
114123 *
115124 * @param sObjectTypes A list of objects given in dependency order (least dependent first)
116- * @param resolveOutOfOrderRelationships If true and a relationship is registered where a parent is inserted after a
117- * child then will update the child post-insert to set the relationship. If
118- * false then relationship will not be set.
125+ * @param unresolvedRelationshipsBehaviour If ATTEMPT_OUT_OF_ORDER_RELATIONSHIPS and a relationship is registered
126+ * where a parent is inserted after a child then will update the child
127+ * post-insert to set the relationship. If IGNORE_OUT_OF_ORDER then
128+ * relationship will not be set.
119129 */
120- public fflib_SObjectUnitOfWork (List <Schema .SObjectType > sObjectTypes , Boolean resolveOutOfOrderRelationships ) {
121- this (sObjectTypes , new SimpleDML (), resolveOutOfOrderRelationships );
130+ public fflib_SObjectUnitOfWork (List <Schema .SObjectType > sObjectTypes ,
131+ UnresolvedRelationshipBehavior unresolvedRelationshipBehavior ) {
132+ this (sObjectTypes , new SimpleDML (), unresolvedRelationshipBehavior );
122133 }
123134
124135 /**
125136 * Constructs a new UnitOfWork to support work against the given object list
126137 *
127138 * @param sObjectTypes A list of objects given in dependency order (least dependent first)
128- * @param resolveOutOfOrderRelationships If true and a relationship is registered where a parent is inserted after a
129- * child then will update the child post-insert to set the relationship. If
130- * false then relationship will not be set.
139+ * @param dml Modify the database via this class
131140 */
132141 public fflib_SObjectUnitOfWork (List <Schema .SObjectType > sObjectTypes , IDML dml )
133142 {
134- this (sObjectTypes , dml , false );
143+ this (sObjectTypes , dml , UnresolvedRelationshipBehavior . IGNORE_OUT_OF_ORDER );
135144 }
136145
137146 /**
138147 * Constructs a new UnitOfWork to support work against the given object list
139148 *
140149 * @param sObjectTypes A list of objects given in dependency order (least dependent first)
141150 * @param dml Modify the database via this class
142- * @param resolveOutOfOrderRelationships If true and a relationship is registered where a parent is inserted after a
143- * child then will update the child post-insert to set the relationship. If
144- * false then relationship will not be set.
151+ * @param unresolvedRelationshipsBehaviour If ATTEMPT_OUT_OF_ORDER_RELATIONSHIPS and a relationship is registered
152+ * where a parent is inserted after a child then will update the child
153+ * post-insert to set the relationship. If IGNORE_OUT_OF_ORDER then
154+ * relationship will not be set.
145155 */
146- public fflib_SObjectUnitOfWork (List <Schema .SObjectType > sObjectTypes , IDML dml , Boolean resolveOutOfOrderRelationships )
156+ public fflib_SObjectUnitOfWork (List <Schema .SObjectType > sObjectTypes , IDML dml ,
157+ UnresolvedRelationshipBehavior unresolvedRelationshipBehavior )
147158 {
148159 m_sObjectTypes = sObjectTypes .clone ();
149160
@@ -159,9 +170,7 @@ public virtual class fflib_SObjectUnitOfWork
159170
160171 m_dml = dml ;
161172
162- if (resolveOutOfOrderRelationships ) {
163- attemptToResolveOutOfOrderRelationships ();
164- }
173+ setUnresolvedRelationshipsBehaviour (unresolvedRelationshipBehavior );
165174 }
166175
167176 // default implementations for commitWork events
@@ -179,13 +188,15 @@ public virtual class fflib_SObjectUnitOfWork
179188 * to set the relationship in a subsequent update. This is slightly less efficient than the default behaviour
180189 * of silently failing to set the lookup in this scenario.
181190 *
191+ * @param behavior
192+ *
182193 * @return this unit of work
183194 */
184- private fflib_SObjectUnitOfWork attemptToResolveOutOfOrderRelationships ( )
195+ private fflib_SObjectUnitOfWork setUnresolvedRelationshipsBehaviour ( UnresolvedRelationshipBehavior behavior )
185196 {
186- m_attemptToResolveOutOfOrderRelationships = true ;
197+ m_unresolvedRelationshipBehaviour = behavior ;
187198 for (Relationships relationships : m_relationships .values ()) {
188- relationships .attemptToResolveOutOfOrderRelationships (true );
199+ relationships .attemptToResolveOutOfOrderRelationships (m_unresolvedRelationshipBehaviour );
189200 }
190201 return this ;
191202 }
@@ -203,7 +214,7 @@ public virtual class fflib_SObjectUnitOfWork
203214 m_dirtyMapByType .put (sObjectType .getDescribe ().getName (), new Map <Id , SObject >());
204215 m_deletedMapByType .put (sObjectType .getDescribe ().getName (), new Map <Id , SObject >());
205216 m_relationships .put (sObjectType .getDescribe ().getName (),
206- new Relationships ().attemptToResolveOutOfOrderRelationships (m_attemptToResolveOutOfOrderRelationships ));
217+ new Relationships ().attemptToResolveOutOfOrderRelationships (m_unresolvedRelationshipBehaviour ));
207218
208219 // give derived class opportunity to register the type
209220 onRegisterType (sObjectType );
@@ -418,7 +429,7 @@ public virtual class fflib_SObjectUnitOfWork
418429 }
419430
420431 // Resolve any unresolved relationships where parent was inserted after child, and so child lookup was not set
421- if (m_attemptToResolveOutOfOrderRelationships )
432+ if (m_unresolvedRelationshipBehaviour == UnresolvedRelationshipBehavior . ATTEMPT_RESOLVE_OUT_OF_ORDER )
422433 {
423434 for (Schema .SObjectType sObjectType : m_sObjectTypes )
424435 {
@@ -476,7 +487,8 @@ public virtual class fflib_SObjectUnitOfWork
476487 private class Relationships
477488 {
478489 private List <IRelationship > m_relationships = new List <IRelationship >();
479- private Boolean m_attemptToResolveOutOfOrderRelationships = false ;
490+ private UnresolvedRelationshipBehavior m_unresolvedRelationshipBehaviour =
491+ UnresolvedRelationshipBehavior .IGNORE_OUT_OF_ORDER ;
480492 private List <RelationshipPermittingOutOfOrderInsert > m_parentInsertedAfterChildRelationships =
481493 new List <RelationshipPermittingOutOfOrderInsert >();
482494
@@ -489,9 +501,9 @@ public virtual class fflib_SObjectUnitOfWork
489501 *
490502 * @return this object
491503 */
492- public Relationships attemptToResolveOutOfOrderRelationships (Boolean attemptToResolve )
504+ public Relationships attemptToResolveOutOfOrderRelationships (UnresolvedRelationshipBehavior behavior )
493505 {
494- m_attemptToResolveOutOfOrderRelationships = attemptToResolve ;
506+ m_unresolvedRelationshipBehaviour = behavior ;
495507 return this ;
496508 }
497509
@@ -504,7 +516,7 @@ public virtual class fflib_SObjectUnitOfWork
504516 relationship .resolve ();
505517
506518 // Check if parent is inserted after the child
507- if (m_attemptToResolveOutOfOrderRelationships &&
519+ if (m_unresolvedRelationshipBehaviour == UnresolvedRelationshipBehavior . ATTEMPT_RESOLVE_OUT_OF_ORDER &&
508520 relationship instanceof RelationshipPermittingOutOfOrderInsert &&
509521 ! ((RelationshipPermittingOutOfOrderInsert ) relationship ).Resolved )
510522 {
@@ -565,7 +577,7 @@ public virtual class fflib_SObjectUnitOfWork
565577 public void add (SObject record , Schema.sObjectField relatedToField , SObject relatedTo )
566578 {
567579 // Relationship to resolve
568- if (! m_attemptToResolveOutOfOrderRelationships )
580+ if (m_unresolvedRelationshipBehaviour == UnresolvedRelationshipBehavior . IGNORE_OUT_OF_ORDER )
569581 {
570582 Relationship relationship = new Relationship ();
571583 relationship .Record = record ;
0 commit comments