Skip to content

Support different collection mapping methods: Replace, Add, Update #665

Description

@cremor

Is your feature request related to a problem? Please describe.
It seems like Mapperly currently uses the target property setter visibility in an "existing target object" mapper (void method) to decide if a collection (ICollection<T>) is completely replaced or if the items in the collection are just added. This is confusing/dangerous because a simple visiblity change on the setter can lead to different behavior of the mapper.

Describe the solution you'd like
I'd like to configure if items are added to an existing collection or if a collection is replaced. One of the two possibilities should be the default for all collection properties, regardless of their setter visibility.
If the currently active setting is to replace the collection, but the target property has no visible setter, then the generated code should call Clear() on the collection before adding the items.

Describe alternatives you've considered
I know that I can call Clear() in a "before map" method. But the main problem here is that I might not even notice that I have to do this until I get a runtime bug.

Additional context
In theory this also affects mappers that return the mapped object, but that would only show a difference if the constructor/object factory already adds items to the collection.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions