Special handling properties of the floating material in some but not all comparisons AutoMapper

I just started with AutoMapper in C #. I successfully created this mapping:

Mapper.CreateMap<InputTypeA, OutputTypeA>() 

I also found a way to add some logic to certain properties, such as date format (in InputTypeA) in a row in a particular format (OutputTypeA).

 .ForMember( dest => dest.MyDateProperty, opt => opt.ResolveUsing( src => String.Format("{0:yyyy-MM-dd}", src.MyDateProperty))); 

Now I need to do the same for a number of properties of the float, but I wonder if there is a short / easy way to do it, with the exception of copying the code, similar to the above, for each property that you want to follow this rule.

I have found that I can create a new map, like this, to display float in the line:

 Mapper.CreateMap<float,string>() .ConvertUsing(src => String.Format(CultureInfo.InvariantCulture.NumberFormat, "{0:0.00}", src)); 

It works, but it is too general, because I also have a comparison to other types of (let it causes InputTypeB), which also contains the properties of the float, which must be treated differently.

 Mapper.CreateMap<InputTypeB, OutputTypeB>() 

How can I make only part of the display of floating point only for the first show?

+6
source share
2 answers

You can create two separate resolver on the basis of two separate configurations, only one of which includes a comparison of the float-to-string:

 public class InputTypeA { public float Foo { get; set; } } public class OutputTypeA { public string Foo { get; set; } } public class InputTypeB { public float Bar { get; set; } } public class OutputTypeB { public string Bar { get; set; } } public class Program { public static void Main(string[] args) { Func<float, string> mapFunc = src => String.Format(CultureInfo.InvariantCulture.NumberFormat, "{0:0.00}", src); var floatToStringConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeA, OutputTypeA>(); cfg.CreateMap<float, string>().ConvertUsing(mapFunc); }); var regularConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeB, OutputTypeB>(); }); IMapper floatToStringMapper = floatToStringConfig.CreateMapper(); IMapper regularMapper = regularConfig.CreateMapper(); var aIn = new InputTypeA() { Foo = 1f }; var aOut = floatToStringMapper.Map<OutputTypeA>(aIn); Console.WriteLine(aOut.Foo); // prints "1.00" var bIn = new InputTypeB() { Bar = 1f }; var bOut = regularMapper.Map<OutputTypeB>(bIn); Console.WriteLine(bOut.Bar); // prints "1" } } get; public class InputTypeA { public float Foo { get; set; } } public class OutputTypeA { public string Foo { get; set; } } public class InputTypeB { public float Bar { get; set; } } public class OutputTypeB { public string Bar { get; set; } } public class Program { public static void Main(string[] args) { Func<float, string> mapFunc = src => String.Format(CultureInfo.InvariantCulture.NumberFormat, "{0:0.00}", src); var floatToStringConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeA, OutputTypeA>(); cfg.CreateMap<float, string>().ConvertUsing(mapFunc); }); var regularConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeB, OutputTypeB>(); }); IMapper floatToStringMapper = floatToStringConfig.CreateMapper(); IMapper regularMapper = regularConfig.CreateMapper(); var aIn = new InputTypeA() { Foo = 1f }; var aOut = floatToStringMapper.Map<OutputTypeA>(aIn); Console.WriteLine(aOut.Foo); // prints "1.00" var bIn = new InputTypeB() { Bar = 1f }; var bOut = regularMapper.Map<OutputTypeB>(bIn); Console.WriteLine(bOut.Bar); // prints "1" } } get; public class InputTypeA { public float Foo { get; set; } } public class OutputTypeA { public string Foo { get; set; } } public class InputTypeB { public float Bar { get; set; } } public class OutputTypeB { public string Bar { get; set; } } public class Program { public static void Main(string[] args) { Func<float, string> mapFunc = src => String.Format(CultureInfo.InvariantCulture.NumberFormat, "{0:0.00}", src); var floatToStringConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeA, OutputTypeA>(); cfg.CreateMap<float, string>().ConvertUsing(mapFunc); }); var regularConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeB, OutputTypeB>(); }); IMapper floatToStringMapper = floatToStringConfig.CreateMapper(); IMapper regularMapper = regularConfig.CreateMapper(); var aIn = new InputTypeA() { Foo = 1f }; var aOut = floatToStringMapper.Map<OutputTypeA>(aIn); Console.WriteLine(aOut.Foo); // prints "1.00" var bIn = new InputTypeB() { Bar = 1f }; var bOut = regularMapper.Map<OutputTypeB>(bIn); Console.WriteLine(bOut.Bar); // prints "1" } } get; public class InputTypeA { public float Foo { get; set; } } public class OutputTypeA { public string Foo { get; set; } } public class InputTypeB { public float Bar { get; set; } } public class OutputTypeB { public string Bar { get; set; } } public class Program { public static void Main(string[] args) { Func<float, string> mapFunc = src => String.Format(CultureInfo.InvariantCulture.NumberFormat, "{0:0.00}", src); var floatToStringConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeA, OutputTypeA>(); cfg.CreateMap<float, string>().ConvertUsing(mapFunc); }); var regularConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeB, OutputTypeB>(); }); IMapper floatToStringMapper = floatToStringConfig.CreateMapper(); IMapper regularMapper = regularConfig.CreateMapper(); var aIn = new InputTypeA() { Foo = 1f }; var aOut = floatToStringMapper.Map<OutputTypeA>(aIn); Console.WriteLine(aOut.Foo); // prints "1.00" var bIn = new InputTypeB() { Bar = 1f }; var bOut = regularMapper.Map<OutputTypeB>(bIn); Console.WriteLine(bOut.Bar); // prints "1" } } get; public class InputTypeA { public float Foo { get; set; } } public class OutputTypeA { public string Foo { get; set; } } public class InputTypeB { public float Bar { get; set; } } public class OutputTypeB { public string Bar { get; set; } } public class Program { public static void Main(string[] args) { Func<float, string> mapFunc = src => String.Format(CultureInfo.InvariantCulture.NumberFormat, "{0:0.00}", src); var floatToStringConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeA, OutputTypeA>(); cfg.CreateMap<float, string>().ConvertUsing(mapFunc); }); var regularConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeB, OutputTypeB>(); }); IMapper floatToStringMapper = floatToStringConfig.CreateMapper(); IMapper regularMapper = regularConfig.CreateMapper(); var aIn = new InputTypeA() { Foo = 1f }; var aOut = floatToStringMapper.Map<OutputTypeA>(aIn); Console.WriteLine(aOut.Foo); // prints "1.00" var bIn = new InputTypeB() { Bar = 1f }; var bOut = regularMapper.Map<OutputTypeB>(bIn); Console.WriteLine(bOut.Bar); // prints "1" } } { public class InputTypeA { public float Foo { get; set; } } public class OutputTypeA { public string Foo { get; set; } } public class InputTypeB { public float Bar { get; set; } } public class OutputTypeB { public string Bar { get; set; } } public class Program { public static void Main(string[] args) { Func<float, string> mapFunc = src => String.Format(CultureInfo.InvariantCulture.NumberFormat, "{0:0.00}", src); var floatToStringConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeA, OutputTypeA>(); cfg.CreateMap<float, string>().ConvertUsing(mapFunc); }); var regularConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeB, OutputTypeB>(); }); IMapper floatToStringMapper = floatToStringConfig.CreateMapper(); IMapper regularMapper = regularConfig.CreateMapper(); var aIn = new InputTypeA() { Foo = 1f }; var aOut = floatToStringMapper.Map<OutputTypeA>(aIn); Console.WriteLine(aOut.Foo); // prints "1.00" var bIn = new InputTypeB() { Bar = 1f }; var bOut = regularMapper.Map<OutputTypeB>(bIn); Console.WriteLine(bOut.Bar); // prints "1" } } > public class InputTypeA { public float Foo { get; set; } } public class OutputTypeA { public string Foo { get; set; } } public class InputTypeB { public float Bar { get; set; } } public class OutputTypeB { public string Bar { get; set; } } public class Program { public static void Main(string[] args) { Func<float, string> mapFunc = src => String.Format(CultureInfo.InvariantCulture.NumberFormat, "{0:0.00}", src); var floatToStringConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeA, OutputTypeA>(); cfg.CreateMap<float, string>().ConvertUsing(mapFunc); }); var regularConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeB, OutputTypeB>(); }); IMapper floatToStringMapper = floatToStringConfig.CreateMapper(); IMapper regularMapper = regularConfig.CreateMapper(); var aIn = new InputTypeA() { Foo = 1f }; var aOut = floatToStringMapper.Map<OutputTypeA>(aIn); Console.WriteLine(aOut.Foo); // prints "1.00" var bIn = new InputTypeB() { Bar = 1f }; var bOut = regularMapper.Map<OutputTypeB>(bIn); Console.WriteLine(bOut.Bar); // prints "1" } } ; public class InputTypeA { public float Foo { get; set; } } public class OutputTypeA { public string Foo { get; set; } } public class InputTypeB { public float Bar { get; set; } } public class OutputTypeB { public string Bar { get; set; } } public class Program { public static void Main(string[] args) { Func<float, string> mapFunc = src => String.Format(CultureInfo.InvariantCulture.NumberFormat, "{0:0.00}", src); var floatToStringConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeA, OutputTypeA>(); cfg.CreateMap<float, string>().ConvertUsing(mapFunc); }); var regularConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeB, OutputTypeB>(); }); IMapper floatToStringMapper = floatToStringConfig.CreateMapper(); IMapper regularMapper = regularConfig.CreateMapper(); var aIn = new InputTypeA() { Foo = 1f }; var aOut = floatToStringMapper.Map<OutputTypeA>(aIn); Console.WriteLine(aOut.Foo); // prints "1.00" var bIn = new InputTypeB() { Bar = 1f }; var bOut = regularMapper.Map<OutputTypeB>(bIn); Console.WriteLine(bOut.Bar); // prints "1" } } ConvertUsing (mapFunc).; public class InputTypeA { public float Foo { get; set; } } public class OutputTypeA { public string Foo { get; set; } } public class InputTypeB { public float Bar { get; set; } } public class OutputTypeB { public string Bar { get; set; } } public class Program { public static void Main(string[] args) { Func<float, string> mapFunc = src => String.Format(CultureInfo.InvariantCulture.NumberFormat, "{0:0.00}", src); var floatToStringConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeA, OutputTypeA>(); cfg.CreateMap<float, string>().ConvertUsing(mapFunc); }); var regularConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeB, OutputTypeB>(); }); IMapper floatToStringMapper = floatToStringConfig.CreateMapper(); IMapper regularMapper = regularConfig.CreateMapper(); var aIn = new InputTypeA() { Foo = 1f }; var aOut = floatToStringMapper.Map<OutputTypeA>(aIn); Console.WriteLine(aOut.Foo); // prints "1.00" var bIn = new InputTypeB() { Bar = 1f }; var bOut = regularMapper.Map<OutputTypeB>(bIn); Console.WriteLine(bOut.Bar); // prints "1" } } > public class InputTypeA { public float Foo { get; set; } } public class OutputTypeA { public string Foo { get; set; } } public class InputTypeB { public float Bar { get; set; } } public class OutputTypeB { public string Bar { get; set; } } public class Program { public static void Main(string[] args) { Func<float, string> mapFunc = src => String.Format(CultureInfo.InvariantCulture.NumberFormat, "{0:0.00}", src); var floatToStringConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeA, OutputTypeA>(); cfg.CreateMap<float, string>().ConvertUsing(mapFunc); }); var regularConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeB, OutputTypeB>(); }); IMapper floatToStringMapper = floatToStringConfig.CreateMapper(); IMapper regularMapper = regularConfig.CreateMapper(); var aIn = new InputTypeA() { Foo = 1f }; var aOut = floatToStringMapper.Map<OutputTypeA>(aIn); Console.WriteLine(aOut.Foo); // prints "1.00" var bIn = new InputTypeB() { Bar = 1f }; var bOut = regularMapper.Map<OutputTypeB>(bIn); Console.WriteLine(bOut.Bar); // prints "1" } } ; public class InputTypeA { public float Foo { get; set; } } public class OutputTypeA { public string Foo { get; set; } } public class InputTypeB { public float Bar { get; set; } } public class OutputTypeB { public string Bar { get; set; } } public class Program { public static void Main(string[] args) { Func<float, string> mapFunc = src => String.Format(CultureInfo.InvariantCulture.NumberFormat, "{0:0.00}", src); var floatToStringConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeA, OutputTypeA>(); cfg.CreateMap<float, string>().ConvertUsing(mapFunc); }); var regularConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeB, OutputTypeB>(); }); IMapper floatToStringMapper = floatToStringConfig.CreateMapper(); IMapper regularMapper = regularConfig.CreateMapper(); var aIn = new InputTypeA() { Foo = 1f }; var aOut = floatToStringMapper.Map<OutputTypeA>(aIn); Console.WriteLine(aOut.Foo); // prints "1.00" var bIn = new InputTypeB() { Bar = 1f }; var bOut = regularMapper.Map<OutputTypeB>(bIn); Console.WriteLine(bOut.Bar); // prints "1" } } Foo = 1f}; public class InputTypeA { public float Foo { get; set; } } public class OutputTypeA { public string Foo { get; set; } } public class InputTypeB { public float Bar { get; set; } } public class OutputTypeB { public string Bar { get; set; } } public class Program { public static void Main(string[] args) { Func<float, string> mapFunc = src => String.Format(CultureInfo.InvariantCulture.NumberFormat, "{0:0.00}", src); var floatToStringConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeA, OutputTypeA>(); cfg.CreateMap<float, string>().ConvertUsing(mapFunc); }); var regularConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeB, OutputTypeB>(); }); IMapper floatToStringMapper = floatToStringConfig.CreateMapper(); IMapper regularMapper = regularConfig.CreateMapper(); var aIn = new InputTypeA() { Foo = 1f }; var aOut = floatToStringMapper.Map<OutputTypeA>(aIn); Console.WriteLine(aOut.Foo); // prints "1.00" var bIn = new InputTypeB() { Bar = 1f }; var bOut = regularMapper.Map<OutputTypeB>(bIn); Console.WriteLine(bOut.Bar); // prints "1" } } Bar = 1f}; public class InputTypeA { public float Foo { get; set; } } public class OutputTypeA { public string Foo { get; set; } } public class InputTypeB { public float Bar { get; set; } } public class OutputTypeB { public string Bar { get; set; } } public class Program { public static void Main(string[] args) { Func<float, string> mapFunc = src => String.Format(CultureInfo.InvariantCulture.NumberFormat, "{0:0.00}", src); var floatToStringConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeA, OutputTypeA>(); cfg.CreateMap<float, string>().ConvertUsing(mapFunc); }); var regularConfig = new MapperConfiguration(cfg => { cfg.CreateMap<InputTypeB, OutputTypeB>(); }); IMapper floatToStringMapper = floatToStringConfig.CreateMapper(); IMapper regularMapper = regularConfig.CreateMapper(); var aIn = new InputTypeA() { Foo = 1f }; var aOut = floatToStringMapper.Map<OutputTypeA>(aIn); Console.WriteLine(aOut.Foo); // prints "1.00" var bIn = new InputTypeB() { Bar = 1f }; var bOut = regularMapper.Map<OutputTypeB>(bIn); Console.WriteLine(bOut.Bar); // prints "1" } } 
+1
source

You can create custom converters values ​​for each event that you need to handle. Then apply them to the appropriate members in your comparisons.

As an example, I need to display from TypeA to TypeB, I want to use a custom conversion DateB:

  public class TypeA { public DateTime DateA { get; set; } public DateTime DateB { get; set; } } public class TypeB { public string DateA { get; set; } public string DateB { get; set; } } 

I create a custom resolver:

  class DateStringResolver : ValueResolver<DateTime, string> { protected override string ResolveCore(DateTime source) { return String.Format("{0:yyyy-MM-dd}", source); } } > {  class DateStringResolver : ValueResolver<DateTime, string> { protected override string ResolveCore(DateTime source) { return String.Format("{0:yyyy-MM-dd}", source); } } {  class DateStringResolver : ValueResolver<DateTime, string> { protected override string ResolveCore(DateTime source) { return String.Format("{0:yyyy-MM-dd}", source); } } 

Then in my configuration mapper:

  Mapper.CreateMap<TypeA, TypeB>() //Only Date B will use our custom resolver .ForMember(d => d.DateB, opt => opt.ResolveUsing<DateStringResolver>().FromMember(src => src.DateA)); 

Now, a resolver can be used wherever it is needed.

Documents: https://github.com/AutoMapper/AutoMapper/wiki/Custom-value-resolvers

+1
source

All Articles