Skip to content

Commit 61e4d88

Browse files
committed
New SessionBoundRegistry class + its ancestrors moved to separate folder
1 parent f91b6c5 commit 61e4d88

File tree

5 files changed

+48
-75
lines changed

5 files changed

+48
-75
lines changed

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

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@ namespace Xtensive.Orm.Internals
1414
/// <summary>
1515
/// Registers <see cref="EntityState"/> changes.
1616
/// </summary>
17-
public sealed class EntityChangeRegistry : SessionBound
17+
public sealed class EntityChangeRegistry : SessionBoundRegistry
1818
{
1919
private readonly HashSet<EntityState> @new = new();
2020
private readonly HashSet<EntityState> modified = new();
2121
private readonly HashSet<EntityState> removed = new();
2222
private int count;
2323

24-
private bool changesDisabled;
25-
2624
/// <summary>
2725
/// Gets the number of registered entities.
2826
/// </summary>
@@ -86,33 +84,15 @@ public void Clear()
8684
removed.Clear();
8785
}
8886

89-
internal Core.Disposable PreventChanges()
90-
{
91-
changesDisabled = true;
92-
return new Core.Disposable((a) => changesDisabled = false);
93-
}
94-
9587
/// <exception cref="ArgumentOutOfRangeException"><paramref name="state"/> is out of range.</exception>
9688
private HashSet<EntityState> GetContainer(PersistenceState state)
9789
{
98-
switch (state) {
99-
case PersistenceState.New:
100-
return @new;
101-
case PersistenceState.Modified:
102-
return modified;
103-
case PersistenceState.Removed:
104-
return removed;
105-
default:
106-
throw new ArgumentOutOfRangeException(nameof(state));
107-
}
108-
}
109-
110-
private void EnsureRegistrationsAllowed()
111-
{
112-
if (changesDisabled) {
113-
throw new InvalidOperationException(
114-
string.Format(Strings.ExSessionXIsActivelyPersistingChangesNoPersistentChangesAllowed, Session.Guid));
115-
}
90+
return state switch {
91+
PersistenceState.New => @new,
92+
PersistenceState.Modified => modified,
93+
PersistenceState.Removed => removed,
94+
_ => throw new ArgumentOutOfRangeException(nameof(state)),
95+
};
11696
}
11797

11898

Orm/Xtensive.Orm/Orm/Internals/EntitySetChangeRegistry.cs renamed to Orm/Xtensive.Orm/Orm/Internals/SessionBoundRegistries/EntitySetChangeRegistry.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ namespace Xtensive.Orm.Internals
1212
/// <summary>
1313
/// Contains <see cref="EntitySetState"/>s which modified during the bounded session.
1414
/// </summary>
15-
public sealed class EntitySetChangeRegistry : SessionBound
15+
public sealed class EntitySetChangeRegistry : SessionBoundRegistry
1616
{
1717
private readonly HashSet<EntitySetState> modifiedEntitySets = new();
1818

19-
private bool changesDisabled;
20-
2119
/// <summary>
2220
/// Count of registered <see cref="EntitySetState"/>.
2321
/// </summary>
@@ -44,19 +42,6 @@ public void Register(EntitySetState entitySetState)
4442
/// </summary>
4543
public void Clear() => modifiedEntitySets.Clear();
4644

47-
internal Core.Disposable PreventChanges()
48-
{
49-
changesDisabled = true;
50-
return new Core.Disposable((a) => changesDisabled = false);
51-
}
52-
53-
private void EnsureRegistrationsAllowed()
54-
{
55-
if (changesDisabled) {
56-
throw new InvalidOperationException(
57-
string.Format(Strings.ExSessionXIsActivelyPersistingChangesNoPersistentChangesAllowed, Session.Guid));
58-
}
59-
}
6045

6146
/// <summary>
6247
/// Initializes a new instance of this class.

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Xtensive.Orm.Internals
1515
{
16-
internal sealed class NonPairedReferenceChangesRegistry : SessionBound
16+
internal sealed class NonPairedReferenceChangesRegistry : SessionBoundRegistry
1717
{
1818
private readonly struct Identifier : IEquatable<Identifier>
1919
{
@@ -54,8 +54,6 @@ public Identifier(EntityState entityState, AssociationInfo association)
5454
private readonly IDictionary<Identifier, HashSet<EntityState>> addedReferences = new Dictionary<Identifier, HashSet<EntityState>>();
5555
private readonly object accessGuard = new();
5656

57-
private bool changesDisabled;
58-
5957
public int RemovedReferencesCount => removedReferences.Values.Sum(el => el.Count);
6058

6159
public int AddedReferencesCount => addedReferences.Values.Sum(el => el.Count);
@@ -100,12 +98,6 @@ public void Clear()
10098
}
10199
}
102100

103-
internal Core.Disposable PreventChanges()
104-
{
105-
changesDisabled = true;
106-
return new Core.Disposable((a) => changesDisabled = false);
107-
}
108-
109101
private void RegisterChange(EntityState referencedState, EntityState referencingState, EntityState noLongerReferencedState, AssociationInfo association)
110102
{
111103
ArgumentValidator.EnsureArgumentNotNull(association, "association");
@@ -284,13 +276,6 @@ private EntityState GetStructureFieldValue(FieldInfo fieldOfStructure, Structure
284276
private string BuildNameOfEntityField(FieldInfo fieldOfOwner, FieldInfo referenceFieldOfStructure) =>
285277
$"{fieldOfOwner.Name}.{referenceFieldOfStructure.Name}";
286278

287-
private void EnsureRegistrationsAllowed()
288-
{
289-
if (changesDisabled) {
290-
throw new InvalidOperationException(
291-
string.Format(Strings.ExSessionXIsActivelyPersistingChangesNoPersistentChangesAllowed, Session.Guid));
292-
}
293-
}
294279

295280
internal NonPairedReferenceChangesRegistry(Session session)
296281
: base(session)

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ namespace Xtensive.Orm.Internals
1414
/// <summary>
1515
/// Registrates information about changed reference fields.
1616
/// </summary>
17-
internal sealed class ReferenceFieldsChangesRegistry : SessionBound
17+
internal sealed class ReferenceFieldsChangesRegistry : SessionBoundRegistry
1818
{
1919
private readonly HashSet<ReferenceFieldChangeInfo> changes = new();
2020

21-
private bool changesDisabled;
22-
2321
/// <summary>
2422
/// Registrates information about field which value was set.
2523
/// </summary>
@@ -67,25 +65,12 @@ public void Clear()
6765
changes.Clear();
6866
}
6967

70-
internal Core.Disposable PreventChanges()
71-
{
72-
changesDisabled = true;
73-
return new Core.Disposable((a) => changesDisabled = false);
74-
}
75-
7668
private void Register(ReferenceFieldChangeInfo fieldChangeInfo)
7769
{
7870
EnsureRegistrationsAllowed();
7971
_ = changes.Add(fieldChangeInfo);
8072
}
8173

82-
private void EnsureRegistrationsAllowed()
83-
{
84-
if (changesDisabled) {
85-
throw new System.InvalidOperationException(
86-
string.Format(Strings.ExSessionXIsActivelyPersistingChangesNoPersistentChangesAllowed, Session.Guid));
87-
}
88-
}
8974

9075
public ReferenceFieldsChangesRegistry(Session session)
9176
: base(session)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (C) 2022 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
// Created by: Dmitri Maximov
5+
// Created: 2022.06.14
6+
7+
using System;
8+
9+
10+
namespace Xtensive.Orm.Internals
11+
{
12+
/// <summary>
13+
/// Base type for <see cref="SessionBound"/> registries.
14+
/// </summary>
15+
public abstract class SessionBoundRegistry : SessionBound
16+
{
17+
private bool changesDisabled;
18+
19+
internal Core.Disposable PreventChanges()
20+
{
21+
changesDisabled = true;
22+
return new Core.Disposable((a) => changesDisabled = false);
23+
}
24+
25+
protected void EnsureRegistrationsAllowed()
26+
{
27+
if (changesDisabled) {
28+
throw new InvalidOperationException(
29+
string.Format(Strings.ExSessionXIsActivelyPersistingChangesNoPersistentChangesAllowed, Session.Guid));
30+
}
31+
}
32+
33+
public SessionBoundRegistry(Session session)
34+
: base(session)
35+
{
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)