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

Ignore nested Properties #395

Closed
AceHack opened this issue May 1, 2023 · 4 comments
Closed

Ignore nested Properties #395

AceHack opened this issue May 1, 2023 · 4 comments

Comments

@AceHack
Copy link

AceHack commented May 1, 2023

Is your feature request related to a problem? Please describe.
I would like to have an array like syntax to avoid nested properties

@latonz
Copy link
Contributor

latonz commented May 1, 2023

Could you elaborate a bit more on your use case? What exactly do you need this functionality for?

@TimothyMakkison
Copy link
Collaborator

TimothyMakkison commented May 1, 2023

The problem
Mapperly currently supports defining nested mapping properties, however it does not support ignoring nested members. In some cases this can break logic or create unwanted warnings that cannot be suppressed via mapperly.

// Here we want to ignore sources Id and UnmappedMember, 
// the target Id will be set via a different explicit mapping and UnmappedMember does not have a corresponding member.
public class NestedChild
{
    // Other members...
    public int Id { get; set; }

    public string UnmappedMember { get; set; }
}
public class NestedChildDto
{
    // Other members
    public Guid Id { get; set; }
}

// Example of nested explicit mapping
[MapProperty("Manufacturer.MyID", "Producer.Id"] // Map property with a different name in the target type
public static partial CarDto MapCarToDto(Car car);

Describe the solution you'd like
MapperIgnoreTarget and MapperIgnoreSource should be updated to support a params string similar to [MapProperty]. MappingBodyBuilders should be updated to support nested Ignore. This will likely use some recursion where each step knows its current member handle so it knows which nested ignores it corresponds to.

[MapperIgnoreSource("Producer.Id")]
//or
[MapperIgnoreSource(nameof(CarDto.Producer), nameof(ProducerDto.Id))]

Notes
I suspect this hasn't been done because it could break the resuing of similar mapping methods. If two mappings share a member type but one ingores a property, the other mapping may reuse this mapping resulting in s subtle bug. When a mapping delegate is created it should record what configurations were used to create it. When checking if a mapper already exists this can be used to ensure that a compatible mapper is reused. [MapProperty] appears to avoid this by creating the object and then updating properties via accessible setters.
I haven't confirmed if this would be a problem. It would require a lot of work to fix 😄

// Here MapToTailDto is created where Bushiness is not mapped over
[MapperIgnoreTarget("Tail.Bushiness")]
public static partial CatDto Map(Cat car);

// MapToTailDto is reused and Bushiness will not be mapped
public static partial DogDto Map(Dog car);

This would likely have to be done when doing #103 and #349 as all require reworking the mapping logic

@AceHack
Copy link
Author

AceHack commented May 1, 2023

I'll go ahead and close this. I got everything working. Thanks for this wonderful project. I'm using an asp.net core web api with EF, EF Migrations, Mediatr, and this project. Makes for a very slick experience. Code first, all the way.

@AceHack AceHack closed this as completed May 1, 2023
@TimothyMakkison
Copy link
Collaborator

TimothyMakkison commented May 1, 2023

I'll go ahead and close this. I got everything working.

What was your work around? I thought you made this issue after reading #392

Thanks for this wonderful project. I'm using an asp.net core web api with EF, EF Migrations, Mediatr, and this project. Makes for a very slick experience. Code first, all the way.

You should check out Mediator it's a source generated version of Mediatr.

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

No branches or pull requests

3 participants