Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ollie Storey 17 posts 172 karma points
    Sep 28, 2020 @ 20:10
    Ollie Storey
    0

    UmbracoMapper Nested Mapping

    I am trying to accomplish nested mapping but I cant inject UmbracoMapper into my MappingDefinition.

    Boot Failed!

    Hopefully, this is a simple one - not sure if it is possible, might not be...

  • Ollie Storey 17 posts 172 karma points
    Sep 29, 2020 @ 13:47
    Ollie Storey
    101

    Answer is Current.Mapper.Map<> for anyone looking at this later

    See documentation

  • AJ Murray 13 posts 122 karma points
    Jul 24, 2024 @ 08:24
    AJ  Murray
    0

    I found that this works too.

    Map definitions are one for the outer order, and one for a line.

    public void DefineMaps(IUmbracoMapper mapper)
    {
        mapper.Define<OrderReadOnly, PostOrderDto>((source, context) => new PostOrderDto(), Map);
        mapper.Define<OrderLineReadOnly, LineItem>((source, context) => new LineItem(), MapLine);
    }
    

    Then the Map method itself for the order contains a mapping for the line items.

    private void Map(OrderReadOnly source, PostOrderDto target, MapperContext context)
    {
    ...
    target.LineItems = context.MapEnumerable<OrderLineReadOnly, LineItem>(source.OrderLines);
    }
    

    Slightly different to the previous answer as it uses the context value that is passed in.

Please Sign in or register to post replies

Write your reply to:

Draft