Skip to content

Commit

Permalink
Merge pull request #59 from clintirving/add-transaction-scope-to-casc…
Browse files Browse the repository at this point in the history
…ade-delete

Add transaction scope to cascade delete
  • Loading branch information
clintirving authored Jan 22, 2025
2 parents 12a852a + bec2aa8 commit 631624a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
2 changes: 1 addition & 1 deletion EfYou/EfYou.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.27</Version>
<Version>1.0.28</Version>
<AssemblyVersion>1.0.19.0</AssemblyVersion>
<FileVersion>1.0.19.0</FileVersion>
</PropertyGroup>
Expand Down
22 changes: 14 additions & 8 deletions EfYou/EntityServices/EntityServiceOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq;
using System.Transactions;
using EfYou.CascadeDelete;
using EfYou.DatabaseContext;
using EfYou.Extensions;
Expand Down Expand Up @@ -294,15 +295,20 @@ public virtual void Delete(List<dynamic> ids)

if (entitiesToDelete.Count != 0)
{
_cascadeDeletionService.CascadeDelete(entitiesToDelete.GetIdsFromEntities());

if (UseBulkDelete)
{
DeleteUsingBulkDelete(entitiesToDelete);
}
else
using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
DeleteUsingEntityFramework(entitiesToDelete);
_cascadeDeletionService.CascadeDelete(entitiesToDelete.GetIdsFromEntities());

if (UseBulkDelete)
{
DeleteUsingBulkDelete(entitiesToDelete);
}
else
{
DeleteUsingEntityFramework(entitiesToDelete);
}

transaction.Complete();
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions EfYouTests/EntityServices/EntityServiceOfTTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Transactions;
using EfYou.CascadeDelete;
using EfYou.DatabaseContext;
using EfYou.EntityServices;
Expand Down Expand Up @@ -727,6 +728,44 @@ public void Delete_ListOfIds_CallsCascadeDeleteWithFilteredEntities()
_cascadeDeleteService.Verify(x => x.CascadeDelete(It.Is<List<dynamic>>(y => y.Count == 1)));
}

/// <summary>
/// Helpful here: https://stackoverflow.com/a/26361464/2844982
/// </summary>
[TestMethod]
public void Delete_ListOfIds_EverythingIsExecutedInATransaction()
{
// Arrange
Transaction cascadeDeleteTransaction = null;
var cascadeDeleteTransactionCommitted = false;


var entities = new List<DummyEntity>
{
new DummyEntity {Id = 3},
new DummyEntity {Id = 4}
};
var accessibleIds = new List<dynamic> { 3 };
SetMockData(entities);
_filterService.Setup(x => x.FilterResultsOnGet(It.IsAny<IQueryable<DummyEntity>>(), It.IsAny<List<dynamic>>(), It.IsAny<IContext>()))
.Returns<IQueryable<DummyEntity>, List<dynamic>, IContext>((x, y, z) => x.Where(a => accessibleIds.Contains(a.Id)));

_cascadeDeleteService.Setup(x => x.CascadeDelete(It.IsAny<List<dynamic>>())).Callback(() =>
{
cascadeDeleteTransaction = Transaction.Current;
cascadeDeleteTransaction.TransactionCompleted += (sender, args) =>
cascadeDeleteTransactionCommitted =
args.Transaction.TransactionInformation.Status == TransactionStatus.Committed;
});

// Act
_entityService.Delete(new List<dynamic> { 3, 4 });

// Assert
Assert.IsNotNull(cascadeDeleteTransaction);
Assert.IsTrue(cascadeDeleteTransactionCommitted);

}

[TestMethod]
public void Delete_NoMatchingIds_DoesNotCallCascadeDelete()
{
Expand Down

0 comments on commit 631624a

Please sign in to comment.