Automapper, mapping single destination property as a concatenation of multiple source property

六眼飞鱼酱① 提交于 2019-12-10 04:35:48

问题


I have a situation where I need to map a single property as a combination of multiple source properties based on some conditions.

Destination :

public class Email
{
    public Email() {
        EmailRecipient = new List<EmailRecipient>();
    }
    public string Subject{get; set;}
    public string Body {get; set;}
    public virtual ICollection<EmailRecipient> EmailRecipient { get; set; } 
}

public class EmailRecipient
{
    public int EmaiId { get; set; }
    public string RecipientEmailAddress { get; set; }
    public int RecipientEmailTypeId { get; set; }
    public virtual Email Email { get; set; }
}

Source:

public class EmailViewModel
{
    public List<EmailRecipientViewModel> To { get; set; }
    public List<EmailRecipientViewModel> Cc { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
}

public class EmailRecipientViewModel
{
    public string RecipientEmailAddress { get; set; }
}

I want Mapper.Map<EmailViewModel,Email>()

Here I would like to map my Email.EmailRecipient as a combination of EmailViewModel.To and EmailViewModel.Cc. However the condition is, Email.EmailRecipient.RecipientEmailTypeId will be 1 for To and 2 for Cc

Hope my question is clear.


回答1:


One possible way to achieve this is to create a map that uses a specific method for this conversion. The map creation would be:

Mapper.CreateMap<EmailViewModel, Email>()
    .ForMember(e => e.EmailRecipient, opt => opt.MapFrom(v => JoinRecipients(v)));

Where the JoinRecipients method would perform the conversion itself. A simple implementation could be something like:

private ICollection<EmailRecipient> JoinRecipients(EmailViewModel viewModel) {
    List<EmailRecipient> result = new List<EmailRecipient>();
    foreach (var toRecipient in viewModel.To) {
        result.Add(new EmailRecipient {
            RecipientEmailTypeId = 1, 
            RecipientEmailAddress = toRecipient.RecipientEmailAddress
        });
    }

    foreach (var ccRecipient in viewModel.Cc) {
        result.Add(new EmailRecipient {
            RecipientEmailTypeId = 2,
            RecipientEmailAddress = ccRecipient.RecipientEmailAddress
        });
    }

    return result;
}



回答2:


I'm a huge opponent of converters, mostly because for other people in your project, things will just happen 'like magic' after the mapping call.

An easier way of handling this would be to implement the property as a method that converts other properties on the viewmodel to the required formatting. Example:

public class EmailViewModel
{
    public ICollection<EmailRecipient> EmailRecipient { 
        get {
             return To.Union(Cc);
        } 
    }
    public List<EmailRecipientViewModel> To { get; set; }
    public List<EmailRecipientViewModel> Cc { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
}

Now automapper automatically maps from EmailRecipient property to EmailRecipient property, and if someone is trying to figure out how it happens, they just need to look on the viewmodel.



来源:https://stackoverflow.com/questions/31220501/automapper-mapping-single-destination-property-as-a-concatenation-of-multiple-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!