Skip to content

CompMap is a C# Roslyn-based source generator that automatically creates mapping methods between classes. It simplifies the process of converting one class type to another by generating the necessary code at compile time.

License

Notifications You must be signed in to change notification settings

Ten-James/CompMap

Repository files navigation

CompileMapper (TenJames.CompMap)

CompMap is a C# Roslyn-based source generator that automatically creates mapping methods between classes. It simplifies the process of converting one class type to another by generating the necessary code at compile time.

Ensure your DTO's are correctly mapped with compile time safety, to ensure valid changes are tracked in your project.

//generated UserReadDto.g.cs
// <auto-generated/>
using System;
using System.Collections.Generic;

using TenJames.CompMap.Mappper;

namespace TenJames.CompMap.Example;

partial class UserReadDto
{

    ///<summary>
    /// The following properties were not mapped because they do not exist in the target class
    ///</summary>
    internal class UserUnmappedProperties
    {
        /// <summary>
        /// Found at /Entitities.cs at 29
        /// </summary>
        public required string Title { get; set; }
    }

    private static partial UserUnmappedProperties GetUserUnmappedProperties(IMapper mapper,  User source);

    /// <summary>
    /// Mapping method generated by TenJames.CompMap
    /// </summary>
    public static UserReadDto MapFrom(IMapper mapper, User source)
    {
        // Note: Some properties were not mapped due to missing counterparts in the target class.
        var unmapped = GetUserUnmappedProperties(mapper, source);
        return new UserReadDto
        {
            Id = source.Id,
            Name = source.Name,
            Guid = source.Guid,
            Documents = mapper.Map<ICollection<DocumentDto> >(source.Documents),
            Title = unmapped.Title,
        };
    }
}

Which can be generated simply from:

using TenJames.CompMap.Attributes;

// Entity

public class User {
    public int Id { get; set; }
    public string Name { get; set; }
    public Guid Guid { get; set; }
    public ICollection<Document> Documents { get; set; }
}

// DTOs

[MapFrom(typeof(User))]
public partial class UserReadDto {
    public int Id { get; set; }
    public string Name { get; set; }
    public Guid Guid { get; set; }
    public required string Title { get; set; }
    public ICollection<DocumentDto> Documents { get; set; }

    //Compile time ensurence of type safety
    private static partial UserUnmappedProperties GetUserUnmappedProperties(IMapper mapper, User source)
    {
        return new UserUnmappedProperties()
        {
            // All using Easy to use Mapping methods
            Title = source.Name + "'s Title",
        };
    }
}

Getting started

Install the TenJames.CompMap package via NuGet:

dotnet add package TenJames.CompMap

The package will automatically be configured as a source generator. If you need to reference it manually in your project file:

<PackageReference
    Include="TenJames.CompMap"
    Version="latest_version"
    OutputItemType="Analyzer"
    ReferenceOutputAssembly="false"
/>

Note: The OutputItemType="Analyzer" and ReferenceOutputAssembly="false" attributes are typically not required when using dotnet add package, as the package is already configured correctly.

Component registration

If you are using dependency injection, register the mapping services in your DI container:

using TenJames.CompMap.Mapper;

...

// Feel free to try with Extending BaseMap for custom mapping logic
services.AddTransient<IMapper, BaseMap>();

Usage

Add Attributes to your DTO classes and then enjoy the generated mapping methods :)


Example

See the Example project for a complete working example.

Contributing

Contributions are welcome! Please fork the repository and submit a pull request with your changes.

see CONTRIBUTING.md for details.

About

CompMap is a C# Roslyn-based source generator that automatically creates mapping methods between classes. It simplifies the process of converting one class type to another by generating the necessary code at compile time.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages