Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 718
K Rajesh Kumar Avatar asked Jul 04 '15 12:07

K Rajesh Kumar


2 Answers

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;
}
like image 184
Thiago Sá Avatar answered Sep 18 '22 16:09

Thiago Sá


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.

like image 22
C Bauer Avatar answered Sep 19 '22 16:09

C Bauer