Mapperic 1.1.3

dotnet add package Mapperic --version 1.1.3                
NuGet\Install-Package Mapperic -Version 1.1.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Mapperic" Version="1.1.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Mapperic --version 1.1.3                
#r "nuget: Mapperic, 1.1.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Mapperic as a Cake Addin
#addin nuget:?package=Mapperic&version=1.1.3

// Install Mapperic as a Cake Tool
#tool nuget:?package=Mapperic&version=1.1.3                

.NET NuGet latest version NuGet Downloads

Mapperic

Mapping Magic! Automatically generate DTO Classes and AutoMapper Configurations using Roslyn Incremental Source Generators.

How it works

This library utilizes Roslyn Incremental Source Generators to generate Data Transfer Objects (DTOs) for classes marked with the attribute [GenerateDto] and properties/fields marked with the attribute [DtoProperty]. Then the relationship between the DTO and the original object is automatically saved into profile that can be fed into AutoMapper to create an object mapper.

Who is this for?

This tool is tailor-made for developers who can’t stand the repetitive task of creating DTOs and writing AutoMapper mappings—but still find themselves stuck doing it on every project.

Example

Test.cs

using Mapperic.Attributes;  
  
namespace DTOGeneratorTester;  
  
[GenerateDto]  
[AddDtoProperty(TargetName = "AgeInMonths", TargetType = typeof(int), ConversionExpression = "src => ((DateTime.Today.Year * 12 + DateTime.Today.Month) - (src.BirthDay.Year * 12 + src.BirthDay.Month))")]  
[AddDtoProperty(TargetName = "AgeInYears", TargetType = typeof(int), ConversionExpression = "src => ((DateTime.Today.Year) - (src.BirthDay.Year))")]  
public class Test  
{  
    [DtoProperty(TargetName = "HiddenName", TargetType = typeof(string), ConversionExpression = "src => string.Join(' ', src.Name.Split(' ', StringSplitOptions.None).Select(w => w.Length > 0 ? w[0] + new string('*', w.Length - 1) : \"\"))")]  
    public string Name { get; set; }  
    [DtoProperty]  
    public DateOnly BirthDay { get; set; }  
      
}

Program.cs

using AutoMapper;  
  
namespace DTOGeneratorTester;  
  
internal static class Program  
{  
    static void Main()  
    {  
        var config = new MapperConfiguration(cfg => {  
	        cfg.AddProfile<Mapperic.Profiles.DtoMappingProfile>(); // This Profile will be generated for all marked types in the Mapperic.Profiles  
		});  
        var mapper = config.CreateMapper();  
  
        var test = new Test{  
            Name = "Michael Jones",  
            BirthDay = new DateOnly(2020, 12, 31),  
		};  
  
        var testMap = mapper.Map<TestDto>(test);  
        Console.WriteLine($"Original:\t[FirstName: {test.Name}, Birthday: {test.BirthDay.ToString("d")}]");  
        Console.WriteLine($"Mapped:\t\t[FullName: {testMap.HiddenName}, AgeInMonths: {testMap.AgeInMonths}, AgeInYears: {testMap.AgeInYears}]");  
    }  
}

.csproj

<PackageReference Include="Mapperic" Version="x.y.z" PrivateAssets="all" />

Known Limitations

  • ConversionExpression does not work on Private Fields/Properties because AutoMapper has no way to access them.
  • ConversionExpression is a little annoying to do for complex statements since there is no IntelliSense. I chose to use strings for these because it is a workaround for C# attributes only being able to store compile-time constants.

Contributions

Feel free to fork this project and submit pull requests to add enhancements or bug fixes.

License

MIT License

Copyright (c) 2025 Greg James

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.3 39 1/18/2025