Skip to content

Commit f3a449a

Browse files
authored
Adds events for changes cancelling (#54)
1 parent 513cad9 commit f3a449a

File tree

3 files changed

+65
-9
lines changed

3 files changed

+65
-9
lines changed

Orm/Xtensive.Orm.Tests/Storage/SessionEventsTest.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2009-2020 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: Alex Kofman
55
// Created: 2009.10.08
66

@@ -45,6 +45,9 @@ protected override DomainConfiguration BuildConfiguration()
4545
private EventArgs persistingArgs;
4646
private EventArgs persistedArgs;
4747

48+
private EventArgs changesCancelingArgs;
49+
private EventArgs changesCanceledArgs;
50+
4851
private EntityEventArgs entityCreatedArgs;
4952
private EntityEventArgs entityRemoving;
5053
private EntityEventArgs entityRemoved;
@@ -100,6 +103,9 @@ public void MainTest()
100103
session.Events.Persisting += (sender, e) => persistingArgs = e;
101104
session.Events.Persisted += (sender, e) => persistedArgs = e;
102105

106+
session.Events.ChangesCanceling += (sender, e) => changesCancelingArgs = e;
107+
session.Events.ChangesCanceled += (sender, e) => changesCanceledArgs = e;
108+
103109
session.Events.EntityCreated += (sender, e) => entityCreatedArgs = e;
104110
session.Events.EntityRemoving += (sender, e) => entityRemoving = e;
105111
session.Events.EntityRemove += (sender, e) => entityRemoved = e;
@@ -113,6 +119,7 @@ public void MainTest()
113119
RollbackTransaction();
114120
ErrorOnCommit();
115121
EditEntity();
122+
CancelChanges();
116123
}
117124
}
118125

@@ -221,5 +228,26 @@ private void EditEntity()
221228
Assert.AreEqual(entity, entityRemoved.Entity);
222229
}
223230
}
231+
232+
private void CancelChanges()
233+
{
234+
var session = Session.Demand();
235+
using (var transactionScope = session.OpenTransaction()) {
236+
ClearEvents();
237+
238+
var megaEntity = new MegaEntity { Value = 1 };
239+
240+
session.CancelChanges();
241+
}
242+
243+
Assert.IsNotNull(transactionRollbackingArgs);
244+
Assert.IsNotNull(transactionRollbackedArgs);
245+
Assert.IsNotNull(changesCancelingArgs);
246+
Assert.IsNotNull(changesCanceledArgs);
247+
Assert.IsNull(persistingArgs);
248+
Assert.IsNull(persistedArgs);
249+
Assert.IsNull(transactionCommitingArgs);
250+
Assert.IsNull(transactionCommitedArgs);
251+
}
224252
}
225253
}

Orm/Xtensive.Orm/Orm/Session.Persist.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2007-2020 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: Dmitri Maximov
55
// Created: 2007.08.10
66

@@ -57,9 +57,15 @@ public void SaveChanges()
5757
/// <exception cref="NotSupportedException">Unable to cancel changes for non-disconnected session. Use transaction boundaries to control the state.</exception>
5858
public void CancelChanges()
5959
{
60+
SystemEvents.NotifyChangesCanceling();
61+
Events.NotifyChangesCanceling();
62+
6063
CancelEntitySetsChanges();
6164
CancelEntitiesChanges();
6265
NonPairedReferencesRegistry.Clear();
66+
67+
SystemEvents.NotifyChangesCanceled();
68+
Events.NotifyChangesCanceled();
6369
}
6470

6571
internal void Persist(PersistReason reason)

Orm/Xtensive.Orm/Orm/SessionEventAccessor.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2010-2020 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: Alex Yakunin
55
// Created: 2010.08.10
66

@@ -67,6 +67,16 @@ public sealed class SessionEventAccessor
6767
/// </summary>
6868
public event EventHandler Persisted;
6969

70+
/// <summary>
71+
/// Occurs when <see cref="Session"/> is about to <see cref="Orm.Session.CancelChanges()"/> changes.
72+
/// </summary>
73+
public event EventHandler ChangesCanceling;
74+
75+
/// <summary>
76+
/// Occurs when changes are canceled.
77+
/// </summary>
78+
public event EventHandler ChangesCanceled;
79+
7080
/// <summary>
7181
/// Occurs when local <see cref="Key"/> created.
7282
/// </summary>
@@ -284,6 +294,18 @@ internal void NotifyPersisted()
284294
Persisted(this, EventArgs.Empty);
285295
}
286296

297+
internal void NotifyChangesCanceling()
298+
{
299+
if (ChangesCanceling != null && AreNotificationsEnabled())
300+
ChangesCanceling(this, EventArgs.Empty);
301+
}
302+
303+
internal void NotifyChangesCanceled()
304+
{
305+
if (ChangesCanceled != null && AreNotificationsEnabled())
306+
ChangesCanceled(this, EventArgs.Empty);
307+
}
308+
287309
internal void NotifyKeyGenerated(Key key)
288310
{
289311
if (KeyGenerated!=null && AreNotificationsEnabled())

0 commit comments

Comments
 (0)