RSCG – AutoGen
RSCG – AutoGen
name | AutoGen |
nuget | https://www.nuget.org/packages/Antelcat.AutoGen/ |
link | ,https://github.com/Antelcat/AutoGen |
author | Feast Antelcat |
Generating function to map DTOs
This is how you can use AutoGen .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net8.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <PropertyGroup> <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath> </PropertyGroup> <ItemGroup> <PackageReference Include="Antelcat.AutoGen" Version="1.0.0-pre-alpha-7" /> </ItemGroup> </Project>
The code that you will use is
using mapperDemo; var p=new Person(); p.FirstName = "Andrei"; p.LastName = "Ignat"; PersonDTO dto= p.ToDTO(); Console.WriteLine(dto.FullName);
public class Person { public int ID { get; set; } public string? FirstName { get; set; } public string? LastName { get; set; } }
using Antelcat.AutoGen.ComponentModel.Mapping; namespace mapperDemo; public class PersonDTO { public int ID { get; set; } public string? FirstName { get; set; } public string? LastName { get; set; } [MapIgnore] public string FullName { get { return FirstName + " " + LastName; } } }
using Antelcat.AutoGen.ComponentModel.Mapping; namespace mapperDemo; public static partial class Extensions { [AutoMap(Extra = [nameof(AfterMap)])] public static partial PersonDTO ToDTO(this Person person); private static void AfterMap(Person person, PersonDTO personDTO) { person.ID= personDTO.ID; } }
The code that is generated is
// <auto-generated/> By Antelcat.AutoGen #pragma warning disable #nullable enable namespace mapperDemo { partial class Extensions { [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Antelcat.AutoGen.SourceGenerators.Generators.Mapping.MapperGenerator", "1.0.0.0")] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute] public static partial global::mapperDemo.PersonDTO ToDTO(this global::Person person) { var ret = new global::mapperDemo.PersonDTO() { ID = person.ID, FirstName = person.FirstName, LastName = person.LastName, }; global::mapperDemo.Extensions.AfterMap(person, ret); return ret; } } }
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/AutoGen
Leave a Reply