mapperly icon indicating copy to clipboard operation
mapperly copied to clipboard

Use UserMapping as global configuration

Open btbenjamin opened this issue 1 year ago • 1 comments

Hello every one

Is it possible to define a global configuration ?

I want to convert all Google.Protobuf.WellKnownTypes.Timestamp to DateTime as :

private DateTime? MapNullableDate(Google.Protobuf.WellKnownTypes.Timestamp date)
        => date?.ToDateTime();

Currently i should set my [UserMapping] in my partial class

[Mapper]
public partial class ProfileMapper
{
    /// <summary> Profile to grpc </summary>
    public partial ProfileBase GrpcProfileToProfile(GrpcProfile grpcProfile);


    [UserMapping]
    private DateTime MapDate(Google.Protobuf.WellKnownTypes.Timestamp date)
        => date?.ToDateTime() ?? new DateTime();


    [UserMapping]
    private DateTime? MapNullableDate(Google.Protobuf.WellKnownTypes.Timestamp date)
        => date?.ToDateTime();
}

But i want to set this config once, something like :

[Mapper]
public partial class ProfileMapper
{
    /// <summary> Profile to grpc </summary>
    public partial ProfileBase GrpcProfileToProfile(GrpcProfile grpcProfile);
}

public class GLOBALMapper
{
    [UserMapping]
    public DateTime MapDate(Google.Protobuf.WellKnownTypes.Timestamp date)
        => date?.ToDateTime() ?? new DateTime();


    [UserMapping]
    public DateTime? MapNullableDate(Google.Protobuf.WellKnownTypes.Timestamp date)
        => date?.ToDateTime();
}

Do you have some suggestion ?

Have nice day

btbenjamin avatar May 07 '24 09:05 btbenjamin

Either extend your GlobalMapper or use it.

latonz avatar May 07 '24 12:05 latonz