It\'s not so much a question, as I have found a way to do what I want, but it seems like there should be a better way to do it. I\'ve searched everywhere and not found anything.
I don't like to have logic inside property getter or setter, but what about adding setter to Children property of Parent class
public class Parent
{
private readonly IList _children = new List();
public IEnumerable Children
{
get => _children;
set => AddChildren(value);
}
public void AddChild(Child child)
{
child.Parent = this;
_children.Add(child);
}
private void AddChildren(IEnumerable children)
{
_children.Clear();
foreach (var child in children)
{
AddChild(child);
}
}
}
And AfterMap logic is not more necessary. Mapping configuration will be simple
Mapper.CreateMap();