Skip to main content

MagicMap by Bramer Daniel

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: MagicMap

Helper utility for generating type mapper classes and more

Author: Bramer Daniel

NuGet: https://www.nuget.org/packages/MagicMap/

You can find more details at https://github.com/bramerdaniel/MagicMap

Source : https://github.com/bramerdaniel/MagicMap

Original Readme

note

MagicMap

SourceGenerator based package for generating boilerplate code like object mappers

This is still under construction

Mapping code from one class to another

public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}

public class PersonModel
{
public string Name { get; set; }
public int Age { get; set; }
}

The required code to write

[TypeMapper(typeof(Person), typeof(PersonModel))]
internal partial class PersonMapper { }

Usage

var source = new Person { Name = "Peter", Age = 34 };
var model = source.ToPersonModel();

About

note

Auto mapping

How to use

Example ( source csproj, source files )

This is the CSharp Project that references MagicMap

<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="MagicMap" Version="1.0.0-alpha.10" OutputItemType="Analyzer" />
</ItemGroup>


</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

//------------------------------------------------
// <auto-generated>
// Generated by the MagicMap source generator
// </auto-generated>
//------------------------------------------------
namespace mapperlyDemo
{
[global::System.Runtime.CompilerServices.CompilerGenerated]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute]
partial class PersonMapper
{
/// <summary>
/// The default singleton instance of the generated type mapper.
/// To customize the creation of the default mapper, just implement this property in the defining partial part.
/// </summary>
public static PersonMapper Default { get; } = new PersonMapper();


/// <summary>Maps all properties of the <see cref = "source"/> to the properties of the <see cref = "target"/></summary>
public void Map(global::Person source, global::mapperlyDemo.PersonDTO target)
{
target.ID = source.ID;
target.FirstName = source.FirstName;
target.LastName = source.LastName;
MapOverride(source, target);
}

internal global::mapperlyDemo.PersonDTO MapFrom(global::Person source)
{
var target = Default is MagicMap.ITypeFactory<global::mapperlyDemo.PersonDTO, global::Person> factory ? factory.Create(source) : new global::mapperlyDemo.PersonDTO();
Default.Map(source, target);
return target;
}

/// <summary>Maps all properties of the <see cref = "source"/> to the properties of the <see cref = "target"/></summary>
public void Map(global::mapperlyDemo.PersonDTO source, global::Person target)
{
target.ID = source.ID;
target.FirstName = source.FirstName;
target.LastName = source.LastName;
MapOverride(source, target);
}

internal global::Person MapFrom(global::mapperlyDemo.PersonDTO source)
{
var target = Default is MagicMap.ITypeFactory<global::Person, global::mapperlyDemo.PersonDTO> factory ? factory.Create(source) : new global::Person();
Default.Map(source, target);
return target;
}

/// <summary>Implement this method, to map the properties the mapper could not handle for any reason.</summary>
partial void MapOverride(global::Person source, global::mapperlyDemo.PersonDTO target);

/// <summary>Implement this method, to map the properties the mapper could not handle for any reason.</summary>
partial void MapOverride(global::mapperlyDemo.PersonDTO source, global::Person target);

}
}

Usefull

Download Example (.NET C# )

Share MagicMap

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

In the same category (Mapper) - 7 other generators

AutoDTO

AutoGen

DynamicsMapper

LightweightObjectMapper

mapperly

MapTo

NextGenMapper