Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper: Map to collection from multiple source properties

Tags:

c#

automapper

UPDATE April 13th, 2018: Automapper 6.1.0 supports unflattening by introducing ReverseMap. See release notes here

I'm trying to use AutoMapper to unflatten an object.

I have a source as follows

public class Source
{
    public string Name {get;set;}
    public string Child1Property1 {get;set;}
    public string Child1Property2 {get;set;}
    public string Child2Property1 {get;set;}
    public string Child2Property2 {get;set;}
}

I want to map to this to destination

public class Destination
{
    public string Name {get;set;}
    public List<Child> Children {get;set;}
}

public class Child
{
    public string Property1 {get;set;}
    public string Property2 {get;set;}
}

My mapping configuration

public static class AutoMapperConfiguration
{
    public static MapperConfiguration Configure()
    {
        var config = new MapperConfiguration(
            cfg =>
            {
                cfg.CreateMap<Source, Destination>()
                    .ForMember(dest => dest.Children, /* What do I put here?*/))
                // I don't think this is correct
                cfg.CreateMap<Source, Child>()
                    .ForMember(dest => dest.Property1, opt => opt.MapFrom(src => src.Child1Property1))
                    .ForMember(dest => dest.Property2, opt => opt.MapFrom(src => src.Child1Property2))
                    .ForMember(dest => dest.Property1, opt => opt.MapFrom(src => src.Child2Property1))
                    .ForMember(dest => dest.Property2, opt => opt.MapFrom(src => src.Child2Property2));

            });
        return config;
    }
}

Now when I test my code I get using mapper.Map<List<Child>>(source) I get a AutoMapperMappingException: Missing type map configuration or unsupported mapping. Which make sense, since there isn't a mapping configured to a List<Child>. If I do mapper.Map<Child>(source), I get a Child instance with all null values for the properties.

I'm unfortunately not in a position to modify the Source class.

Is this possible at all with AutoMapper? and if so how?

like image 486
Hintham Avatar asked Oct 27 '16 09:10

Hintham


People also ask

Can AutoMapper map collections?

Polymorphic element types in collections AutoMapper supports polymorphic arrays and collections, such that derived source/destination types are used if found.

When should you not use AutoMapper?

If you have to do complex mapping behavior, it might be better to avoid using AutoMapper for that scenario. Reverse mapping can get very complicated very quickly, and unless it's very simple, you can have business logic showing up in mapping configuration.

How do I use AutoMapper to list a map?

How do I use AutoMapper? First, you need both a source and destination type to work with. The destination type's design can be influenced by the layer in which it lives, but AutoMapper works best as long as the names of the members match up to the source type's members.

Does AutoMapper map private properties?

AutoMapper will map property with private setter with no problem. If you want to force encapsulation, you need to use IgnoreAllPropertiesWithAnInaccessibleSetter. With this option, all private properties (and other inaccessible) will be ignored.


1 Answers

There are at least 2 options. You can use a simple extension method to simplify the mapping or you can create a custom type converter.

public class ConvertSourceToDestination : ITypeConverter<Source, Destination>
{
    public Destination Convert(Source source, Destination destination, ResolutionContext context)
    {
        destination = destination ?? new Destination();
        destination.Children = destination.Children ?? new List<Child>();
        destination.Children.Add(new Child() { Property1 = source.Child1Property1, Property2 = source.Child1Property2 });
        destination.Children.Add(new Child() { Property1 = source.Child2Property1, Property2 = source.Child2Property2 });
        destination.Name = source.Name;

        return destination;
    }
}

public static class SourceExtension
{
    public static IEnumerable<Child> Children(this Source source)
    {
        yield return new Child() { Property1 = source.Child1Property1, Property2 = source.Child1Property2 };
        yield return new Child() { Property1 = source.Child2Property1, Property2 = source.Child2Property2 };
    }

    public static MapperConfiguration CreateMapping()
    {
        var config = new MapperConfiguration(
            cfg =>
            {
                cfg.CreateMap<Source, Destination>()
                .ForMember(dest => dest.Children, opt => opt.MapFrom(src => src.Children()));
            });

        return config;
    }

    public static MapperConfiguration CreateMapping2()
    {
        var config = new MapperConfiguration(
            cfg =>
            {
                cfg.CreateMap<Source, Destination>().ConvertUsing(new ConvertSourceToDestination());

            });

        return config;
    }
}
like image 180
MikeJ Avatar answered Oct 18 '22 15:10

MikeJ