Skip to content
Aleksander Parchomenko edited this page Oct 6, 2021 · 7 revisions

SourceMapper is code generator for mappings defined with using of attributes used in interfaces or abstract classes. It is based on C# 9 feature called Source Generators. During build process it generates mapping classes and methods that can be used in your project.

Installation

Install using nuget package manager:

Install-Package Compentio.SourceMapper

or .NET CLI:

dotnet add package Compentio.SourceMapper

Getting started

To define mapping we have to mark mapping abstract class or interface with MapperAttribute:

[Mapper]
public interface INotesMapper
{
    NoteDto MapToDto(NoteDao source);
}

This will generate mapping class with default class name NotesMapper for properties that names are the same for NoteDto and NoteDao classes. The generated class is in the same namespace as its base abstract class of interface. It can be found in project in Visual Studio:

Dependencies -> Analyzers -> Compentio.SourceMapper.Generators.MainSourceGenerator.

When the names are different than we can use Source and Target names of the properties:

[Mapper(ClassName = "InterfaceUserMapper")]
public interface IUserMapper
{
    [Mapping(Source = nameof(UserDao.FirstName), Target = nameof(UserInfo.Name))]
    UserInfo MapToDomainMoodel(UserDao userDao);       
}

The ClassName property in MapperAttribute is responsible for name of the generated mapping class. For default MapperAttribute interface prefix I is removed or Impl suffix added to the generated class name if there is no I prefix in the mapping interface name.

NuGet Nuget GitHub GitHub top language

Clone this wiki locally