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

Struggling with mapping readonly ICollection property #689

Open
akordowski opened this issue Mar 29, 2024 Discussed in #688 · 0 comments
Open

Struggling with mapping readonly ICollection property #689

akordowski opened this issue Mar 29, 2024 Discussed in #688 · 0 comments

Comments

@akordowski
Copy link

akordowski commented Mar 29, 2024

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

var config = TypeAdapterConfig.GlobalSettings;
config.Scan(typeof(ChannelMapping).Assembly);
config.Default.UseDestinationValue(m => m.SetterModifier == AccessModifier.None &&
                                        m.Type.IsGenericType &&
                                        m.Type.GetGenericTypeDefinition() == typeof(ICollection<>));

var channelSrc = new MapsterTest.Objects.Source.Channel
{
    ChannelId = "123",
    Thumbnails = new MapsterTest.Objects.Source.ThumbnailDetails
    {
        Default = new MapsterTest.Objects.Source.Thumbnail
        {
            Url = "https://www.youtube.com/default.jpg"
        },
        Medium = new MapsterTest.Objects.Source.Thumbnail
        {
            Url = "https://www.youtube.com/medium.jpg"
        },
        High = new MapsterTest.Objects.Source.Thumbnail
        {
            Url = "https://www.youtube.com/high.jpg"
        }
    }
};

// Thumbnails are mapped correctly to a collection
var thumbnailsDest = channelSrc.Thumbnails.Adapt<ICollection<MapsterTest.Objects.Destination.Thumbnail>>().ToList();

// channelDest.Thumbnails collection is empty
var channelDest = channelSrc.Adapt<MapsterTest.Objects.Destination.Channel>();

ThumbnailMapping.cs

public class ThumbnailMapping : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.ForType<Objects.Source.ThumbnailDetails, ICollection<Objects.Destination.Thumbnail>>()
            .MapWith(src => MapThumbnailDetails(src).ToList());
    }

    private static IEnumerable<Objects.Destination.Thumbnail> MapThumbnailDetails(Objects.Source.ThumbnailDetails thumbnailDetails)
    {
        yield return MapThumbnail(thumbnailDetails.Default, "Default");
        yield return MapThumbnail(thumbnailDetails.Medium, "Medium");
        yield return MapThumbnail(thumbnailDetails.High, "High");
    }

    private static Objects.Destination.Thumbnail MapThumbnail(
        Objects.Source.Thumbnail thumbnail,
        string thumbnailType) =>
        new()
        {
            Type = thumbnailType,
            Url = thumbnail.Url.Trim(),
        };
}

Channel.cs (Destination)

public class Channel
{
    public string ChannelId { get; set; } = default!;
    public ICollection<Thumbnail> Thumbnails { get; } = new List<Thumbnail>();
}

Thumbnail.cs (Destination)

public class Thumbnail
{
    public string Type { get; set; } = default!;
    public string Url { get; set; } = default!;
}

Channel.cs (Source)

public class Channel
{
    public string ChannelId { get; set; } = default!;
    public ThumbnailDetails Thumbnails { get; set; } = default!;
}

ThumbnailDetails.cs (Source)

public class ThumbnailDetails
{
    public Thumbnail? Default { get; set; }
    public Thumbnail? Medium { get; set; }
    public Thumbnail? High { get; set; }
}

Thumbnail.cs (Source)

public class Thumbnail
{
    public string Url { get; set; } = default!;
}
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

1 participant