Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Special treatment for float properties in some but not all AutoMapper mappings

Tags:

c#

automapper

I've just started with AutoMapper in C#. I've succesfully created a mapping like this:

Mapper.CreateMap<InputTypeA, OutputTypeA>()

I've also found a way to add some logic to specific properties, like formatting a date (in InputTypeA) to a string in a specific format (in 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 float properties, but I'm wondering if there is a short/easy way to do this, except copying a piece of code like the one above for every property that needs to follow this rule.

I've found that I can create a new map like this for mapping floats to strings:

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

This works, but is too generic, because I also have a mapping for another type (let's call it InputTypeB), that also contains float properties, which need to be treated differently.

Mapper.CreateMap<InputTypeB, OutputTypeB>()

How can I make the float-to-string mapping part of the first mapping only?

like image 539
GolezTrol Avatar asked Mar 18 '14 15:03

GolezTrol


1 Answers

You could create two separate mappers based on two separate configurations, only one of which includes the float-to-string mapping:

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"
    }
}
like image 114
Rik Avatar answered Oct 19 '22 00:10

Rik