-
Notifications
You must be signed in to change notification settings - Fork 334
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
Struggling with mapping readonly ICollection property #689
Comments
@akordowski I have the same issue. Did you ever find a solution? This project is starting to look abandoned — no commits since September 2023, and no release since then. And issues opened are not answered... Is it time to move on to a different mapper? |
This is how I solved the problem. It's far from being an optimal solution but it works. It was a special case, but it might give you an idea. Hope it helps you. public class SrcClassMapping : IRegister
{
public void Register(TypeAdapterConfig config)
{
config
.ForType<DestClass, ICollection<SrcClass>>()
.MapWith(src => MapCollection(src, null))
.MapToTargetWith((src, dest) => MapCollection(src, dest));
}
private static ICollection<SrcClass> MapCollection(
DestClass? destObj,
ICollection<SrcClass>? srcCollection)
{
srcCollection ??= new List<SrcClass>();
if (destObj is null)
return srcCollection;
// Custom mapping
return srcCollection;
}
} |
@akordowski Great, thanks! Will give it a try |
In order to identify issues that are still active, we are closing issues that we believe are either resolved or are dormant. If your issue is still active then please reopen. Thanks. |
@stagep Please reopen, as I can only comment. Thank you. |
Hello @akordowski. Destination Class does it really look like you indicated?
it doesn't have any additional constructors? |
Yes, the destination class looks exactly like this and don't have any constructors. |
Ok, I'm starting to research this problem. |
Properties without a setter are not updated . Your code even gets built in and works. But the result is not assigned anywhere.
This Working because here your function specified in the Map() condition is simply called and the result is returned.
|
@DocSvartz If I understand this correctly, the issue is that the values in Thumbnails (which is a read-only list) should be mapped automatically from the source collection. I'm looking at the test for UseDestinationValue, and it appears to work as intended. And that test is in principle the same use case as this issue. The difference being that this issue is configured using TypeAdapterConfig. The test only uses the [UseDestinationValue] attribute, and as far as I can tell there are no tests that uses the TypeAdapterConfig method. |
@andrerav For this example Doesn't work even if you use |
@andrerav
|
Mapping Function For the last example
|
Yes, I see. Could this work with MapToTargetWith() instead? |
I haven't checked, but if the result is passed to it
Then the collection mapping mechanism itself must be implemented by the user |
Right. So the workaround is MapToTargetWith() (and I see that this is the workaround @akordowski suggested earlier in this thread). But the ideal solution is for Mapster to detect UseDestinationValue in the logic for MapWith so that the collection returned from the lambda is copied into the destination collection? |
Although maybe I'm already confused :) |
@andrerav I think yes, that's what was meant UseDestinationValue == .Invoke (MapToWith, $result.Thumbnails) |
Discussed in #688
Originally posted by akordowski March 24, 2024
Hi everyone!
I am currently struggling with the mapping of a readonly ICollection property. I followed the instructions but I can't make it work. The readonly collection is not populated with the mapped content. What am I making wrong? Have anyone an idea how I can make it work?
Here the code:
Programm.cs
ThumbnailMapping.cs
Channel.cs (Destination)
Thumbnail.cs (Destination)
Channel.cs (Source)
ThumbnailDetails.cs (Source)
Thumbnail.cs (Source)
The text was updated successfully, but these errors were encountered: