Go to file
Wvader fd294925fc struct 2021-12-06 02:06:54 +00:00
.github/workflows Update and rename build_and-test.yml to build-and-test.yml 2021-07-29 17:52:42 +01:00
src/MapTo struct 2021-12-06 02:06:54 +00:00
test Fix missing static null analysis attribute errors on unsupported tfms. 2021-07-29 17:40:55 +01:00
.editorconfig Cleanup. 2020-12-21 16:34:53 +00:00
.gitignore Generate partial class and assign destination properties based on an attribute. 2020-12-19 07:55:47 +00:00
Directory.Build.props Add support for c# records. 2021-06-30 19:17:56 +01:00
LICENSE Cleanup. 2020-12-21 16:34:53 +00:00
MapTo.sln Fix cyclic reference issue. 2021-04-09 08:05:53 +01:00
MapTo.sln.DotSettings Add TypeConverter interface. 2021-01-06 08:52:50 +00:00
README.md Fix cyclic reference issue. 2021-04-09 08:05:53 +01:00
version.json Inject Update method 2021-12-05 23:46:24 +00:00

README.md

MapTo

Nuget Publish Packages

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

MapTo is a library to programmatically generate the necessary code to map one object to another during compile-time, eliminating the need to use reflection to map objects and make 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

MapTo relies on a set of attributes to instruct it on how to generate the mappings. To start, declare the destination class as partial and annotate it with MapFrom attribute. As its name implies, MapFrom attribute tells the library what the source class you want to map from is.

using MapTo;

namespace App.ViewModels
{
    [MapFrom(typeof(App.Data.Models.User))]
    public partial class UserViewModel 
    {
        public string FirstName { get; }
    
        public string LastName { get; }
        
        [IgnoreProperty]
        public string FullName { get; set; }
    }
}

To get an instance of UserViewModel from the User class, you can use any of the following methods:

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

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

// OR
vm = new UserViewModel(user); // A generated contructor.

// OR
vm = UserViewModel.From(user); // A generated factory method.

Please refer to sample console app for a more complete example.

Available Attributes

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 the way 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(SourcePropertyName = "Id")]
public int Key { get; set; }

MapTypeConverter

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, MapTypeConverter can be used.

This attribute will accept a type that implements ITypeConverter<in TSource, out TDestination> interface.

[MapFrom(typeof(User))]
public partial class UserViewModel
{
    public DateTimeOffset RegisteredAt { get; set; }
    
    [IgnoreProperty]
    public ProfileViewModel Profile { get; set; }
    
    [MapTypeConverter(typeof(IdConverter))]
    [MapProperty(SourcePropertyName = nameof(User.Id))]    
    public string Key { get; }

    private class IdConverter : ITypeConverter<int, string>
    {
        public string Convert(int source, object[] converterParameters) => $"{source:X}";
    }
}