0

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.

  • Possible duplicate of [How to delete child object in NHibernate?](https://stackoverflow.com/questions/302720/how-to-delete-child-object-in-nhibernate) – guillaume31 Nov 23 '18 at 14:12

3 Answers3

1

Found my answer Here

nhibernate mapping: A collection with cascade="all-delete-orphan" was no longer referenced

and here

property Access strategies in nhibernate

NHibernate configuration

    mapping
        .HasMany<Child>(Reveal.Member<Order>("Children"))
        .Access.LowerCaseField(Prefix.Underscore)
        .Cascade.AllDeleteOrphan()
        .Not.KeyNullable()
        .Not.KeyUpdate();
0

Here is the way it is done with ByCode mapping, important is colmap.Cascade(Cascade.All). I hope this helps.

public class Client
{
    public virtual int ClientId { get; protected set; }
    public virtual string ClientName { get; protected set; }
    public virtual IList<ClientLocation> ClientLocations { get; protected set; }

    protected Client()
    {
        this.ClientLocations = new List<ClientLocation>();
    }
}

public class ClientLocation
{
    public virtual int ClientLocationId { get; protected set; }
    public virtual Client Client { get; protected set; }
    public virtual string LocationName { get; protected set; }

    protected ClientBranch()
    {
    }
}

Mappings

public class ClientMap : ClassMapping<Client>
{        
    public ClientMap() {
    Lazy(true);

        Id(x => x.ClientId, map => map.Generator(Generators.Identity));
    Property(x => x.ClientName);

        Bag(x => x.ClientLocations, colmap => { colmap.Key(x => x.Column("CLIENTID")); colmap.Cascade(Cascade.All); }, map => { map.OneToMany(); });
    }
}


public class ClientLocationMap : ClassMapping<ClientLocation>
{
    public ClientLocationMap()
    {
    Lazy(true);

        Id(x => x.ClientLocationId, map => map.Generator(Generators.Identity));
    Property(x => x.LocationName);

        ManyToOne(x => x.Client, map => { map.Column("CLIENTID"); map.NotNullable(true); map.Cascade(Cascade.All); });
    }
}
Raj
  • 166
  • 1
  • 11
-1

If the children belong to the aggregate root (i.e. composition instead of association), the removal or addition of the child entity must happen through the AggregateRoot and not independently. Moreover, children should be Value Objects and not aggregates in their own right.

Therefore, you are right - the Repository would only fetch the parent. You would then have RemoveChild command that would act on that instance and post a ChildRemoved event which would take the child away from the list.

Alessandro Santini
  • 1,943
  • 13
  • 17
  • 1
    @AlessandroSantini Internal aggregate objects can be entities, they don't have to be value objects. Also, the original question doesn't mention the use of commands or events. I think an answer shouldn't imply these. – guillaume31 Nov 23 '18 at 14:07