0

I am using automapper to map some objects between the database and another representation.

The entity looks something like

public class MyEntity {
    public int Id { get; set; }
    public Guid RowId { get; set; }
}  

public class MyObject {
    public Guid Id { get; set; }
}

As you can see, the names and types are unaligned.
Since I got many Entities and Objects, I'd rather not CreateMap<A, B>().ForMember(d => d.Id, mex => mex.MapFrom(s => s.RowId));.

To not having to do the above Convention:

AddMemberConfiguration()
    .AddMember<NameSplitMember>()
    .AddName<ReplaceName>(_ => _.AddReplace("RowId", "Id"));

This does not what I suspected it to do and I was not able to figure out, how to use the ReplaceName Convention. So I'd like to hear ideas about how to map that types.

MyEntity and MyObject both are base types, so I could also use that.

What I'm trying to archieve in pseudo-code:

if(source is MyEntity && target is MyObject)
{
     target.Id = source.RowId;
}

ForAllMembers
On recommendation of @lucian-bargaoanu I tried looking into ForAllMembers.
I did the following in the MapperProfile:

public class MapperProfile : Profile {
     public MapperProfile() {
         ForAllMaps(MapEntityBaseId);
     }

    protected void MapEntityBaseId(TypeMap map, IMappingExpression mex)
    {
        if (!map.SourceType.IsSubclassOf(typeof(EntityBase)))
            return;
        if (!map.DestinationType.IsSubclassOf(typeof(MyObject)))
            return;

        mex.ForMember("Id", opt => opt.MapFrom("RowId"));
    }
}

also the debugger hints me, that ForAllMember is executed as expected, it still fails the mapping.

I created a GIST for the ForAllMembers: https://gist.github.com/anonymous/511a1b69b795aa2bc7e7cd261fcb98b1

TGlatzer
  • 5,815
  • 2
  • 25
  • 46

0 Answers0