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

Ability to ignore source fields #137

Closed
mikart143 opened this issue May 5, 2019 · 3 comments
Closed

Ability to ignore source fields #137

mikart143 opened this issue May 5, 2019 · 3 comments
Assignees

Comments

@mikart143
Copy link

mikart143 commented May 5, 2019

I am not able to find any config which could help with my problem. I want to update object but i don't want to update target fields with null fields from source. I want to ignore source fields if they are null.

@SteveWilkes SteveWilkes self-assigned this May 7, 2019
@SteveWilkes
Copy link
Member

Hi!

There's no quick way to achieve this, but it seems like a sensible thing to support, so I'll add it in. It's not going to be quick to add though, but thanks for the suggestion!

Cheers,

Steve

@SteveWilkes
Copy link
Member

Implemention underway on this branch :)

@SteveWilkes
Copy link
Member

SteveWilkes commented Sep 14, 2019

Well - I said it wouldn't be quick!

This is supported as of v1.5, which is now on NuGet. Sources can be ignored like this:

Mapper.WhenMapping
    .Over<Order>()                    // Apply the filter to Order updates
    .IgnoreSources(c => c
        .If<string>(s => s == null)); // Ignore null source strings
Mapper.WhenMapping
    .From<OrderDto>()             // Apply when mapping from OrderDto
    .Over<Order>()                // Apply the ignore to Order updates (optional)
    .IgnoreSource(dto => dto.Id); // Ignore the Order.Id property

...or like this:

// Source, target and mapping types are implicit from the mapping:
Mapper.Map(orderDto).Over(order, cfg => cfg
    .If((dto, o) => dto.Id == 0) // Apply the ignores if OrderDto.Id is 0
    .IgnoreSource(
        dto => dto.Id,           // Ignore Order.Id...
        dto => dto.OrderNumber); // ...and Order.OrderNumber

...or like this:

Mapper.WhenMapping
    .IgnoreSourceMembersOfType<IDontMapMe>();

...and in other ways!

Thanks again for the suggestion :)

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

No branches or pull requests

2 participants