MapTo 1.0.27
dotnet add package MapTo --version 1.0.27
NuGet\Install-Package MapTo -Version 1.0.27
<PackageReference Include="MapTo" Version="1.0.27" />
paket add MapTo --version 1.0.27
#r "nuget: MapTo, 1.0.27"
// Install MapTo as a Cake Addin #addin nuget:?package=MapTo&version=1.0.27 // Install MapTo as a Cake Tool #tool nuget:?package=MapTo&version=1.0.27
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.27 | 99 | 9/18/2024 |
1.0.25 | 110 | 9/17/2024 |
1.0.24 | 111 | 9/11/2024 |
1.0.23 | 88 | 8/29/2024 |
1.0.22 | 99 | 8/26/2024 |
1.0.21 | 103 | 8/26/2024 |
1.0.20 | 100 | 8/26/2024 |
1.0.19 | 120 | 8/24/2024 |
1.0.18 | 69 | 8/1/2024 |
1.0.17 | 64 | 8/1/2024 |
1.0.16 | 390 | 6/7/2024 |
1.0.15 | 100 | 6/7/2024 |
1.0.14 | 88 | 6/5/2024 |
1.0.13 | 102 | 6/4/2024 |
1.0.12 | 1,580 | 6/1/2024 |
1.0.11 | 98 | 6/1/2024 |
1.0.10 | 104 | 6/1/2024 |
1.0.9 | 86 | 6/1/2024 |
1.0.7 | 104 | 5/29/2024 |
1.0.6 | 109 | 5/15/2024 |
1.0.5 | 102 | 5/15/2024 |
1.0.4 | 94 | 5/15/2024 |
1.0.3 | 89 | 5/15/2024 |
1.0.2 | 90 | 5/14/2024 |
1.0.1 | 141 | 4/5/2024 |
0.9.23 | 310 | 1/3/2024 |
0.9.22 | 164 | 12/29/2023 |
0.9.14 | 185 | 12/2/2023 |
0.9.8 | 196 | 10/19/2023 |
0.9.7 | 156 | 10/16/2023 |
0.9.5 | 153 | 10/13/2023 |
0.9.3 | 143 | 10/9/2023 |
0.9.1 | 163 | 9/30/2023 |
0.7.20 | 786 | 7/29/2021 |
0.7.17 | 356 | 7/29/2021 |
0.7.16 | 376 | 7/28/2021 |
0.7.15 | 367 | 7/28/2021 |
0.7.14 | 417 | 7/3/2021 |
0.7.11 | 364 | 7/2/2021 |
0.7.9 | 352 | 7/1/2021 |
0.7.8 | 350 | 6/30/2021 |
0.7.4 | 338 | 6/23/2021 |
0.7.1 | 523 | 4/9/2021 |
0.6.3 | 365 | 3/26/2021 |
0.6.2 | 344 | 3/26/2021 |
0.6.1 | 405 | 2/21/2021 |
0.5.14 | 334 | 2/17/2021 |
0.5.11 | 346 | 2/7/2021 |
0.5.3 | 346 | 1/29/2021 |
0.5.1 | 383 | 1/25/2021 |