Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mapping Source Method Call To Target Property #1617

Open
TonEnfer opened this issue Dec 1, 2024 · 2 comments
Open

Add mapping Source Method Call To Target Property #1617

TonEnfer opened this issue Dec 1, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@TonEnfer
Copy link
Contributor

TonEnfer commented Dec 1, 2024

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

@TonEnfer TonEnfer added the enhancement New feature or request label Dec 1, 2024
@latonz
Copy link
Contributor

latonz commented Dec 2, 2024

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()).

@TonEnfer
Copy link
Contributor Author

TonEnfer commented Dec 3, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants