MapTo 1.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package MapTo --version 1.0.5                
NuGet\Install-Package MapTo -Version 1.0.5                
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.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MapTo --version 1.0.5                
#r "nuget: MapTo, 1.0.5"                
#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.5

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

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 95 9/18/2024
1.0.25 108 9/17/2024
1.0.24 109 9/11/2024
1.0.23 87 8/29/2024
1.0.22 98 8/26/2024
1.0.21 102 8/26/2024
1.0.20 99 8/26/2024
1.0.19 115 8/24/2024
1.0.18 68 8/1/2024
1.0.17 63 8/1/2024
1.0.16 349 6/7/2024
1.0.15 99 6/7/2024
1.0.14 87 6/5/2024
1.0.13 101 6/4/2024
1.0.12 1,405 6/1/2024
1.0.11 97 6/1/2024
1.0.10 103 6/1/2024
1.0.9 85 6/1/2024
1.0.7 103 5/29/2024
1.0.6 108 5/15/2024
1.0.5 101 5/15/2024
1.0.4 93 5/15/2024
1.0.3 88 5/15/2024
1.0.2 89 5/14/2024
1.0.1 140 4/5/2024
0.9.23 306 1/3/2024
0.9.22 161 12/29/2023
0.9.14 184 12/2/2023
0.9.8 195 10/19/2023
0.9.7 155 10/16/2023
0.9.5 152 10/13/2023
0.9.3 142 10/9/2023
0.9.1 162 9/30/2023
0.7.20 785 7/29/2021
0.7.17 355 7/29/2021
0.7.16 375 7/28/2021
0.7.15 366 7/28/2021
0.7.14 416 7/3/2021
0.7.11 363 7/2/2021
0.7.9 351 7/1/2021
0.7.8 349 6/30/2021
0.7.4 337 6/23/2021
0.7.1 522 4/9/2021
0.6.3 364 3/26/2021
0.6.2 341 3/26/2021
0.6.1 404 2/21/2021
0.5.14 333 2/17/2021
0.5.11 345 2/7/2021
0.5.3 345 1/29/2021
0.5.1 382 1/25/2021