Add mapping `Source Method Call` To `Target Property`
It seems that in some cases it would be convenient to have automatic conversion by calling a method on the source object, for example:
public record Model(int[] ErrorCodes)
{
public bool HasError()
{
return ErrorCodes.Any(x => x != 0);
}
}
public record Dto(int[] ErrorCodes, bool HasError);
[Mapper]
public partial class Mapper
{
public partial Dto Map(Model model);
}
could produce:
public partial class Mapper
{
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.1.1.0")]
public partial global::ConsoleApp1.Dto Map(global::ConsoleApp1.Model model)
{
var target = new global::ConsoleApp1.Dto(model.ErrorCodes,model.HasError());
return target;
}
}
or
public record Model(DateTime[] AllDates)
{
public DateTime GetLastDay()
{
return AllDates.Max();
}
}
public record Dto(DateTime LastDay);
[Mapper]
public partial class Mapper
{
public partial Dto Map(Model model);
}
could produce:
public partial class Mapper
{
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.1.1.0")]
public partial global::ConsoleApp1.Dto Map(global::ConsoleApp1.Model model)
{
var target = new global::ConsoleApp1.Dto(model.GetLastDay());
return target;
}
}
I understand that this can currently be solved with user mapping methods in the mapper, but it seems that sometimes this could be avoided with the proposed solution.
I think the rule by which this should work should be simple - it works when the method name ends with the target name
The 'magic' that Mapperly handles becomes more and more with every new feature. This would add additional complexity to Mapperly and add more magic (especially the variable prefix). I think for most of these mappings it can be solved quite simple by replacing the source method with a property getter (e.g. public DateTime LastDay => AllDates.Max()).
I think for most of these mappings it can be solved quite simple by replacing the source method with a property getter
This is true, but the source type is not always editable, it may be provided by library code. In our not very large project, we came across this only once, when we integrated with a third-party service. Based on the lack of similar requests, this is a very rare situation.
I think increasing complexity is a natural process for all evolving products. In any case, the decision is yours.