question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for destination value in user implemented mapping methods

See original GitHub issue

Is 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:open
  • Created 6 months ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
EniacMlezicommented, Mar 28, 2023

Maybe fixing #103 would also cover this?

1reaction
latonzcommented, Mar 17, 2023

I think your use case could be implemented by using the after map feature with an ignored property:

[Mapper]
public partial class MyMapper
{
    public void MapPersonToPersonDto(Person person, PersonDto destination)
    {
        MapPersonToPersonDtoInternal(person, destination);
        destination.Age = CustomAgeMapping(person, destination);
    }

    [MapperIgnoreTarget(nameof(PersonDto.Age))]
    private partial void MapPersonToPersonDtoInternal(Person person, PersonDto destination)
}

Would this work for your use case?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Value Resolvers
Custom Value Resolvers¶. Although AutoMapper covers quite a few destination member mapping scenarios, there are the 1 to 5% of destination values that...
Read more >
Automapper: passing parameter to Map method
I'm using Automapper in a project and I need to dynamically valorize a field of my destination object. In my configuration I have...
Read more >
Pass data between destinations
In the Navigation editor, click on the destination that receives the argument. · In the Attributes panel, click Add (+). · In the...
Read more >
IncludeMembers and Custom Projections with AutoMapper
In this article, get ready to dive into custom projections with AutoMapper, using IncludeMembers and Value and Type Converters.
Read more >
Getting Started with AutoMapper in ASP.NET Core
We use the CreateMap() method to create a mapping by providing the source and destination properties. If we want to customize the configuration ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found