Skip to content

Commit

Permalink
added test: CRUD aggregate store now correctly deletes aggregates whe…
Browse files Browse the repository at this point in the history
…n the action also triggers the removal of other related entities
  • Loading branch information
martinzima committed Oct 12, 2022
1 parent b8e08ff commit 9e5b2b6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<VersionPrefix>1.30.1</VersionPrefix>
<VersionPrefix>1.30.2</VersionPrefix>
</PropertyGroup>

<PropertyGroup>
Expand Down
5 changes: 5 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# RELEASE NOTES

## [1.30.2] - 2022-10-12

### Fixed
- CRUD aggregate store now correctly deletes aggregates when the action also triggers the removal of other related entities

## [1.30.1] - 2022-06-16

### Added
Expand Down
3 changes: 1 addition & 2 deletions Revo.Infrastructure/Repositories/CrudAggregateStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ protected void InjectClassIds()

protected void RemoveDeletedEntities()
{
var aggregates = GetAttachedAggregates().ToList();
foreach (var aggregate in aggregates)
foreach (var aggregate in GetAttachedAggregates().ToArray())
{
if (aggregate.IsDeleted)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,35 @@ public async Task SaveChanges_RemovesDeleted()
crudRepository.GetEntityState(testAggregate).Should().Be(EntityState.Detached);
}

[Fact]
public async Task SaveChanges_RemovesDeletedAndDependencies()
{
// some providers like EF Core might remove other related entities (e.g. owned collections) when removing an aggregate,
// modifying the internal collection - RemoveDeletedEntities needs to materialize the iterated collection of to-be-deleted aggregates first

var testAggregate = new TestAggregate(Guid.NewGuid());
crudRepository.Add(testAggregate);

var testEntity = new TestEntity(Guid.NewGuid());
crudRepository.Add(testEntity);
await crudRepository.SaveChangesAsync();

crudRepository.When(x => x.Remove(testAggregate))
.Do(ci =>
{
crudRepository.Remove(testEntity);
crudRepository.SaveChanges();
});

testAggregate = await sut.GetAsync<TestAggregate>(testAggregate.Id);
testAggregate.Delete();

await sut.SaveChangesAsync();

crudRepository.GetEntityState(testAggregate).Should().Be(EntityState.Detached);
crudRepository.GetEntityState(testEntity).Should().Be(EntityState.Detached);
}

[Fact]
public async Task SaveChanges_FindNull()
{
Expand Down Expand Up @@ -269,6 +298,17 @@ public void Delete()
}
}

public class TestEntity : BasicEntity
{
public TestEntity(Guid id) : base(id)
{
}

protected TestEntity()
{
}
}

public class Event1 : DomainAggregateEvent
{
}
Expand Down

0 comments on commit 9e5b2b6

Please sign in to comment.