MapTo 1.0.28
dotnet add package MapTo --version 1.0.28
NuGet\Install-Package MapTo -Version 1.0.28
<PackageReference Include="MapTo" Version="1.0.28" />
paket add MapTo --version 1.0.28
#r "nuget: MapTo, 1.0.28"
// Install MapTo as a Cake Addin #addin nuget:?package=MapTo&version=1.0.28 // Install MapTo as a Cake Tool #tool nuget:?package=MapTo&version=1.0.28
MapTo
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 | Versions 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. |
-
.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.28 | 34 | 12/27/2024 |
1.0.27 | 129 | 9/18/2024 |
1.0.25 | 123 | 9/17/2024 |
1.0.24 | 122 | 9/11/2024 |
1.0.23 | 99 | 8/29/2024 |
1.0.22 | 110 | 8/26/2024 |
1.0.21 | 116 | 8/26/2024 |
1.0.20 | 111 | 8/26/2024 |
1.0.19 | 131 | 8/24/2024 |
1.0.18 | 80 | 8/1/2024 |
1.0.17 | 77 | 8/1/2024 |
1.0.16 | 464 | 6/7/2024 |
1.0.15 | 113 | 6/7/2024 |
1.0.14 | 99 | 6/5/2024 |
1.0.13 | 113 | 6/4/2024 |
1.0.12 | 1,704 | 6/1/2024 |
1.0.11 | 109 | 6/1/2024 |
1.0.10 | 117 | 6/1/2024 |
1.0.9 | 99 | 6/1/2024 |
1.0.7 | 118 | 5/29/2024 |
1.0.6 | 120 | 5/15/2024 |
1.0.5 | 113 | 5/15/2024 |
1.0.4 | 107 | 5/15/2024 |
1.0.3 | 100 | 5/15/2024 |
1.0.2 | 101 | 5/14/2024 |
1.0.1 | 150 | 4/5/2024 |
0.9.23 | 332 | 1/3/2024 |
0.9.22 | 171 | 12/29/2023 |
0.9.14 | 192 | 12/2/2023 |
0.9.8 | 203 | 10/19/2023 |
0.9.7 | 163 | 10/16/2023 |
0.9.5 | 160 | 10/13/2023 |
0.9.3 | 150 | 10/9/2023 |
0.9.1 | 170 | 9/30/2023 |
0.7.20 | 795 | 7/29/2021 |
0.7.17 | 367 | 7/29/2021 |
0.7.16 | 385 | 7/28/2021 |
0.7.15 | 378 | 7/28/2021 |
0.7.14 | 430 | 7/3/2021 |
0.7.11 | 375 | 7/2/2021 |
0.7.9 | 361 | 7/1/2021 |
0.7.8 | 361 | 6/30/2021 |
0.7.4 | 347 | 6/23/2021 |
0.7.1 | 534 | 4/9/2021 |
0.6.3 | 376 | 3/26/2021 |
0.6.2 | 353 | 3/26/2021 |
0.6.1 | 414 | 2/21/2021 |
0.5.14 | 345 | 2/17/2021 |
0.5.11 | 355 | 2/7/2021 |
0.5.3 | 355 | 1/29/2021 |
0.5.1 | 392 | 1/25/2021 |