MapTo
MapTo copied to clipboard
Method-based conversion
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}";
*/
}
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.