简体   繁体   中英

How to Map List Values to String using Automapper?

This is my Dto Model Object

public class SourceClass
{
    public List<SourceList> SourceLists { get; set; }
}

public class SourceList
{
    public bool Type1 { get; set; }
    public bool Type2 { get; set; }
    public bool Type3 { get; set; }
    public decimal Value { get; set; }
}

I am mapping to my ViewModel

public class DestinationClass
{
    public string Value1 { get; set; }
    public string Value2 { get; set; }
    public string Value3 { get; set; }

}

Using my own Resolver Method. The aim is to Map the values to the List to ViewModel Values if the Dto List has corresponding bool as true.

For example, If the list contain has IsValue1 as true, then the viewModel Value1 need to be updated with the Value coming from IsValue1.

It currently is updating values of all the properties in the viewModel with the same value ie Value1, Value2 and Value3 has the same value of value1. As it looks for the condition IsValue1 which is true in the dto unless I remove the value.

public class Resolver : ValueResolver<SourceClass, string>
{
    protected override string ResolveCore(SourceClass source)
    {
        if (source.SourceLists.Any())
        {
            foreach (var sourceList in source.SourceLists)
            {
                //if the source list Type1 is true, add the value of the list to the value1
                if (sourceList.Type1.Equals(true))
                {
                    var value1 = sourceList.Value.ToString(CultureInfo.InvariantCulture);
                    return value1;
                }

                if (sourceList.Type2.Equals(true))
                {
                    var value1 = sourceList.Value.ToString(CultureInfo.InvariantCulture);
                    return value1;
                }

                if (sourceList.Type3.Equals(true))
                {
                    var value1 = sourceList.Value.ToString(CultureInfo.InvariantCulture);
                    return value1;
                }
            }
        }
        return String.Empty;
    }
}

The implementation of the code is in this way:

CreateMap<SourceClass, DestinationClass>()
  .ForMember(m => m.Value1, o => o.ResolveUsing<Resolver>())
  .ForMember(m => m.Value2, o => o.ResolveUsing<Resolver>())
  .ForMember(m => m.Value3, o => o.ResolveUsing<Resolver>());

I could change the code and use individual mapper to correct this however this will in turn defeat the aim of using Automapper.

The way I have resolved this problem is removing the value everytime the condition is satisfied. I have used the RemoveAt function to do it.

My solution

  public class Resolver : ValueResolver<SourceClass, string>
   {
    protected override string ResolveCore(SourceClass source)
    {
        var dc = new DestinationClass();
        if (source.SourceLists.Any())
        {
            foreach (var sourceList in source.SourceLists)
            {
                //if the source list Type1 is true, add the value of the list to the value1
                if (sourceList.Type1.Equals(true))
                {
                    dc.Value1 = sourceList.Value.ToString(CultureInfo.InvariantCulture);
                    source.SourceLists.RemoveAt(0);
                    return dc.Value1;
                }

                if (sourceList.Type2.Equals(true))
                {
                    dc.Value2 = sourceList.Value.ToString(CultureInfo.InvariantCulture);
                    source.SourceLists.RemoveAt(0);
                    return dc.Value2;
                }

                if (sourceList.Type3.Equals(true))
                {
                    dc.Value3 = sourceList.Value.ToString(CultureInfo.InvariantCulture);
                    source.SourceLists.RemoveAt(0);
                    return dc.Value3;
                }
            }
        }
        return String.Empty;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM