Skip to content

Commit 644f3ab

Browse files
committed
No change allowed during active persisting
1 parent 0fb2dd9 commit 644f3ab

File tree

9 files changed

+159
-136
lines changed

9 files changed

+159
-136
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2008-2020 Xtensive LLC.
1+
// Copyright (C) 2008-2022 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Dmitri Maximov
@@ -16,9 +16,9 @@ namespace Xtensive.Orm.Internals
1616
/// </summary>
1717
public sealed class EntityChangeRegistry : SessionBound
1818
{
19-
private readonly HashSet<EntityState> @new = new HashSet<EntityState>();
20-
private readonly HashSet<EntityState> modified = new HashSet<EntityState>();
21-
private readonly HashSet<EntityState> removed = new HashSet<EntityState>();
19+
private readonly HashSet<EntityState> @new = new();
20+
private readonly HashSet<EntityState> modified = new();
21+
private readonly HashSet<EntityState> removed = new();
2222
private int count;
2323

2424
/// <summary>
@@ -34,7 +34,8 @@ internal void Register(EntityState item)
3434
{
3535
// Remove-create sequences fix for Issue 690
3636
if (item.PersistenceState == PersistenceState.New && removed.Contains(item)) {
37-
removed.Remove(item);
37+
EnsureChangesAreNotPersisting();
38+
_ = removed.Remove(item);
3839
count--;
3940
if (item.DifferentialTuple.Difference == null) {
4041
item.SetPersistenceState(PersistenceState.Synchronized);
@@ -43,18 +44,22 @@ internal void Register(EntityState item)
4344
item.SetPersistenceState(PersistenceState.Modified);
4445
}
4546
else if (item.PersistenceState == PersistenceState.Removed && @new.Contains(item)) {
46-
@new.Remove(item);
47+
EnsureChangesAreNotPersisting();
48+
_ = @new.Remove(item);
4749
count--;
4850
return;
4951
}
5052
else if (item.PersistenceState == PersistenceState.Removed && modified.Contains(item)) {
51-
modified.Remove(item);
53+
EnsureChangesAreNotPersisting();
54+
_ = modified.Remove(item);
5255
count--;
5356
}
5457

5558
var container = GetContainer(item.PersistenceState);
56-
if (container.Add(item))
59+
EnsureChangesAreNotPersisting();
60+
if (container.Add(item)) {
5761
count++;
62+
}
5863
}
5964

6065
/// <summary>

Orm/Xtensive.Orm/Orm/Internals/EntitySetChangeRegistry.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2014 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2014-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.
44
// Created by: Alexey Kulakov
55
// Created: 2014.03.27
66

@@ -13,35 +13,33 @@ namespace Xtensive.Orm.Internals
1313
/// </summary>
1414
public sealed class EntitySetChangeRegistry : SessionBound
1515
{
16-
private readonly HashSet<EntitySetState> modifiedEntitySets = new HashSet<EntitySetState>();
16+
private readonly HashSet<EntitySetState> modifiedEntitySets = new();
1717

1818
/// <summary>
1919
/// Count of registered <see cref="EntitySetState"/>.
2020
/// </summary>
21-
public int Count { get { return modifiedEntitySets.Count; } }
21+
public int Count => modifiedEntitySets.Count;
2222

2323
/// <summary>
2424
/// Register the specified <see cref="EntitySetState"/>.
2525
/// </summary>
2626
/// <param name="entitySetState"><see cref="EntitySetState"/> to bound.</param>
2727
public void Register(EntitySetState entitySetState)
2828
{
29-
modifiedEntitySets.Add(entitySetState);
29+
EnsureChangesAreNotPersisting();
30+
_ = modifiedEntitySets.Add(entitySetState);
3031
}
3132

3233
/// <summary>
3334
/// Gets all registered items.
3435
/// </summary>
3536
/// <returns></returns>
36-
public IEnumerable<EntitySetState> GetItems()
37-
{
38-
return modifiedEntitySets;
39-
}
37+
public IEnumerable<EntitySetState> GetItems() => modifiedEntitySets;
4038

41-
public void Clear()
42-
{
43-
modifiedEntitySets.Clear();
44-
}
39+
/// <summary>
40+
/// Clears the registry.
41+
/// </summary>
42+
public void Clear() => modifiedEntitySets.Clear();
4543

4644
/// <summary>
4745
/// Initializes a new instance of this class.

0 commit comments

Comments
 (0)