Skip to content

Additional mapping parameters for derived mappings #2079

@danielwinkler

Description

@danielwinkler

The documentation clearly indicates the limitation of Additional mapping parameters for derived mappings

I would like to ask if you would consider a strict way where you would support it; e.g. you would copy the exact signature to the generated derived type mappings.

Example Mapper

using Riok.Mapperly.Abstractions;

namespace Medel.ReDi.Backend.UnitTests.Mappers;

public class MyBaseClass
{
    public int x;
    public double y;
    public string z;
}

public class MyDerivedDateClass : MyBaseClass
{
    public DateTime a;
}

public class MyDerivedGuidClass : MyBaseClass
{
    public Guid b;
}

public class MyBaseClassDTO
{
    public int x;
    public double y;
    public string z;
    public DateTime RetrievedAt;
}

public class MyDerivedDateClassDTO : MyBaseClassDTO
{
    public DateTime a;
}

public class MyDerivedGuidClassDTO : MyBaseClassDTO
{
    public Guid b;
}

[Mapper]
public static partial class MyMapper
{
    [MapDerivedType<MyDerivedDateClass, MyDerivedDateClassDTO>]
    [MapDerivedType<MyDerivedGuidClass, MyDerivedGuidClassDTO>]
    public static partial MyBaseClassDTO Map(MyBaseClass source, DateTime retrievedAt);
}

Generated Output

// <auto-generated />
#nullable enable
namespace Medel.ReDi.Backend.UnitTests.Mappers
{
    public static partial class MyMapper
    {
        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.3.0.0")]
        public static partial global::Medel.ReDi.Backend.UnitTests.Mappers.MyBaseClassDTO Map(global::Medel.ReDi.Backend.UnitTests.Mappers.MyBaseClass source, global::System.DateTime retrievedAt)
        {
            return source switch
            {
                global::Medel.ReDi.Backend.UnitTests.Mappers.MyDerivedDateClass x => MapToMyDerivedDateClassDTO(x),
                global::Medel.ReDi.Backend.UnitTests.Mappers.MyDerivedGuidClass x => MapToMyDerivedGuidClassDTO(x),
                _ => throw new global::System.ArgumentException($"Cannot map {source.GetType()} to Medel.ReDi.Backend.UnitTests.Mappers.MyBaseClassDTO as there is no known derived type mapping", nameof(source)),
            };
        }

        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.3.0.0")]
        private static global::Medel.ReDi.Backend.UnitTests.Mappers.MyDerivedDateClassDTO MapToMyDerivedDateClassDTO(global::Medel.ReDi.Backend.UnitTests.Mappers.MyDerivedDateClass source)
        {
            var target = new global::Medel.ReDi.Backend.UnitTests.Mappers.MyDerivedDateClassDTO();
            target.a = source.a;
            target.x = source.x;
            target.y = source.y;
            target.z = source.z;
            target.RetrievedAt = retrievedAt;
            return target;
        }

        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.3.0.0")]
        private static global::Medel.ReDi.Backend.UnitTests.Mappers.MyDerivedGuidClassDTO MapToMyDerivedGuidClassDTO(global::Medel.ReDi.Backend.UnitTests.Mappers.MyDerivedGuidClass source)
        {
            var target = new global::Medel.ReDi.Backend.UnitTests.Mappers.MyDerivedGuidClassDTO();
            target.b = source.b;
            target.x = source.x;
            target.y = source.y;
            target.z = source.z;
            target.RetrievedAt = retrievedAt;
            return target;
        }
    }
}

If the generated functions would be extended by , global::System.DateTime retrievedAt then the generated mappings would work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions