Add support for destination value in user implemented mapping methods
See original GitHub issueIs your feature request related to a problem? Please describe.
Currently, the User implemented mapping methods feature in Mapperly only allows users to provide a method body that maps a single source type to a single destination type. However, in some scenarios, it may be necessary to have more control over the mapping process, such as when mapping optional values or when the user wants to choose which value (source or destination) to use for the destination property.
Describe the solution you’d like
I propose adding support for destination values in the User implemented mapping methods feature. This would allow users to specify which value (source or destination) to use for the destination property when mapping between objects.
For example, the following code snippet demonstrates how this feature might be used to map an optional value:
MyMapper.cs
[Mapper]
public partial class MyMapper
{
public partial void MapPersonToPersonDto(Person source, PersonDto destination) { }
private static int? FromOptional<T>(Optional<int?> source, int? destination) => source.HasValue ? source.Value : destination;
}
MyMapper.g.cs
[Mapper]
public partial class MyMapper
{
public partial void MapPersonToPersonDto(Person source, PersonDto destination)
{
destination.Id = source.Id;
/* other mappings omitted for brevity */
destination.Age = FromOptional(source.Age, destination.Age);
}
}
With this change, users would have more control over how mappings are defined and could handle more complex mapping scenarios that are not possible with the current implementation.
Issue Analytics
- State:
- Created 6 months ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top GitHub Comments
Maybe fixing #103 would also cover this?
I think your use case could be implemented by using the after map feature with an ignored property:
Would this work for your use case?