RSCG – mapperly

RSCG – mapperly
 
 

name mapperly
nuget https://www.nuget.org/packages/Riok.Mapperly/
link https://mapperly.riok.app/docs/getting-started/installation/
author Riok

Mapping classes to/from DTO

 

This is how you can use mapperly .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Riok.Mapperly" Version="2.8.0"  OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  </ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>

</Project>


The code that you will use is


// See https://aka.ms/new-console-template for more information
using mapperlyDemo;
var p=new Person();
p.FirstName = "Andrei";
p.LastName = "Ignat";
PersonMapper mapper = new() ;
var dto=mapper.Person2PersonDTO(p);
Console.WriteLine(dto.FullName);




public class Person
{
    public int ID { get; set; }
    public string? FirstName { get; set; }
    public string? LastName { get; set; }
}




namespace mapperlyDemo;

public class PersonDTO
{
    public int ID { get; set; }
    public string? FirstName { get; set; }
    public string? LastName { get; set; }

    public string FullName { 
        get
        {
            return FirstName + " " + LastName;
        }
    }
}



using Riok.Mapperly.Abstractions;

namespace mapperlyDemo;

[Mapper]
public partial class PersonMapper
{
    public partial PersonDTO Person2PersonDTO(Person p);
}

 

The code that is generated is

#nullable enable
namespace mapperlyDemo
{
    public partial class PersonMapper
    {
        public partial global::mapperlyDemo.PersonDTO Person2PersonDTO(global::Person p)
        {
            var target = new global::mapperlyDemo.PersonDTO();
            target.ID = p.ID;
            target.FirstName = p.FirstName;
            target.LastName = p.LastName;
            return target;
        }
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/mapperly