{"id":13629306,"url":"https://github.com/mrtaikandi/MapTo","last_synced_at":"2025-04-17T08:34:38.631Z","repository":{"id":46826800,"uuid":"323387769","full_name":"mrtaikandi/MapTo","owner":"mrtaikandi","description":"A convention based object to object mapper using Roslyn source generator.","archived":false,"fork":false,"pushed_at":"2025-04-10T19:14:35.000Z","size":496,"stargazers_count":93,"open_issues_count":0,"forks_count":6,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-10T20:39:27.620Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mrtaikandi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-12-21T16:17:03.000Z","updated_at":"2025-04-10T19:14:39.000Z","dependencies_parsed_at":"2023-10-14T16:58:14.826Z","dependency_job_id":"b991bd4f-0a23-4427-bbb3-39a8e72e4353","html_url":"https://github.com/mrtaikandi/MapTo","commit_stats":null,"previous_names":[],"tags_count":35,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrtaikandi%2FMapTo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrtaikandi%2FMapTo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrtaikandi%2FMapTo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrtaikandi%2FMapTo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mrtaikandi","download_url":"https://codeload.github.com/mrtaikandi/MapTo/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249326186,"owners_count":21251735,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T22:01:07.044Z","updated_at":"2025-04-17T08:34:38.624Z","avatar_url":"https://github.com/mrtaikandi.png","language":"C#","readme":"# MapTo\n[![Nuget](https://img.shields.io/nuget/v/mapto?logo=nuget)](https://www.nuget.org/packages/MapTo/)\n![Publish Packages](https://github.com/mrtaikandi/MapTo/workflows/Publish%20Packages/badge.svg)\n\nA convention based object to object mapper using [Roslyn source generator](https://github.com/dotnet/roslyn/blob/master/docs/features/source-generators.md).\n\nMapTo 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.\n\n## GitAds Sponsored\n[![Sponsored by GitAds](https://gitads.dev/v1/ad-serve?source=mrtaikandi/mapto@github)](https://gitads.dev/v1/ad-track?source=mrtaikandi/mapto@github)\n\n\n## Installation\n```\ndotnet add package MapTo --prerelease\n```\n\n## Usage\nUnlike 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.\n\n```c#\nusing MapTo;\n\nnamespace App.ViewModels;\n\n[MapFrom(typeof(App.Data.Models.User))]\npublic class UserViewModel \n{\n    public string FirstName { get; init; }\n\n    public string LastName { get; init; }\n    \n    [IgnoreProperty]\n    public string FullName { get; set; }\n}\n```\n\nTo get an instance of `UserViewModel` from the `User` class, you can use the generated extension method:\n\n```c#\nvar user = new User(id: 10) { FirstName = \"John\", LastName = \"Doe\" };\n\nvar vm = user.MapToUserViewModel(); // A generated extension method for User class.\n```\n\nSometimes, 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.\n\n```c#\n[MapFrom(typeof(App.Data.Models.User))]\npublic partial class UserViewModel \n{\n    public int Id { get; }\n    \n    public string FirstName { get; init; }\n\n    public string LastName { get; init; }\n    \n    [IgnoreProperty]\n    public string FullName { get; set; }\n}\n```\n\n## Available Attributes\n\n### MapFrom\nAs 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.\n\n```c#\n[MapFrom(typeof(App.Data.Models.User), BeforeMap = nameof(RunBeforeMap), AfterMap = nameof(RunAfterMap))]\npublic partial class UserViewModel\n{\n    public int Id { get; }\n\n    ...\n    \n    // The BeforeMap method can also return a `User` type. If so, \n    // the returned value will be used as the source object.\n    // Or it can return `null` to skip the mapping process and return `null` to \n    // the extension method's caller.\n    private static void RunBeforeMap(User? source) { /* ... */ }\n    \n    private static void RunAfterMap(UserViewModel target) { /* ... */ }\n}\n```\n\n### IgnoreProperty\nBy 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.\n```c#\n[IgnoreProperty]\npublic string FullName { get; set; }\n``` \n\n### MapProperty\nThis 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.\n\n```c#\n[MapProperty(From = \"Id\")]\npublic int Key { get; set; }\n```\n\n### PropertyTypeConverter\nA 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.\n\nThis 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:\n\n```c#\npublic static TDestination Convert(TSource source)\n\n// or\n\npublic static TDestination Convert(TSource source, object[]? parameters)\n```\n\n```c#\n[MapFrom(typeof(User))]\npublic partial class UserViewModel\n{\n    public DateTimeOffset RegisteredAt { get; set; }\n    \n    [IgnoreProperty]\n    public ProfileViewModel Profile { get; set; }\n    \n    [MapProperty(From = nameof(User.Id))]    \n    [PropertyTypeConverter(nameof(IntToHexConverter))]\n    public string Key { get; }\n\n    private static string IntToHexConverter(int source) =\u003e $\"{source:X}\"; // The converter method.\n}\n```\n\u003c!-- GitAds-Verify: XKDJSQXWY7MCR79AFEAPKKQIXPRKL8DR --\u003e","funding_links":[],"categories":["others","Content","C# #","Source Generators"],"sub_categories":["67. [MapTo](https://ignatandrei.github.io/RSCG_Examples/v2/docs/MapTo) , in the [Mapper](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#mapper) category","Mappers"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrtaikandi%2FMapTo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrtaikandi%2FMapTo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrtaikandi%2FMapTo/lists"}