Go to file
Mohammadreza Taikandi f13f8783c3 Add xml docs. 2020-12-27 08:13:21 +00:00
.github/workflows Fix build configuration. 2020-12-24 07:38:03 +00:00
src/MapTo Add xml docs. 2020-12-27 08:13:21 +00:00
test Ignore properties with matching names but different types. 2020-12-24 20:05:52 +00: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 versioning. 2020-12-22 16:13:36 +00:00
LICENSE Cleanup. 2020-12-21 16:34:53 +00:00
MapTo.sln Housekeeping. 2020-12-22 16:37:19 +00:00
MapTo.sln.DotSettings Generate partial class and assign destination properties based on an attribute. 2020-12-19 07:55:47 +00:00
README.md Update readme. 2020-12-27 07:34:27 +00:00
version.json Reduce semVer1 padding. 2020-12-24 07:37:28 +00:00

README.md

MapTo

Nuget Publish Packages

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

MapTo creates mappings during compile-time, which eliminates the need for using reflection to create the mappings. This makes it super simple to use and way faster than other libraries.

Installation

dotnet add package MapTo --prerelease

Usage

To generate the mappings, simply declare the destination class as partial and annotate it with MapFrom attribute.

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; }
    }
}

Generated Source

In the above example, if App.Data.Models.User class is defined as:

namespace App.Data.Models
{
    public class User
    {
        public User(int id) => Id = id;
        
        public int Id { get; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string FullName => $"{FirstName} {LastName}";
    }
}

It will generate the following code:

// <auto-generated />
using System;

namespace App.ViewModels
{
    public partial class UserViewModel
    {
        public UserViewModel(App.Data.Models.User user)
        {
            if (user == null) throw new ArgumentNullException(nameof(user));
            
            FirstName = user.FirstName;
            LastName = user.LastName;
        }

        public static UserViewModel From(App.Data.Models.User user)
        {
            return user == null ? null : new UserViewModel(user);
        }
    }

    public static partial class UserToUserViewModelExtensions
    {
        public static UserViewModel ToUserViewModel(this App.Data.Models.User user)
        {
            return user == null ? null : new UserViewModel(user);
        }
    }
}

Which makes it possible to get an instance of UserViewModel from one of the following ways:

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

var vm = user.ToUserViewModel();

// OR
vm = new UserViewModel(user);

// OR
vm = UserViewModel.From(user);