{"id":28167874,"url":"https://github.com/mikegoatly/mappit","last_synced_at":"2026-03-02T17:04:42.474Z","repository":{"id":291300524,"uuid":"977139102","full_name":"mikegoatly/Mappit","owner":"mikegoatly","description":"An object mapping library using source generators","archived":false,"fork":false,"pushed_at":"2025-05-20T13:38:02.000Z","size":134,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-27T12:41:16.368Z","etag":null,"topics":["automapper-alternative","code-generation","csharp","dotnet","object-mapping","type-mapping"],"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/mikegoatly.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":"2025-05-03T14:12:08.000Z","updated_at":"2025-05-20T13:35:32.000Z","dependencies_parsed_at":null,"dependency_job_id":"91bc8adb-5600-41a3-9f39-dad34b05cc8c","html_url":"https://github.com/mikegoatly/Mappit","commit_stats":null,"previous_names":["mikegoatly/mappit"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mikegoatly/Mappit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikegoatly%2FMappit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikegoatly%2FMappit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikegoatly%2FMappit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikegoatly%2FMappit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mikegoatly","download_url":"https://codeload.github.com/mikegoatly/Mappit/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikegoatly%2FMappit/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30011191,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-02T17:00:27.440Z","status":"ssl_error","status_checked_at":"2026-03-02T17:00:03.402Z","response_time":60,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["automapper-alternative","code-generation","csharp","dotnet","object-mapping","type-mapping"],"created_at":"2025-05-15T14:14:18.727Z","updated_at":"2026-03-02T17:04:42.466Z","avatar_url":"https://github.com/mikegoatly.png","language":"C#","readme":"﻿\u003e ⚠️ This project is in active early development. The API may change, and there may be bugs. I'm very interested in feedback, so please open issues \nif you find any problems or have suggestions for improvements. The goal is to make this library as useful as possible for everyone.\n\n# Mappit\n\nMappit is a library that allows simple mapping between two types. Instead of using reflection at runtime, code generation is used to define the mapping code.\n\nThe library errs on the side of correctness, so if types can't be fully mapped you'll get *compilation errors*, not errors at runtime.\n\nSo the benefits of Mappit are:\n\n* Compile-time validation of mappings - you can see the generated source code, so it's easy to reason about what's going on.\n* No runtime reflection\n* No runtime performance overhead - mappings are pretty much what you'd write by hand\n\n## Supported mapping features\n\n### Properties\n\n* **Implicit property mappings** (properties with matching names and compatible types)\n* **Custom property mappings** - mapping from one property to another property with a different name, including support for custom value transformations.\n* **Implicit nullable mappings** - automatic mapping between a type with it's `Nullable\u003cT\u003e` counterpart. The reverse is supported too, but will be configured to throw \n  `ArgumentNullException` at runtime is the source value is null.\n\n### Enums\n\n* **Implicit enum mappings** where all the enum names match\n* **Custom enum value mappings** - mapping from one enum member to another\n\n### Collections and Dictionaries\n\n* **Implicit collection mapping** - where a mapped type has a property that's a collection or dictionary, Mappit will create an implicit mapping for the collection elements or dictionary keys/values.\n* **Explicit collection mapping** - registering a collection mapping in your mapper class, e.g. `IList\u003cFooRepresentation\u003e Map(IEnumerable\u003cFoo\u003e source)` will automatically create a mapping for the collection elements.\n  The same applies for both keys and values of dictionary types.\n* **Copying collections by reference** - by default, collections and dictionaries are copied by reference when no element mapping is required. You can opt into deep copying at \n  the class or method level.\n\n### Type construction\n\n* **Constructor initialization**, including constructors that only cover some of the properties. Any remaining properties will be initialized via their setters.\n* **Missing properties on the target type** - by default you'll get compile-time errors, but can opt in to ignore them.\n* Support for both structs and classes, including records.\n\n## Getting started\n\n```\nnuget install Mappit\n```\n\n``` csharp\n// This is the mapper class - the implementation is generated at compile time\n[Mappit]\npublic partial class DemoMapper\n{\n    /// \u003csummary\u003e\n    /// Maps a Foo object to FooRepresentation\n    /// \u003c/summary\u003e\n    /// \u003cparam name=\"source\"\u003eThe source Foo object\u003c/param\u003e\n    /// \u003creturns\u003eA mapped FooRepresentation\u003c/returns\u003e\n    public partial IList\u003cFooRepresentation\u003e Map(IEnumerable\u003cFoo\u003e source);\n}\n\npublic static class Program\n{\n    public static void Main()\n    {\n        // Create a mapper instance\n        DemoMapper mapper = new DemoMapper();\n\n        // Create source objects\n        Foo[] foos = [\n            new Foo(1, \"Test Object\", DateTime.Now, true),\n            new Foo(2, \"Another Object\", DateTime.Now.AddDays(-1), false)\n        ];\n\n        // Map to target type using the strongly-typed method\n        IList\u003cFooRepresentation\u003e fooRepresentations = mapper.Map(foos);\n\n        foreach (var fooRepresentation in fooRepresentations)\n        {\n            Console.WriteLine($\"Mapped object: Id={fooRepresentation.Id}, Name={fooRepresentation.Name}\");\n        }\n    }\n}\n\npublic record Foo(int Id, string Name, DateTime CreatedDate, bool IsActive);\n\n// Notice the mix of properties and constructor parameters\npublic class FooRepresentation(int id)\n{\n    public int Id { get; } = id;\n    public required string Name { get; set; }\n    public DateTime CreatedDate { get; set; }\n    public bool IsActive { get; set; }\n}\n```\n\nThe generated code looks like this:\n\n```csharp\npublic partial class DemoMapper : IDemoMapper\n{\n\n    // Implementation of mapping from Foo to FooRepresentation\n    public global::Mappit.Examples.FooRepresentation Map(global::Mappit.Examples.Foo source)\n    {\n        if (source is null)\n        {\n            return default;\n        }\n\n        return new global::Mappit.Examples.FooRepresentation(\n            source.Id)\n        {\n            Name = source.Name,\n            CreatedDate = source.CreatedDate,\n            IsActive = source.IsActive,\n        };\n    }\n\n    // Implementation of mapping from IEnumerable\u003cFoo\u003e to IList\u003cFooRepresentation\u003e\n    public partial global::System.Collections.Generic.IList\u003cglobal::Mappit.Examples.FooRepresentation\u003e Map(global::System.Collections.Generic.IEnumerable\u003cglobal::Mappit.Examples.Foo\u003e source)\n    {\n        if (source is null)\n        {\n            return default;\n        }\n\n        return new global::System.Collections.Generic.List\u003cglobal::Mappit.Examples.FooRepresentation\u003e(source.Select(Map));\n    }\n}\n```\n\nSome people like interfaces for everything, so every generated mapper also implements its own interface - the above example has a generated interface `IDempMapper`.\n\nIf you like, you can have multiple mapper classes with different names. They will each end up with their own interface, and you can use them independently.\n\n### Custom Property Mapping\n\nIf you need to map properties with different names, you can use the `MapProperty` attribute:\n\n```csharp\n[Mappit]\npublic partial class Mapper\n{\n    [MapProperty(nameof(Foo.SourceProp), nameof(FooRepresentation.TargetProp))]\n    public partial FooRepresentation Map(Foo source);\n}\n```\n\nThis will map the `SourceProp` property of `Foo` to the `TargetProp` property of `FooRepresentation`. \n\nThe property names are validated at compile time, so you'll get a compilation error if they don't exist or have incompatible types.\n\n#### Custom Value Conversion Methods\n\nYou can specify a custom conversion method for transforming property values during mapping:\n\n```csharp\n[Mappit]\npublic partial class Mapper\n{\n    // If the target name is not specified, it is assumed to be the same as the source name.\n    // (You'll get a validation error if it's not!)\n    [MapProperty(nameof(Foo.Name), ValueConversionMethod = nameof(CleanupName))]\n    public partial FooRepresentation Map(Foo source);\n    \n    // Custom conversion method - must have matching parameter and return types\n    private string CleanupName(string name) =\u003e name?.Trim();\n}\n```\n\nThe conversion method must:\n\n1. Have a return type matching the target property type\n2. Take a single parameter matching the source property type\n3. Be defined within the mapper class\n\nThis feature enables:\n\n* Custom type conversions between incompatible types\n* Data transformations like string cleanup, null handling, etc.\n* Keeping most mappings automatic while customizing specific properties\n* Reusing the same conversion logic across multiple mappings\n\n### Handling Missing Properties\n\nBy default, at the class level, Mappit will generate an error when source properties don't have matching target properties. You can control this behavior with the `IgnoreMissingPropertiesOnTarget` option at either\nthe class or mapping method:\n\n```csharp\n// Class level setting - default is false, but you can set it to true here\n[Mappit(IgnoreMissingPropertiesOnTarget = true)]\npublic partial class Mapper\n{\n    // This mapping will ignore properties that exist in the source but not in the target\n    // because of the class-level setting\n    public partial FooRepresentation Map(Foo source);\n    \n    // Override at the field level to require all properties to be mapped\n    [IgnoreMissingPropertiesOnTarget(false)]\n    public partial BarRepresentation Map(Bar source);\n}\n```\n\n## Copying collections by reference\n\nBy default, Mappit will copy collections and dictionaries by reference when no element mapping is required. This means that the \ncollection in your mapped object will be the same instance as the source collection. This is useful for performance reasons, especially\nwhen dealing with large collections, but it does mean that if you modify the collection in the mapped object, it will also modify \nthe source collection.\n\nIf you want to force a deep copy of the collection, you can opt into this behavior at the class level:\n\n```csharp\n[Mappit(DeepCopyCollectionsAndDictionaries = true)]\n```\n\nOr at a method level:\n```csharp\n[Mappit]\npublic partial class Mapper\n{\n    [DeepCopyCollectionsAndDictionaries]\n    public partial FooRepresentation Map(Foo source);\n}\n```\n\nIf you need to deep copy most of the time, but have a few cases where you want to copy by reference, you can set the class level option to `true` \nand override it at the method level to `false`.\n\n```csharp\n[Mappit(DeepCopyCollectionsAndDictionaries = true)]\npublic partial class Mapper\n{\n    // This will copy the collection by reference, even though the class level setting is true\n    [DeepCopyCollectionsAndDictionaries(false)]\n    public partial FooRepresentation Map(Foo source);\n}\n```\n\n## Implicit collection property mapping\n\nIf a property is a collection, array or dictionary, Mappit will implicitly map the collection elements or dictionary values to the target type. For example:\n\n```csharp\npublic record Team(List\u003cPerson\u003e People);\npublic record Person(string Name, int Age);\n\npublic record TeamRepresentation(List\u003cPersonRepresentation\u003e People);\npublic record PersonRepresentation(string Name, int Age);\n\n\n// In order to map a team to team representation, you only need to map\n// the Team and Person mappings - Mappit will handle implicitly mapping the collection\n[Mappit]\npublic partial class Mapper\n{\n    public partial TeamRepresentation Map(Team source);\n    public partial PersonRepresentation Map(Person source);\n}\n\nvar mapper = new Mapper();\nvar team = new Team(new List\u003cPerson\u003e { new Person(\"Alice\", 30), new Person(\"Bob\", 25) });\n\nvar teamRepresentation = mapper.Map\u003cTeamRepresentation\u003e(team);\n\nConsole.WriteLine(teamRepresentation.People.Count); // Outputs: 2\n```\n\n## Enum Mapping\n\nEnums with the same name and compatible values are mapped automatically. For enums with different names or values, you need to use custom enum mapping.\n\n### Custom Enum Mapping\n\nFor enums with different values, you can define custom mappings using the `MapMember` attribute:\n\n```csharp\npublic enum SourceStatus { \n    Active = 0, \n    Inactive = 1,\n    Pending = 2\n}\n\npublic enum TargetStatus { \n    Enabled = 0, \n    Disabled = 1,\n    AwaitingConfirmation = 2\n}\n\n[Mappit]\npublic partial class Mapper\n{\n    [MapEnumValue(nameof(SourceStatus.Active), nameof(TargetStatus.Enabled))]\n    [MapEnumValue(nameof(SourceStatus.Inactive), nameof(TargetStatus.Disabled))]\n    [MapEnumValue(nameof(SourceStatus.Pending), nameof(TargetStatus.AwaitingConfirmation))]\n    public partial TargetStatus Map(SourceStatus source);\n}\n```\n\nIf you get any of these names wrong, you'll get a compile-time error.\n\n## Custom type mappings\n\nIf you run into limitations for a certain type, you can provide a concrete implementation for a mapping method that the \nsource generator will use as-is:\n\n```csharp\n[Mappit]\npublic partial class CustomMappingTestMapper\n{\n    public WeirdModelMapped Map(WeirdModel source)\n    {\n        return new WeirdModelMapped { Name = new string([..source.Name.Reverse()]) };\n    }\n}\n```\n\n## How does the source generator work?\n\nI want to document this properly some time, but for now, here's a quick overview:\n\n1. The source generator scans for classes with the `Mappit` attribute.\n1. The basic structure of the user defined mappings is generated into a `MapperClassInfo` object.\n1. A validation step is performed to ensure that the mappings are valid, producing a `ValidatedMapperClassInfo` instance. \n   This step includes checking for things like:\n   * Missing properties on the source or target types\n   * Incompatible types between source and target properties\n   * Defining implicit mappings for nullables, collections, dictionaries, etc.\n1. The `ValidatedMapperClassInfo` instance is then used to generate the actual mapping code, which is written to a file in the user's project.\n\n## Known limitations\n\n* Classes containing properties with properties differing only by case are not supported.\n* Recursive object graphs won't work and your code will likely hang forever. I'll get to this too!\n\n## Todo\n\n* Recursion handling\n* More complex mappings, like:\n  * Object flattening - e.g. map a complex object to a simple one\n  * Object expansion - e.g. map a simple object to a complex one","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikegoatly%2Fmappit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmikegoatly%2Fmappit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikegoatly%2Fmappit/lists"}