Having an AggregateRoot and a list of child Entities, how do you persist the updated list of children after removing or updating one of them ?
This is an application layer service
async Task HandleAsync(RemoveChildRequest request)
{
Aggregate aggregate = await _aggregateRepository.GetByIdAsync(request.AggregateId);
aggregate.RemoveChild(request.ChildId);
await _aggregateRepository.Update(aggregate);
await _unitOfWork.CommitAsync();
}
This is the Aggregate method of removing a child.
public virtual void RemoveChild(Guid ChildId)
{
Child kid = _children.Single(item => item.Id == ChildId);
_children.Remove(kid);
}
And this is the repository The aggregate is as it should be, has same data but without the child it was removed from the collection.
Update(Aggregate aggregate)
{
await Session.UpdateAsync(aggregate, aggregate.Id);
}
This is my NHibernate configuration
mapping
.HasMany<Children>(Reveal.Member<Aggregate>("Children"))
.Not.Inverse()
.Not.KeyNullable()
.Not.KeyUpdate()
.Cascade.Delete();
After the commit is done, there is no update done against the DB. Somehow i feel is normal because, I only remove an entry from the children collection and that's all.
The structure
Aggregate
{
private virtual IList<Child> _children;
protected virtual List<Child> Children { get => _children; }
}
Child
{
}
So only the parent holds a reference to the Child
I could do something like this in the Aggregate Repository
RemoveChild(Child kid)
{
Session.DeleteAsync(kid);
}
But as far as I know, Repositories should be Aggregates specific only.
I'm interested in how the code that will actually persist the changes to the data store looks like? How do you remove the child. The Repository.