From 29f96b670eefd16af32e8d684eacba23b41e0dd9 Mon Sep 17 00:00:00 2001 From: Mohammadreza Taikandi Date: Tue, 22 Dec 2020 08:20:49 +0000 Subject: [PATCH] Add initial readme content. --- README.md | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4c2c323..2fbf009 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,87 @@ # MapTo -An object to object mapping generator using using Roslyn code generator. \ No newline at end of file +An object to object mapping generator using using [Roslyn source generator](https://github.com/dotnet/roslyn/blob/master/docs/features/source-generators.md). + +## Installation +``` +dotnet add package MapTo +``` + +## Usage +MapTo creates mappings during compile-time. To indicate which objects it needs to generate the mappings for, simply declare the class as `partial` and annotate it with `MapFrom` attribute. + +```c# +[MapFrom(sourceType: typeof(App.Data.Models.User))] +public partial class UserViewModel +{ + public string FirstName { get; } + + public string LastName { get; } +} +``` + +If `Data.Models.User` class is defined as follow: + +```c# +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: + +```c# +// +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 UserExtensions + { + 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: + +```c# +var user = new User(id: 10) { FirstName = "John", LastName = "Doe" }; + +var vm = user.ToUserViewModel(); + +// OR +vm = new UserViewModel(user); + +// OR +vm = UserViewModel.From(user); +``` \ No newline at end of file