Mappa.Generator

RSCG – Mappa.Generator
 
 

name Mappa.Generator
nuget https://www.nuget.org/packages/Mappa.Generator/
link https://github.com/sanelli/Mappa
author Stefano Anelli

Mapping from an object to another object

 

This is how you can use Mappa.Generator .

The code that you start with is


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

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

	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>

	<ItemGroup>
	  <PackageReference Include="Mappa" Version="10.3.0" />
	  <PackageReference Include="Mappa.Generator" Version="10.3.0">
	    <PrivateAssets>all</PrivateAssets>
	    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
	  </PackageReference>
	</ItemGroup>

</Project>


The code that you will use is


using mapperDemo;
var p=new Person();
p.FirstName = "Andrei";
p.LastName = "Ignat";
PersonDTO personDTO= UserMapper.MapUser(p);
Console.WriteLine(personDTO.FullName);




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





using Mappa.Attributes;

namespace mapperDemo;
public partial struct PersonDTO
{
    public string? FirstName { get; set; }
    public string? LastName { get; set; }

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

[Mappa]
public partial class UserMapper
{
    
    public static partial PersonDTO MapUser(Person from);
}

 

The code that is generated is

// <auto-generated>
//    <tool-name>Mappa</tool-name>
//    <tool-version>10.3.0.0</tool-version>
//    <timestamp>Sun, 01 Nov 2026 19:28:38 GMT</timestamp>
//    <note>This is an autogenerated file: edit to this file might be lost.</note>
// </auto-generated>

namespace mapperDemo;

public partial class UserMapper
{
   #nullable enable
   [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
   [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Mappa", "10.3.0.0")]
   public static partial mapperDemo.PersonDTO MapUser(Person from)
   {
      string? __mappa_tmp_1 = from.FirstName;
      string? __mappa_tmp_2 = from.LastName;
      mapperDemo.PersonDTO __mappa_tmp_3 = new mapperDemo.PersonDTO()
      {
         FirstName = __mappa_tmp_1,
         LastName = __mappa_tmp_2,
      };

      return __mappa_tmp_3;
   }
   #nullable restore
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Mappa.Generator


Posted

in

,

by

Tags: