. I know I can a" /> . I know I can a" /> . I know I can a"/>

Create a custom mapping method to map a property using Riok.Mapperly

531 Views Asked by At

Using Riok.Mapperly and C#, I need to map a property of type string that contains values like this "1_2_4_8" and map it to a List<int>. I know I can add a method to the mapping class for Mapperly to pick, like so:

[Mapper]
public static partial class CountryMapper
{
    public static partial M.Country ToBoCountry(this EF.Country country);
    private static List<int> SplitUnderscore(string? str)
    {
        return str is null ? [] : [.. str.Split('_').Where(x => 
            !string.IsNullOrEmpty(x)).Select(int.Parse)];
    }
}

This way if an other property in the County class has a string that has the values like this "1|2|4|8", I cannot create another method like this:

private static List<int> SplitPipe(string? str)
{
    return str is null ? [] : [.. str.Split('|').Where(x => 
        !string.IsNullOrEmpty(x)).Select(int.Parse)];
}

It will just pick the first it finds.

Is there a way to instruct mapperly what method to use to map a specific property?

1

There are 1 best solutions below

0
latonz On BEST ANSWER

This is not yet possible with Mapperly. What you need is #783. A PR implementing this is open and will probably be merged in a few weeks. You can subscribe to the issue to get updates as they arrive.