RSCG – MapTo
RSCG – MapTo
name | MapTo |
nuget | https://www.nuget.org/packages/MapTo/ |
link | https://github.com/mrtaikandi/MapTo |
author | Mohammedreza Taikandi |
AutoGenerate Mapping
This is how you can use MapTo .
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> <PropertyGroup> <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath> </PropertyGroup> <ItemGroup> <PackageReference Include="MapTo" Version="0.9.1" /> </ItemGroup> </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"; PersonDTO dto = p.MapToPersonDTO(); Console.WriteLine(dto.FullName);
public class Person { public int ID { get; set; } public string? FirstName { get; set; } public string? LastName { get; set; } }
using MapTo; namespace mapperlyDemo; [MapFrom(typeof(Person))] public class PersonDTO { public int ID { get; set; } public string? FirstName { get; set; } public string? LastName { get; set; } [IgnoreProperty] public string FullName { get { return FirstName + " " + LastName; } } }
The code that is generated is
// <auto-generated /> #nullable enable namespace mapperlyDemo; [global::System.CodeDom.Compiler.GeneratedCodeAttribute("MapTo", "0.9.1.51471")] public static class PersonMapToExtensions { [return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull("person")] public static global::mapperlyDemo.PersonDTO? MapToPersonDTO(this Person? person) { if (ReferenceEquals(person, null)) { return null; } return new PersonDTO { ID = person.ID, FirstName = person.FirstName, LastName = person.LastName }; } }
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/MapTo
Leave a Reply