mapperly icon indicating copy to clipboard operation
mapperly copied to clipboard

Add samples

Open CommonGuy opened this issue 3 years ago • 3 comments

Provide some complete mapper samples. Samples are a great way to get started for newer users or to check how specific features can be used.

CommonGuy avatar Feb 14 '22 14:02 CommonGuy

Also, it would be nice to see some source code you generate, since one of the claims is that Mapperly is even faster than the naive manual mapping.

samusaran avatar Feb 28 '22 13:02 samusaran

An example snippet of generated code:

public partial ObjectsMapperBenchmark.SpotifyAlbum? Map(ObjectsMapperBenchmark.SpotifyAlbumDto? spotifyAlbumDto)
{
    if (spotifyAlbumDto == null)
        return default;
    var target = new ObjectsMapperBenchmark.SpotifyAlbum();
    target.AlbumType = spotifyAlbumDto.AlbumType;
    if (spotifyAlbumDto.Artists != null)
    {
        target.Artists = MapToArtistArray(spotifyAlbumDto.Artists);
    }

    // ...
    
    return target;
}

private ObjectsMapperBenchmark.Artist? MapToArtist(ObjectsMapperBenchmark.ArtistDto? source)
{
    if (source == null)
        return default;
    var target = new ObjectsMapperBenchmark.Artist();
    if (source.ExternalUrls != null)
    {
        target.ExternalUrls = MapToExternalUrls(source.ExternalUrls);
    }

    target.Href = source.Href;
    target.Id = source.Id;
    target.Name = source.Name;
    target.Type = source.Type;
    target.Uri = source.Uri;
    return target;
}

private ObjectsMapperBenchmark.Artist[] MapToArtistArray(ObjectsMapperBenchmark.ArtistDto[] source)
{
    var target = new ObjectsMapperBenchmark.Artist[source.Length];
    for (var i = 0; i < source.Length; i++)
    {
        target[i] = MapToArtist(source[i]);
    }

    return target;
}

Mapperly is only faster than naive manual mapping approaches (eg. https://github.com/mjebrahimi/Benchmark.netCoreMappers/blob/master/ObjectsMapperBenchmark/ManuallyMapper.cs). The main performance factor is the LINQ usage.

Mapperly cannot beat an optimized, manually written mapping method, but developers are often lazy and write sub-optimal code. We'll add samples (both of the usage and the generated code) to our documentation.

CommonGuy avatar Feb 28 '22 14:02 CommonGuy

Damn, you just gave me a reality check :| I'm one of those devs that maps objects like the example in ManyallyMapper.cs.

samusaran avatar Feb 28 '22 14:02 samusaran

:tada: This issue has been resolved in version 2.6.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Jan 12 '23 10:01 github-actions[bot]