AutoMapper 5.2.0(also 6.02) starts acting differently from 5.1.0 when mapping from one class to another when the destination class "gets" is properties because of an interface

41 views
Skip to first unread message

RR

unread,
Mar 30, 2017, 12:47:32 AM3/30/17
to AutoMapper-users
Hi, we have two very simple classes (Aggregate and Entity), we want to map from Aggregate(Source) to Entity(destination) and the Entity(destination) implements an interface called IChangeTracking, the interface itself only "contains" getter for it's properties, but the Entity(destination) also contains a setter (no matter if the setter it's public, protected private or internal), now we have the following extension method which just contains a very simple mapping configuration and starting with 5.2.0 the Properties from Entity will no longer getting mapped using the extension method, if I add this mapping directly to CreateMap<Aggregate, Entity> everything is working, is this a bug or is this by design?

Br
Renè

  public static IMappingExpression<TSource, TDestination> ChangeTracking<TSource, TDestination>(this IMappingExpression<TSource, TDestination> mappingExpression)
            where TDestination : IChangeTrackingTest
        {
            if (mappingExpression == null)
                throw new ArgumentNullException(nameof(mappingExpression));

            mappingExpression
                .ForMember(destination => destination.CreatedBy, options =>
                                                                 {
                                                                     options.Condition((source, destination) => string.IsNullOrEmpty(destination.CreatedBy));
                                                                     options.MapFrom(source => "Name from identityFactory()");
                                                                 })
                .ForMember(destination => destination.CreatedAt, options =>
                                                                 {
                                                                     options.Condition((source, destination) => destination.CreatedAt == default(DateTime));
                                                                     options.MapFrom(source => DateTime.Now);
                                                                 });


            return mappingExpression;
        }

public class AutoMapperInit
    {
       
        public static IMapper Init()
        {
            MapperConfigurationExpression mapperConfigurationExpression = new MapperConfigurationExpression();
            mapperConfigurationExpression.AddProfile(new AutoMapperProfile());

            MapperConfiguration config = new MapperConfiguration(mapperConfigurationExpression);
            config.AssertConfigurationIsValid();

            return config.CreateMapper();
        }

 public class AutoMapperProfile : Profile
    {
            public AutoMapperProfile()
        {
            ShouldMapField = fieldInfo => true;
            ShouldMapProperty = propertyInfo => true;

            //works in 5.1.1 but doesn't work in 5.2.0 or 6.0.2 
            CreateMap<Aggregate, Entity>()
                .ChangeTracking(); //EXTENSION METHOD!

            //works in 5.1.1 and 5.2.0 with public setter and private setter, even if interface contains only property with get
            //CreateMap<Aggregate, Entity>()
            //    .ForMember(destination => destination.CreatedBy, options =>
            //                                                     {
            //                                                         options.Condition((source, destination) => string.IsNullOrEmpty(destination.CreatedBy));
            //                                                         options.MapFrom(source => "Name from identityFactory()");
            //                                                     })
            //    .ForMember(destination => destination.CreatedAt, options =>
            //                                                     {
            //                                                         options.Condition((source, destination) => destination.CreatedAt == default(DateTime));
            //                                                         options.MapFrom(source => DateTime.Now);
            //                                                     });

            CreateMap<Entity, Aggregate>()
                ;
        }


 public class Aggregate
    {
      

        public string Name
        {
            get;
            set;
        }

      
    }

public class Entity : IChangeTracking
    {
        public string Name
        {
            get;
            private set;
        }

      
        public string CreatedBy
        {
            get;
            set;
        }

        public DateTime CreatedAt
        {
            get;
            set;
        }
      
    }

    public interface IChangeTracking
    {
       
        string CreatedBy
        {
            get;
        }

        DateTime CreatedAt
        {
            get;
        }


Jimmy Bogard

unread,
Mar 30, 2017, 9:32:51 AM3/30/17
to automapper-users
Can you open a GH issue?

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages