Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions Orm/Xtensive.Orm.Tests/Storage/SessionEventsTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (C) 2003-2010 Xtensive LLC.
// All rights reserved.
// For conditions of distribution and use, see license.
// Copyright (C) 2009-2020 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Alex Kofman
// Created: 2009.10.08

Expand Down Expand Up @@ -45,6 +45,9 @@ protected override DomainConfiguration BuildConfiguration()
private EventArgs persistingArgs;
private EventArgs persistedArgs;

private EventArgs changesCancelingArgs;
private EventArgs changesCanceledArgs;

private EntityEventArgs entityCreatedArgs;
private EntityEventArgs entityRemoving;
private EntityEventArgs entityRemoved;
Expand Down Expand Up @@ -100,6 +103,9 @@ public void MainTest()
session.Events.Persisting += (sender, e) => persistingArgs = e;
session.Events.Persisted += (sender, e) => persistedArgs = e;

session.Events.ChangesCanceling += (sender, e) => changesCancelingArgs = e;
session.Events.ChangesCanceled += (sender, e) => changesCanceledArgs = e;

session.Events.EntityCreated += (sender, e) => entityCreatedArgs = e;
session.Events.EntityRemoving += (sender, e) => entityRemoving = e;
session.Events.EntityRemove += (sender, e) => entityRemoved = e;
Expand All @@ -113,6 +119,7 @@ public void MainTest()
RollbackTransaction();
ErrorOnCommit();
EditEntity();
CancelChanges();
}
}

Expand Down Expand Up @@ -221,5 +228,26 @@ private void EditEntity()
Assert.AreEqual(entity, entityRemoved.Entity);
}
}

private void CancelChanges()
{
var session = Session.Demand();
using (var transactionScope = session.OpenTransaction()) {
ClearEvents();

var megaEntity = new MegaEntity { Value = 1 };

session.CancelChanges();
}

Assert.IsNotNull(transactionRollbackingArgs);
Assert.IsNotNull(transactionRollbackedArgs);
Assert.IsNotNull(changesCancelingArgs);
Assert.IsNotNull(changesCanceledArgs);
Assert.IsNull(persistingArgs);
Assert.IsNull(persistedArgs);
Assert.IsNull(transactionCommitingArgs);
Assert.IsNull(transactionCommitedArgs);
}
}
}
12 changes: 9 additions & 3 deletions Orm/Xtensive.Orm/Orm/Session.Persist.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (C) 2003-2010 Xtensive LLC.
// All rights reserved.
// For conditions of distribution and use, see license.
// Copyright (C) 2007-2020 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Dmitri Maximov
// Created: 2007.08.10

Expand Down Expand Up @@ -57,9 +57,15 @@ public void SaveChanges()
/// <exception cref="NotSupportedException">Unable to cancel changes for non-disconnected session. Use transaction boundaries to control the state.</exception>
public void CancelChanges()
{
SystemEvents.NotifyChangesCanceling();
Events.NotifyChangesCanceling();

CancelEntitySetsChanges();
CancelEntitiesChanges();
NonPairedReferencesRegistry.Clear();

SystemEvents.NotifyChangesCanceled();
Events.NotifyChangesCanceled();
}

internal void Persist(PersistReason reason)
Expand Down
28 changes: 25 additions & 3 deletions Orm/Xtensive.Orm/Orm/SessionEventAccessor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (C) 2003-2010 Xtensive LLC.
// All rights reserved.
// For conditions of distribution and use, see license.
// Copyright (C) 2010-2020 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Alex Yakunin
// Created: 2010.08.10

Expand Down Expand Up @@ -67,6 +67,16 @@ public sealed class SessionEventAccessor
/// </summary>
public event EventHandler Persisted;

/// <summary>
/// Occurs when <see cref="Session"/> is about to <see cref="Orm.Session.CancelChanges()"/> changes.
/// </summary>
public event EventHandler ChangesCanceling;

/// <summary>
/// Occurs when changes are canceled.
/// </summary>
public event EventHandler ChangesCanceled;

/// <summary>
/// Occurs when local <see cref="Key"/> created.
/// </summary>
Expand Down Expand Up @@ -284,6 +294,18 @@ internal void NotifyPersisted()
Persisted(this, EventArgs.Empty);
}

internal void NotifyChangesCanceling()
{
if (ChangesCanceling != null && AreNotificationsEnabled())
ChangesCanceling(this, EventArgs.Empty);
}

internal void NotifyChangesCanceled()
{
if (ChangesCanceled != null && AreNotificationsEnabled())
ChangesCanceled(this, EventArgs.Empty);
}

internal void NotifyKeyGenerated(Key key)
{
if (KeyGenerated!=null && AreNotificationsEnabled())
Expand Down