MapTo 1.0.21

There is a newer version of this package available.
See the version list below for details.
dotnet add package MapTo --version 1.0.21                
NuGet\Install-Package MapTo -Version 1.0.21                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MapTo" Version="1.0.21" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MapTo --version 1.0.21                
#r "nuget: MapTo, 1.0.21"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MapTo as a Cake Addin
#addin nuget:?package=MapTo&version=1.0.21

// Install MapTo as a Cake Tool
#tool nuget:?package=MapTo&version=1.0.21                

MapTo

Nuget Publish Packages

A convention based object to object mapper using Roslyn source generator.

MapTo is a library that programmatically generates the necessary code to map one object to another during compile-time. It eliminates the need to use reflection to map objects and makes it much faster in runtime. It provides compile-time safety checks and ease of use by leveraging extension methods.

Installation

dotnet add package MapTo --prerelease

Usage

Unlike other libraries that require a separate class to define the mappings, MapTo uses attributes to define and instruct it on generating the mappings. To start, declare the target class and annotate it with the MapFrom attribute to specify the source class.

using MapTo;

namespace App.ViewModels;

[MapFrom(typeof(App.Data.Models.User))]
public class UserViewModel 
{
    public string FirstName { get; init; }

    public string LastName { get; init; }
    
    [IgnoreProperty]
    public string FullName { get; set; }
}

To get an instance of UserViewModel from the User class, you can use the generated extension method:

var user = new User(id: 10) { FirstName = "John", LastName = "Doe" };

var vm = user.MapToUserViewModel(); // A generated extension method for User class.

Sometimes, the target class (UserViewModel in this case) might have read-only properties that need to be set during the mapping. To do that, you can define the properties without setters and declare the target class as partial. Changing the class to partial will allow the MapTo generator to create the necessary constructor to initialize the read-only properties.

[MapFrom(typeof(App.Data.Models.User))]
public partial class UserViewModel 
{
    public int Id { get; }
    
    public string FirstName { get; init; }

    public string LastName { get; init; }
    
    [IgnoreProperty]
    public string FullName { get; set; }
}

Available Attributes

MapFrom

As mentioned above, this attribute is used to specify the source class. It also can be used to specify custom methods to run on before or after the mapping process.

[MapFrom(typeof(App.Data.Models.User), BeforeMap = nameof(RunBeforeMap), AfterMap = nameof(RunAfterMap))]
public partial class UserViewModel
{
    public int Id { get; }

    ...
    
    // The BeforeMap method can also return a `User` type. If so, 
    // the returned value will be used as the source object.
    // Or it can return `null` to skip the mapping process and return `null` to 
    // the extension method's caller.
    private static void RunBeforeMap(User? source) { /* ... */ }
    
    private static void RunAfterMap(UserViewModel target) { /* ... */ }
}

IgnoreProperty

By default, MapTo will include all properties with the same name (case-sensitive), whether read-only or not, in the mapping unless annotating them with the IgnoreProperty attribute.

[IgnoreProperty]
public string FullName { get; set; }

MapProperty

This attribute gives you more control over how the annotated property should get mapped. For instance, if the annotated property should use a property in the source class with a different name.

[MapProperty(From = "Id")]
public int Key { get; set; }

PropertyTypeConverter

A compilation error gets raised by default if the source and destination properties types are not implicitly convertible, but to convert the incompatible source type to the desired destination type, PropertyTypeConverterAttribute can be used.

This attribute will accept a static method in the target class or another class to convert the source type to the destination type. The method must have the following signature:

public static TDestination Convert(TSource source)

// or

public static TDestination Convert(TSource source, object[]? parameters)
[MapFrom(typeof(User))]
public partial class UserViewModel
{
    public DateTimeOffset RegisteredAt { get; set; }
    
    [IgnoreProperty]
    public ProfileViewModel Profile { get; set; }
    
    [MapProperty(From = nameof(User.Id))]    
    [PropertyTypeConverter(nameof(IntToHexConverter))]
    public string Key { get; }

    private static string IntToHexConverter(int source) => $"{source:X}"; // The converter method.
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.27 105 9/18/2024
1.0.25 112 9/17/2024
1.0.24 113 9/11/2024
1.0.23 90 8/29/2024
1.0.22 101 8/26/2024
1.0.21 105 8/26/2024
1.0.20 102 8/26/2024
1.0.19 122 8/24/2024
1.0.18 71 8/1/2024
1.0.17 66 8/1/2024
1.0.16 449 6/7/2024
1.0.15 102 6/7/2024
1.0.14 90 6/5/2024
1.0.13 106 6/4/2024
1.0.12 1,649 6/1/2024
1.0.11 100 6/1/2024
1.0.10 106 6/1/2024
1.0.9 88 6/1/2024
1.0.7 106 5/29/2024
1.0.6 111 5/15/2024
1.0.5 104 5/15/2024
1.0.4 96 5/15/2024
1.0.3 91 5/15/2024
1.0.2 92 5/14/2024
1.0.1 143 4/5/2024
0.9.23 316 1/3/2024
0.9.22 166 12/29/2023
0.9.14 187 12/2/2023
0.9.8 198 10/19/2023
0.9.7 158 10/16/2023
0.9.5 155 10/13/2023
0.9.3 145 10/9/2023
0.9.1 165 9/30/2023
0.7.20 788 7/29/2021
0.7.17 358 7/29/2021
0.7.16 378 7/28/2021
0.7.15 369 7/28/2021
0.7.14 421 7/3/2021
0.7.11 366 7/2/2021
0.7.9 354 7/1/2021
0.7.8 352 6/30/2021
0.7.4 340 6/23/2021
0.7.1 525 4/9/2021
0.6.3 367 3/26/2021
0.6.2 346 3/26/2021
0.6.1 407 2/21/2021
0.5.14 338 2/17/2021
0.5.11 348 2/7/2021
0.5.3 348 1/29/2021
0.5.1 385 1/25/2021