public class Source
{
public int Id { get; set; }
public string Name { get; set; }
public IList Children { get; set; }
}
public class Destination
{
public int Id { get; set; }
public string Name { get; set; }
public IList Children { get; set; }
}
public class SourceChild
{
public int Id { get; set; }
public string Name { get; set; }
}
public class DestinationChild
{
public int Id { get; set; }
public string Name { get; set; }
}
// AutoMapper.Configurations.cs
public static class AutoMapperConfigurations
{
public static void Configure()
{
Mapper.Initialize(config =>
{
config.CreateMap();
config.CreateMap()
.ForMember(dest => dest.Children,
opt => opt
.MapFrom(src => src.Children)
.ConvertUsing(new ChildConverter()));
});
}
}
public class ChildConverter : ITypeConverter, IList>
{
public IList Convert(IList source, IList destination, ResolutionContext context)
{
var dest = destination ?? new List();
dest.Clear();
foreach (var srcChild in source)
{
var destChild = context.Mapper.Map(srcChild);
destChild.ParentId = context.Parent.ParentDtoId; // assuming DestinationChild has a ParentId property
dest.Add(destChild);
}
return dest;
}
}