MapTo 1.0.1

dotnet add package MapTo --version 1.0.1
NuGet\Install-Package MapTo -Version 1.0.1
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.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MapTo --version 1.0.1
#r "nuget: MapTo, 1.0.1"
#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.1

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

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.1 83 4/5/2024
0.9.23 177 1/3/2024
0.9.22 129 12/29/2023
0.9.14 159 12/2/2023
0.9.8 161 10/19/2023
0.9.7 124 10/16/2023
0.9.5 120 10/13/2023
0.9.3 117 10/9/2023
0.9.1 133 9/30/2023
0.7.20 745 7/29/2021
0.7.17 331 7/29/2021
0.7.16 340 7/28/2021
0.7.15 339 7/28/2021
0.7.14 381 7/3/2021
0.7.11 335 7/2/2021
0.7.9 311 7/1/2021
0.7.8 323 6/30/2021
0.7.4 315 6/23/2021
0.7.1 491 4/9/2021
0.6.3 335 3/26/2021
0.6.2 320 3/26/2021
0.6.1 371 2/21/2021
0.5.14 312 2/17/2021
0.5.11 317 2/7/2021
0.5.3 316 1/29/2021
0.5.1 341 1/25/2021