How to setup a mapping for a simple hierarchy with AutoMapper?

Hi everyone! I decided to publish this because I have just made a very basic toy application to clear my mind about the mapping hierarchies (using the Include method). I finally got it and to celebrate I’m posting the code. Of course, it uses AutoMapper, by Jimmy Bogard (thanks for your fantastic mapping library!).

It uses an extension method to ignore not mapped members in one of the levels of the hierarchy.

namespace MapperTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Mapper.CreateMap<GraphicsBase, BaseObject>()
                .ForMember(o => o.X1, expression => expression.MapFrom(b => b.Left))
                .Include<GraphicsRectBase, RectangleBase>()
                ;

            Mapper.CreateMap<GraphicsRectBase, RectangleBase>()
                .ForMember(o => o.Rx, expression => expression.MapFrom(b => b.CornerRadius))
                .Include<GraphicsField, Field>()
                .IgnoreAllNonExisting()
                ;

            Mapper.CreateMap<GraphicsField, Field>()
               .ForMember(o => o.Name, expression => expression.MapFrom(b => b.FieldName));

            var graphicsField = new GraphicsField { Left = 1, CornerRadius = 2, FieldName = "Field" };

            var baseObject = Mapper.Map(graphicsField);

            Mapper.AssertConfigurationIsValid();
        }
    }

    public static class AutoMapperExtensions
    {
        public static IMappingExpression<TSource, TDestination> IgnoreAllNonExisting<TSource, TDestination>(
            this IMappingExpression<TSource, TDestination> expression)
        {
            var sourceType = typeof(TSource); var destinationType = typeof(TDestination);
            var existingMaps = Mapper.GetAllTypeMaps().First(x => x.SourceType == sourceType && x.DestinationType == destinationType);
            foreach (var property in existingMaps.GetUnmappedPropertyNames())
            {
                expression.ForMember(property, opt => opt.Ignore());
            }
            return expression;
        }
    }

    public class GraphicsBase
    {
        public double Left { get; set; }
    }

    public class GraphicsRectBase : GraphicsBase
    {
        public double CornerRadius { get; set; }
    }

    public class GraphicsField : GraphicsRectBase
    {
        public string FieldName { get; set; }
    }

    public class BaseObject
    {
        public double X1 { get; set; }
    }

    public class RectangleBase : BaseObject
    {
        public double Rx { get; set; }
        public double NonExisting { get; set; }
    }

    public class Field : RectangleBase
    {
        public string Name { get; set; }
    }
}

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s