77using System ;
88using System . Collections . Generic ;
99using System . Linq ;
10- using Xtensive . Collections ;
1110using Xtensive . Core ;
1211using Xtensive . Orm . Model ;
1312
@@ -22,21 +21,15 @@ internal sealed class NonPairedReferenceChangesRegistry : SessionBoundRegistry
2221
2322 public static Identifier Make ( EntityState state , AssociationInfo association ) => new ( state , association ) ;
2423
25- public bool Equals ( Identifier other )
26- {
27- if ( ! Association . Equals ( other . Association ) )
28- return false ;
29- if ( ! EntityState . Equals ( other . EntityState ) )
30- return false ;
31- return true ;
32- }
24+ public bool Equals ( Identifier other ) =>
25+ Association . Equals ( other . Association ) && EntityState . Equals ( other . EntityState ) ;
3326
3427 public override bool Equals ( object obj ) => Equals ( ( Identifier ) obj ) ;
3528
3629 public override int GetHashCode ( )
3730 {
3831 unchecked {
39- int hash = 139 ;
32+ var hash = 139 ;
4033 hash = hash * 311 + EntityState . GetHashCode ( ) ;
4134 hash = hash * 311 + Association . GetHashCode ( ) ;
4235 return hash ;
@@ -63,29 +56,19 @@ public IEnumerable<EntityState> GetRemovedReferencesTo(EntityState target, Assoc
6356 ArgumentValidator . EnsureArgumentNotNull ( target , "target" ) ;
6457 ArgumentValidator . EnsureArgumentNotNull ( association , "association" ) ;
6558
66- if ( association . IsPaired ) {
67- return Array . Empty < EntityState > ( ) ;
68- }
69-
70- var key = Identifier . Make ( target , association ) ;
71- return removedReferences . TryGetValue ( key , out var removedMap )
72- ? removedMap
73- : Array . Empty < EntityState > ( ) ;
59+ return association . IsPaired || ! removedReferences . TryGetValue ( Identifier . Make ( target , association ) , out var removedMap )
60+ ? Array . Empty < EntityState > ( )
61+ : removedMap ;
7462 }
7563
7664 public IEnumerable < EntityState > GetAddedReferenceTo ( EntityState target , AssociationInfo association )
7765 {
7866 ArgumentValidator . EnsureArgumentNotNull ( target , "target" ) ;
7967 ArgumentValidator . EnsureArgumentNotNull ( association , "association" ) ;
8068
81- if ( association . IsPaired ) {
82- return Array . Empty < EntityState > ( ) ;
83- }
84-
85- var key = Identifier . Make ( target , association ) ;
86- return addedReferences . TryGetValue ( key , out var removedMap )
87- ? removedMap
88- : Array . Empty < EntityState > ( ) ;
69+ return association . IsPaired || ! addedReferences . TryGetValue ( Identifier . Make ( target , association ) , out var removedMap )
70+ ? Array . Empty < EntityState > ( )
71+ : removedMap ;
8972 }
9073
9174 public void Invalidate ( ) => Clear ( ) ;
@@ -103,12 +86,10 @@ private void RegisterChange(EntityState referencedState, EntityState referencing
10386 ArgumentValidator . EnsureArgumentNotNull ( association , "association" ) ;
10487 ArgumentValidator . EnsureArgumentNotNull ( referencingState , "referencingState" ) ;
10588
106- if ( ! Session . DisableAutoSaveChanges )
107- return ;
108- if ( association . IsPaired )
109- return ;
110- if ( referencedState == null && noLongerReferencedState == null )
89+ if ( ! Session . DisableAutoSaveChanges || association . IsPaired
90+ || ( referencedState == null && noLongerReferencedState == null ) ) {
11191 return ;
92+ }
11293
11394 if ( referencedState != null && noLongerReferencedState != null ) {
11495 var oldKey = Identifier . Make ( noLongerReferencedState , association ) ;
@@ -156,7 +137,6 @@ private void RegisterAddInternal(Identifier newKey, EntityState referencingState
156137 _ = removedReferences . Remove ( newKey ) ;
157138 }
158139 }
159-
160140 return ;
161141 }
162142 if ( addedReferences . TryGetValue ( newKey , out var addedRefs ) ) {
@@ -199,8 +179,9 @@ private void OnEntitySetItemAddCompleted(object sender, EntitySetItemActionCompl
199179
200180 private void OnEntityFieldValueSetCompleted ( object sender , EntityFieldValueSetCompletedEventArgs e )
201181 {
202- if ( ShouldSkipRegistration ( e ) )
182+ if ( ShouldSkipRegistration ( e ) ) {
203183 return ;
184+ }
204185 if ( e . Field . IsStructure ) {
205186 HandleStructureValues ( e . Entity , e . Field , ( Structure ) e . OldValue , ( Structure ) e . NewValue ) ;
206187 }
@@ -215,7 +196,7 @@ private void HandleStructureValues(Entity owner, FieldInfo fieldOfOwner, Structu
215196 . Union ( oldValue . TypeInfo . StructureFieldMapping . Values . Where ( f => f . IsEntity ) ) ;
216197
217198 foreach ( var referenceFieldOfStructure in referenceFields ) {
218- var realField = owner . TypeInfo . Fields [ BuildNameOfEntityField ( fieldOfOwner , referenceFieldOfStructure ) ] ;
199+ var realField = owner . TypeInfo . Fields [ BuildNameEntityFieldName ( fieldOfOwner , referenceFieldOfStructure ) ] ;
219200 if ( realField . Associations . Count > 1 ) {
220201 continue ;
221202 }
@@ -233,35 +214,18 @@ private void HandleEntityValues(Entity owner, FieldInfo field, Entity oldValue,
233214 RegisterChange ( newValue ? . State , owner . State , oldValue ? . State , association ) ;
234215 }
235216
236- private bool ShouldSkipRegistration ( EntityFieldValueSetCompletedEventArgs e )
237- {
238- if ( ! Session . DisableAutoSaveChanges )
239- return true ;
240- if ( Session . IsPersisting )
241- return true ;
242- if ( e . Exception != null )
243- return true ;
244- if ( ! e . Field . IsEntity && ! e . Field . IsStructure )
245- return true ;
246- if ( e . Field . IsEntity && e . Field . Associations . First ( ) . IsPaired )
247- return true ;
248- if ( e . NewValue == null && e . OldValue == null )
249- return true ;
250- return false ;
251- }
217+ private bool ShouldSkipRegistration ( EntityFieldValueSetCompletedEventArgs e ) =>
218+ ! Session . DisableAutoSaveChanges || Session . IsPersisting
219+ || e . Exception != null
220+ || ( ! e . Field . IsEntity && ! e . Field . IsStructure )
221+ || ( e . Field . IsEntity && e . Field . Associations . First ( ) . IsPaired )
222+ || ( e . NewValue == null && e . OldValue == null ) ;
223+
252224
253- private bool ShouldSkipRegistration ( EntitySetItemActionCompletedEventArgs e )
254- {
255- if ( ! Session . DisableAutoSaveChanges )
256- return true ;
257- if ( Session . IsPersisting )
258- return true ;
259- if ( e . Exception != null )
260- return true ;
261- if ( e . Field . Associations . First ( ) . IsPaired )
262- return true ;
263- return false ;
264- }
225+ private bool ShouldSkipRegistration ( EntitySetItemActionCompletedEventArgs e ) =>
226+ ! Session . DisableAutoSaveChanges || Session . IsPersisting
227+ || e . Exception != null
228+ || e . Field . Associations . First ( ) . IsPaired ;
265229
266230 private EntityState GetStructureFieldValue ( FieldInfo fieldOfStructure , Structure structure )
267231 {
@@ -273,7 +237,7 @@ private EntityState GetStructureFieldValue(FieldInfo fieldOfStructure, Structure
273237 return entity ? . State ;
274238 }
275239
276- private string BuildNameOfEntityField ( FieldInfo fieldOfOwner , FieldInfo referenceFieldOfStructure ) =>
240+ private string BuildNameEntityFieldName ( FieldInfo fieldOfOwner , FieldInfo referenceFieldOfStructure ) =>
277241 $ "{ fieldOfOwner . Name } .{ referenceFieldOfStructure . Name } ";
278242
279243
0 commit comments