MapTo icon indicating copy to clipboard operation
MapTo copied to clipboard

Method-based conversion

Open Serg046 opened this issue 4 years ago • 1 comments

Currently you have to implement ITypeConverter in order to provide a conversion for types that cannot be implicitly casted. I find it slightly complicated. What if we allow converting based on methods? Something like this:

[MapFrom(typeof(User))]
public partial class UserViewModel
{
    public DateTimeOffset RegisteredAt { get; set; }
    
    [IgnoreProperty]
    public ProfileViewModel Profile { get; set; }
    
    [MapTypeConverter(nameof(ConvertKey))]
    [MapProperty(SourcePropertyName = nameof(User.Id))]    
    public string Key { get; }

    public static string ConvertKey(int source, object[] converterParameters) => $"{source:X}";

    /* This should be also allowed. If you don't set the parameter, you don't have to add it to the contract
    public static string ConvertKey(int source) => $"{source:X}";
    */
}

Serg046 avatar Jun 07 '21 16:06 Serg046

This was actually part of my plan to add to the library. Before adding this, I want to see if there is any better way of handling the converter scenario. If not, what you're suggesting is reasonable too.

mrtaikandi avatar Jun 08 '21 17:06 mrtaikandi