سؤال

Here's my DTO:

public class DiaryEventType_dto
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string  Group { get; set; }
    public bool Redundant { get; set; }
    public string Type { get; set; }
}

And it maps to two possible entity types:

public partial class UserDiaryEventType
{
    public System.Guid Id { get; set; }
    public string Name { get; set; }
    public string TypeGroup { get; set; }
    public bool Redundant { get; set; }
}

public partial class SystemDiaryEventType
{
    public System.Guid Id { get; set; }
    public string Name { get; set; }
    public string TypeGroup { get; set; }
    public bool Redundant { get; set; }
}

The "Type" property is meant to distinguish which type the DTO was originally mapped from (why would I want to do this rather than have two separate DTO classes? Legacy code, that's why - too much pain to change it all).

Ideally I'd like to populate this during automapping, otherwise the mapper will throw a wobbler at me because "Type" isn't mapped:

        Mapper.CreateMap<Entities.UserDiaryEventType, DiaryEventType_dto>()
            .ForMember(m => m.Group, o => o.MapFrom(s => s.TypeGroup));
        Mapper.CreateMap<DiaryEventType_dto, Entities.UserDiaryEventType>()
            .ForMember(m => m.TypeGroup, o => o.MapFrom(s => s.Group));
        Mapper.CreateMap<Entities.SystemDiaryEventType, DiaryEventType_dto>()
            .ForMember(m => m.Group, o => o.MapFrom(s => s.TypeGroup));
        Mapper.CreateMap<DiaryEventType_dto, Entities.SystemDiaryEventType>()
            .ForMember(m => m.TypeGroup, o => o.MapFrom(s => s.Group));

But I can't figure out the syntax for doing so. Something like:

//pseudo code
Mapper.CreateMap<DiaryEventType_dto, Entities.UserDiaryEventType>()
     .SetValue("User");
Mapper.CreateMap<DiaryEventType_dto, Entities.SystemDiaryEventType>()
     .SetValue("System");

Is it possible?

هل كانت مفيدة؟

المحلول

ResolveUsing lets you use a custom value or calculation.

Mapper.CreateMap<Entities.UserDiaryEventType, DiaryEventType_dto>()
        .ForMember(m => m.Group, o => o.MapFrom(s => s.TypeGroup))
        .ForMember(m => m.Group, o => o.ResolveUsing(s => "User"));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top