Skip to content

Commit

Permalink
sup
Browse files Browse the repository at this point in the history
  • Loading branch information
oskogstad committed Sep 11, 2024
1 parent d10f1c1 commit 9fe2c38
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ internal static AggregateNode Create(Type type, object entity, AggregateNodeStat
/// Convenience method to check if the state of the node is <see cref="AggregateNodeState.Modified"/> and not by a child node.
/// </summary>
public bool IsDirectlyModified() => State is AggregateNodeState.Modified && !ModifiedByChild;

/// <summary>
/// Convenience method to check if the state of the node is <see cref="AggregateNodeState.Added"/>.
/// </summary>
public bool IsAdded() => State is AggregateNodeState.Added;

/// <summary>
/// Convenience method to check if the state of the node is <see cref="AggregateNodeState.Modified"/>.
/// </summary>
public bool IsModified() => State is AggregateNodeState.Modified;

/// <summary>
/// Convenience method to check if the state of the node is <see cref="AggregateNodeState.Restored"/>.
/// </summary>
public bool IsRestored() => State is AggregateNodeState.Restored;

/// <summary>
/// Convenience method to check if the state of the node is <see cref="AggregateNodeState.Deleted"/>.
/// </summary>
public bool IsDeleted() => State is AggregateNodeState.Deleted;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,61 +24,31 @@ internal static async Task HandleAggregateEntities(this ChangeTracker changeTrac

foreach (var (_, aggregateNode) in aggregateNodeByEntry)
{
if (aggregateNode.Entity is IAggregateCreatedHandler created && aggregateNode.State is AggregateNodeState.Added)
if (aggregateNode.Entity is IAggregateCreatedHandler created && aggregateNode.IsAdded())
{
created.OnCreate(aggregateNode, utcNow);
}

if (aggregateNode.Entity is IAggregateUpdatedHandler updated && aggregateNode.State is AggregateNodeState.Modified)
if (aggregateNode.Entity is IAggregateUpdatedHandler updated && aggregateNode.IsModified())
{
updated.OnUpdate(aggregateNode, utcNow);
}


























if (aggregateNode.Entity is IAggregateDeletedHandler deleted && aggregateNode.State is AggregateNodeState.Deleted)
if (aggregateNode.Entity is IAggregateDeletedHandler deleted && aggregateNode.IsDeleted())
{
deleted.OnDelete(aggregateNode, utcNow);
}

if (aggregateNode.Entity is IAggregateRestoredHandler restored && aggregateNode.State is AggregateNodeState.Restored)
if (aggregateNode.Entity is IAggregateRestoredHandler restored && aggregateNode.IsRestored())
{
restored.OnRestore(aggregateNode, utcNow);
}

if (aggregateNode.Entity is IUpdateableEntity updatable && aggregateNode.State is AggregateNodeState.Modified)
if (aggregateNode.Entity is IUpdateableEntity updatable)
{
updatable.Update(utcNow);
}

if (aggregateNode.Entity is IUpdateableEntity createdUpdatable && aggregateNode.State is AggregateNodeState.Added)
{
if (createdUpdatable.UpdatedAt == default)
if (aggregateNode.IsModified() || (aggregateNode.IsAdded() && updatable.UpdatedAt == default))
{
createdUpdatable.Update(utcNow);
updatable.Update(utcNow);
}
}

Expand All @@ -89,6 +59,7 @@ internal static async Task HandleAggregateEntities(this ChangeTracker changeTrac
}
}


internal static ModelBuilder AddAggregateEntities(this ModelBuilder modelBuilder)
{
// This will eager load entire aggregate trees for ALL queries unless explicitly
Expand Down

0 comments on commit 9fe2c38

Please sign in to comment.