{"id":23050819,"url":"https://github.com/geta/geta-mapping","last_synced_at":"2025-06-12T01:33:51.748Z","repository":{"id":66329525,"uuid":"69889287","full_name":"Geta/geta-mapping","owner":"Geta","description":null,"archived":false,"fork":false,"pushed_at":"2022-04-25T08:48:58.000Z","size":60,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":23,"default_branch":"master","last_synced_at":"2025-06-11T08:55:10.519Z","etag":null,"topics":["helpers"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Geta.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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}},"created_at":"2016-10-03T16:29:46.000Z","updated_at":"2022-04-11T13:47:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"64494903-cdd0-4685-be41-ffb67dec2a09","html_url":"https://github.com/Geta/geta-mapping","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/Geta/geta-mapping","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Geta%2Fgeta-mapping","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Geta%2Fgeta-mapping/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Geta%2Fgeta-mapping/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Geta%2Fgeta-mapping/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Geta","download_url":"https://codeload.github.com/Geta/geta-mapping/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Geta%2Fgeta-mapping/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259377507,"owners_count":22848371,"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":["helpers"],"created_at":"2024-12-15T23:38:08.659Z","updated_at":"2025-06-12T01:33:51.722Z","avatar_url":"https://github.com/Geta.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Geta Mapping\n\n![](http://tc.geta.no/app/rest/builds/buildType:(id:GetaPackages_GetaMapping_00ci),branch:master/statusIcon)\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=Geta_geta-mapping\u0026metric=alert_status)](https://sonarcloud.io/summary/new_code?id=Geta_geta-mapping)\n[![Platform](https://img.shields.io/badge/Platform-.NET%20Standard%202.0-blue.svg?style=flat)](https://docs.microsoft.com/en-us/dotnet/core/)\n\nGeta Mapping is a project for common mapping logic. It consists of two libraries: [Geta.Mapping](#getamapping) and [Geta.AutoMapper](#getaautomapper).\n\n## Geta.Mapping\n\n### Description\n\nGeta.Mapping is a library with abstractions for common mapping logic.\n\n### Installation\n\n```\ndotnet add package Geta.Mapping\n```\n\n### DI Configuration\n\nFor `StructureMap` or `Lamar`, configure interfaces to connect to implementations automatically:\n\n```csharp\nScan(x =\u003e\n{\n    x.TheCallingAssembly();\n    x.WithDefaultConventions();\n    x.ConnectImplementationsToTypesClosing(typeof(IMapper\u003c,\u003e));\n    x.ConnectImplementationsToTypesClosing(typeof(ICreateFrom\u003c,\u003e));\n});\n```\n\n### Usage\n\nThen create a mapping class you want to map one object to another. Inherit from `IMapper\u003cTFrom, TTo\u003e`.\n\n```csharp\npublic class MyPocoToMyDtoMapper : IMapper\u003cMyPoco, MyDto\u003e\n{\n    public virtual void Map(MyPoco from, MyDto to)\n    {\n        to.Name = from.Name;\n    }\n}\n```\n\nThis mapping implementation will work for any classes, even for those that have a constructor with parameters.\n\nIf your destination class has a parameter-less constructor, then you can implement `Mapper\u003cTFrom, TTo\u003e`.\n\n```csharp\npublic class MyPocoToMyDtoMapper : Mapper\u003cMyPoco, MyDto\u003e\n{\n    public override void Map(MyPoco from, MyDto to)\n    {\n        to.Name = from.Name;\n    }\n}\n```\n\nNow you can use this mapper by injecting it.\n\nWhen you want to map one object to another, then use `IMapper\u003cTFrom, TTo\u003e` interface.\n\n```csharp\npublic class MyController\n{\n    private readonly IMapper\u003cMyPoco, MyDto\u003e _myMapper;\n\n    public MyController(IMapper\u003cMyPoco, MyDto\u003e myMapper)\n    {\n        _myMapper = myMapper;\n    }\n    \n    public IActionResult Index()\n    {\n        var myPoco = // Get a source object from DB or somewhere else\n        var myDto = new MyDto(true); // Instantiating _myDto_ manually as there is no parameter-less contructor\n        _myMapper.Map(myPoco, myDto);\n\n        // ...\n    }\n}\n```\n\nWhen you want to create one object from another, and a destination object's class has a parameter-less constructor, your mapper should implement `Mapper\u003cTFrom, TTo\u003e` and you should inject `ICreateFrom\u003cTFrom, TTo\u003e`.\n\n```csharp\npublic class MyController\n{\n    private readonly ICreateFrom\u003cMyPoco, MyDto\u003e _myDtoCreator;\n\n    public MyController(ICreateFrom\u003cMyPoco, MyDto\u003e myDtoCreator)\n    {\n        _myDtoCreator = myDtoCreator;\n    }\n    \n    public IActionResult Index()\n    {\n        var myPoco = // Get a source object from DB or somewhere else\n        var myDto = _myDtoCreator.Create(myPoco);\n\n        // ...\n    }\n}\n```\n\n## Geta.AutoMapper\n\n### Description\nGeta.AutoMapper is a small addition for Automapper library to simplify mapping configuration. It scans for existing mappings and provides a standardized way to do a custom mappings with automapper using `ICustomMapping` interface.\n\n### Features\n* Scans for existing mappings in the solution\n* Use `AutoMap` attribute to cover simple mapping cases (default functionality in AutoMapper - https://docs.automapper.org/en/stable/Attribute-mapping.html)\n* Use `ICustomMapping` interface for a custom mapping scenarios\n\n### How to get started?\n```\nInstall-Package Geta.AutoMapper\n```\n\nMake sure to call `AutoMapperConfig.LoadMappings(...)` on application startup.\n\n```csharp\nvar configExpression = new MapperConfigurationExpression();\n\n/* can add custom configurations if needed */\n\nAutoMapperConfig.LoadMappings(configExpression, Assembly.GetExecutingAssembly());\n\nvar mapper = new MapperConfiguration(configExpression).CreateMapper();\n```\n### Usage \nYou can use two ways how to configure the mapping between two types:\n1. Decorate destination type with `AutoMap` attribute (functionality already available in AutoMapper);\n2. Destination type implementing `ICustomMapping` interface.\n\n\n```csharp\npublic class AttributeMappingTestModel\n{\n\tpublic int SomeProperty { get; set; }\n\n\tpublic AttributeMappingTestChildModel Child { get; set; }\n\n}\n\npublic class AttributeMappingTestChildModel\n{\n\tpublic string Property { get; set; }\n}\n\n[AutoMap(typeof(AttributeMappingTestModel))]\npublic class AttributeMappingTestViewModel\n{\n\t[SourceMember(nameof(AttributeMappingTestModel.SomeProperty))]\n\tpublic int PropertyA { get; set; }\n\n\tpublic string ChildProperty { get; set; }\n}\n```\n\nYou can then simply call AutoMapper's `mapper.Map\u003cAttributeMappingTestViewModel\u003e(modelToMap);` to map from a `AttributeTestModel` to an `AttributeTestViewModel`.\n```\nNote: `SourceMember` attribute isn't working for child object property mapping configurations. If destination object propery isn't following naming pattern {PropertyName}{ChildPropertyName} you will have to use custom mapping approach.\n```\nUse `ICustomMappings` for more advanced mapping scenarios. \n\n```csharp\npublic class CustomMappingTestModel\n{\n\tpublic int SomeProperty { get; set; }\n\tpublic DateTime OtherProperty { get; set; }\n\n\tpublic CustomMappingChildModel Child { get; set; }\n\n}\n\npublic class CustomMappingChildModel\n{\n\tpublic string SomeProperty { get; set; }\n}\n\npublic class CustomMappingTestViewModel : ICustomMapping\n{\n\tpublic int Property { get; set; }\n\tpublic DateTime OtherProperty { get; set; }\n\tpublic string ChildProperty { get; set; }\n\tpublic void CreateMapping(IMapperConfigurationExpression configuration)\n\t{\n\t\tconfiguration.CreateMap\u003cCustomMappingTestModel, CustomMappingTestViewModel\u003e()\n\t\t\t.ForMember(d =\u003e d.ChildProperty, o =\u003e o.MapFrom(s =\u003e s.Child.SomeProperty))\n\t\t\t.ForMember(d =\u003e d.Property, o =\u003e o.MapFrom(s =\u003e s.SomeProperty));\n\t}\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeta%2Fgeta-mapping","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgeta%2Fgeta-mapping","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeta%2Fgeta-mapping/lists"}