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

Make it possible to choose between "replace" und "add" for collections #665

Open
cremor opened this issue Aug 21, 2023 · 2 comments
Open
Labels
enhancement New feature or request

Comments

@cremor
Copy link

cremor commented Aug 21, 2023

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.

@CommonGuy CommonGuy added the enhancement New feature or request label Aug 21, 2023
@CommonGuy
Copy link
Contributor

Thanks for your feature request. I agree that this can be confusing and we'll think about the possible solutions to this problem

@cremor
Copy link
Author

cremor commented Aug 24, 2023

Maybe it would even be possible to create a "merge" option? That might have a higher complexity to implement, but could also be useful. I assume that would require either a way to specify which property is compared for the equals check, or user provided methods for the equals check.

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

No branches or pull requests

2 participants