Skip to content

Commit 18aa7b5

Browse files
committed
Some registries refactored
1 parent 61e4d88 commit 18aa7b5

File tree

3 files changed

+32
-74
lines changed

3 files changed

+32
-74
lines changed

Orm/Xtensive.Orm/Orm/Internals/SessionBoundRegistries/EntityChangeRegistry.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
using System;
88
using System.Collections.Generic;
9-
using Xtensive.Collections;
109

1110

1211
namespace Xtensive.Orm.Internals
@@ -24,7 +23,7 @@ public sealed class EntityChangeRegistry : SessionBoundRegistry
2423
/// <summary>
2524
/// Gets the number of registered entities.
2625
/// </summary>
27-
public int Count { get { return count; } }
26+
public int Count => count;
2827

2928
/// <summary>
3029
/// Registers the specified item.
@@ -69,8 +68,9 @@ internal void Register(EntityState item)
6968
/// <returns>The sequence of items with specified state.</returns>
7069
public IEnumerable<EntityState> GetItems(PersistenceState state)
7170
{
72-
foreach (var item in GetContainer(state))
71+
foreach (var item in GetContainer(state)) {
7372
yield return item;
73+
}
7474
}
7575

7676
/// <summary>

Orm/Xtensive.Orm/Orm/Internals/SessionBoundRegistries/NonPairedReferenceChangesRegistry.cs

Lines changed: 27 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System;
88
using System.Collections.Generic;
99
using System.Linq;
10-
using Xtensive.Collections;
1110
using Xtensive.Core;
1211
using 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

Orm/Xtensive.Orm/Orm/Internals/SessionBoundRegistries/ReferenceFieldsChangesRegistry.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,12 @@ public void Register(Key fieldOwner, Key fieldValue, Key auxiliaryEntity, FieldI
5252
/// Gets all registered items.
5353
/// </summary>
5454
/// <returns>All registered items.</returns>
55-
public IEnumerable<ReferenceFieldChangeInfo> GetItems()
56-
{
57-
return changes;
58-
}
55+
public IEnumerable<ReferenceFieldChangeInfo> GetItems() => changes;
5956

6057
/// <summary>
6158
/// Removes all registered items.
6259
/// </summary>
63-
public void Clear()
64-
{
65-
changes.Clear();
66-
}
60+
public void Clear() => changes.Clear();
6761

6862
private void Register(ReferenceFieldChangeInfo fieldChangeInfo)
6963
{

0 commit comments

Comments
 (0)