Skip to main content

MapItEasy by Phong Nguyen

NuGet / site data​

Nuget GitHub last commit GitHub Repo stars

Details​

Info​

info

Name: MapItEasy

Package Description

Author: Phong Nguyen

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

You can find more details at https://github.com/phongnguyend/MapItEasy/

Source: https://github.com/phongnguyend/MapItEasy/

Author​

note

Phong Nguyen Alt text

Original Readme​

note

MapItEasy​

Simple and fast object mapper (using Expression Trees API and Source Generators) to map data between 2 objects which have identical (or nearly identical) shapes.

Use Cases​
  • Cloning.
  • Data archiving (moving data from the active table to the archived table).
Installation​

Install the package from NuGet:

dotnet add package MapItEasy

Or using the NuGet Package Manager in Visual Studio:

Install-Package MapItEasy
Getting Started​

######### Using IMapper

IMapper mapper = new ExpressionMapper();
// or
IMapper mapper = new ReflectionMapper();

########## Map all properties (return new object)

var source = new A \{ Id = 1, Name = "abc1", Description = "xyz1" };

var target = mapper.Map<A, B>(source);

########## Map all properties (existing object)

var source = new A \{ Id = 1, Name = "abc1", Description = "xyz1" };
var target = new B();

mapper.Map(source, target);

########## Map only selected properties

var source = new A \{ Id = 1, Name = "abc1", Description = "xyz1" };
var target = new B();

mapper.Map(source, target, new MappingOptions<A> \{ Include = x => new \{ x.Name \} });
// target.Id == 0, target.Name == "abc1", target.Description == null

########## Map all properties except selected ones

var source = new A \{ Id = 1, Name = "abc1", Description = "xyz1" };
var target = new B();

mapper.Map(source, target, new MappingOptions<A> \{ Exclude = x => new \{ x.Name \} });
// target.Id == 1, target.Name == null, target.Description == "xyz1"

Note: Include and Exclude cannot be used together. Doing so will throw an InvalidOperationException.

######### Using Extension Methods

MapperExtensions provides convenient extension methods using ExpressionMapper under the hood:

var source = new A \{ Id = 1, Name = "abc1", Description = "xyz1" };

// Return new object
var target = source.Map<A, B>();

// Map to existing object
var target2 = new B();
source.Map(target2);

// With options
source.Map(target2, new MappingOptions<A> \{ Include = x => new \{ x.Name \} });
Source Generator​

For zero-reflection, compile-time mapping, install the source generator package:

dotnet add package MapItEasy.Generators

Or using the NuGet Package Manager in Visual Studio:

Install-Package MapItEasy.Generators

Define partial methods decorated with the [GeneratedMapping] attribute, and the source generator will provide the implementation at compile time:

using MapItEasy;

public static partial class MappingExtensions
{
// Return a new mapped object
[GeneratedMapping]
public static partial B MapToB(A source);

// Map to an existing object
[GeneratedMapping]
public static partial void MapToB(A source, B target);

// Extension method - return a new mapped object
[GeneratedMapping]
public static partial B ToB(this A source);

// Extension method - map to an existing object
[GeneratedMapping]
public static partial void ToB(this A source, B target);

// With MappingOptions support
[GeneratedMapping]
public static partial B MapToB(A source, MappingOptions<A>? options = null);

[GeneratedMapping]
public static partial void MapToB(A source, B target, MappingOptions<A>? options = null);
}

Usage:

var source = new A \{ Id = 1, Name = "abc1", Description = "xyz1" };

// Static call
var target = MappingExtensions.MapToB(source);

// Extension method call
var target2 = source.ToB();

// Map to existing object via extension method
var target3 = new B();
source.ToB(target3);

// With MappingOptions
var target4 = MappingExtensions.MapToB(source, new MappingOptions<A> \{ Include = x => new \{ x.Name \} });
License​

MapItEasy is licensed under the MIT license.

About​

note

Mapping from an object to another object

How to use​

Example (source csproj, source files)​

This is the CSharp Project that references MapItEasy

<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="MapItEasy" Version="2.0.0" />
<PackageReference Include="MapItEasy.Generators" Version="2.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>

Generated Files​

Those are taken from $(BaseIntermediateOutputPath)\GX

// <auto-generated />
#nullable enable

namespace mapperDemo;

public static partial class UserMapper
{
public static partial global::mapperDemo.PersonDTO MapUser(global::Person from)
{
if (from == null)
throw new global::System.ArgumentNullException(nameof(from));

var target = new global::mapperDemo.PersonDTO();

target.FirstName = from.FirstName;
target.LastName = from.LastName;

return target;
}

}

Useful​

Download Example (.NET C#)​

Share MapItEasy​

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

Category "Mapper" has the following generators:​

1 AutoDTO Nuget GitHub Repo stars 2023-08-24

2 AutoGen Nuget GitHub Repo stars 2024-02-22

3 DynamicsMapper Nuget GitHub Repo stars 2023-10-16

4 Facet Nuget GitHub Repo stars 2025-08-17

5 LightweightObjectMapper Nuget GitHub Repo stars 2024-09-18

6 lomapper Nuget GitHub Repo stars 2026-04-07

7 MagicMap Nuget GitHub Repo stars 2023-10-08

8 MapItEasy Nuget GitHub Repo stars 2026-09-10

9 mapperly Nuget GitHub Repo stars 2023-04-16

10 MapTo Nuget GitHub Repo stars 2023-10-05

11 Najlot.Map.SourceGenerator Nuget GitHub Repo stars 2026-08-29

12 NextGenMapper Nuget GitHub Repo stars 2023-08-16

See category​

Mapper