Skip to content

Commit

Permalink
Tracking AggregateRoot as modified when contained entity is being del…
Browse files Browse the repository at this point in the history
…eted
  • Loading branch information
marcwittke committed Sep 19, 2017
1 parent 36d9502 commit 1f72985
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Backend.Fx.EfCorePersistence/DbContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static void UpdateTrackingProperties(this DbContext dbContext, string use
int count = 0;
dbContext.ChangeTracker
.Entries<Entity>()
.Where(entry => entry.State == EntityState.Added || entry.State == EntityState.Modified)
.Where(entry => entry.State == EntityState.Added || entry.State == EntityState.Modified || entry.State == EntityState.Deleted)
.ForAll(entry =>
{
try
Expand All @@ -71,14 +71,22 @@ public static void UpdateTrackingProperties(this DbContext dbContext, string use
}
entity.SetCreatedProperties(userId, utcNow);
}
else
else if (entry.State == EntityState.Modified)
{
if (isTraceEnabled)
{
Logger.Trace("tracking that {0}[{1}] was modified by {2} at {3:T} UTC", entity.GetType().Name, entity.Id, userId, utcNow);
}
entity.SetModifiedProperties(userId, utcNow);
}
else if (entry.State == EntityState.Deleted)
{
if (isTraceEnabled)
{
Logger.Trace("tracking that {0}[{1}] was deleted by {2} at {3:T} UTC", entity.GetType().Name, entity.Id, userId, utcNow);
}
entity.SetDeleted(userId, utcNow);
}
}
catch (Exception ex)
{
Expand Down
19 changes: 19 additions & 0 deletions src/Backend.Fx/BuildingBlocks/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ public void SetModifiedProperties([NotNull] string changedBy, DateTime changedOn
}
}

public void SetDeleted([NotNull] string changedBy, DateTime changedOn)
{
if (changedBy == null)
{
throw new ArgumentNullException(nameof(changedBy));
}
if (changedBy == string.Empty)
{
throw new ArgumentException(nameof(changedBy));
}

// Deleting me results implicitly in a modification of the aggregate root.
AggregateRoot myAggregateRoot = FindMyAggregateRoot();
if (myAggregateRoot != this)
{
myAggregateRoot?.SetModifiedProperties(changedBy, changedOn);
}
}

protected abstract AggregateRoot FindMyAggregateRoot();

public bool Equals(Entity other)
Expand Down

0 comments on commit 1f72985

Please sign in to comment.