eQuantic.Mapper.Generator 1.1.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package eQuantic.Mapper.Generator --version 1.1.3
NuGet\Install-Package eQuantic.Mapper.Generator -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="eQuantic.Mapper.Generator" Version="1.1.3">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add eQuantic.Mapper.Generator --version 1.1.3
#r "nuget: eQuantic.Mapper.Generator, 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 eQuantic.Mapper.Generator as a Cake Addin
#addin nuget:?package=eQuantic.Mapper.Generator&version=1.1.3

// Install eQuantic.Mapper.Generator as a Cake Tool
#tool nuget:?package=eQuantic.Mapper.Generator&version=1.1.3

eQuantic.Mapper Library

The eQuantic Mapper provides all the implementation needed to use the Mapper Pattern

To install eQuantic.Mapper, run the following command in the Package Manager Console

Install-Package eQuantic.Mapper

If you choose to use generated mappers, install the Generator package below

Install-Package eQuantic.Mapper.Generator

Example of implementation

The models

public class ExampleA
{
    public string Id { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public string Date { get; set; } = string.Empty;
}

public class ExampleB
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public DateTime Date { get; set; }
    
    [MapFrom(typeof(ExampleA), nameof(ExampleA.Id))]
    public string Code { get; set; } = string.Empty;
}

The mapper

public class ExampleMapper : IMapper<ExampleA, ExampleB>
{
    public ExampleB? Map(ExampleA? source)
    {
        return Map(source, new ExampleB());
    }

    public ExampleB? Map(ExampleA? source, ExampleB? destination)
    {
        if (source == null)
        {
            return null;
        }

        if (destination == null)
        {
            return Map(source);
        }
        
        destination.Id = source.Id;
        destination.Name = source.Name;
        destination.Date = source.Date;
        
        return destination;
    }
}

The mapper with context

public class ExampleContext
{
    public string Code { get; set; }
}
public class ExampleMapper : IMapper<ExampleA, ExampleB, ExampleContext>
{
    public ExampleContext Context { get; set; }
    
    public ExampleB? Map(ExampleA? source)
    {
        return Map(source, new ExampleB());
    }

    public ExampleB? Map(ExampleA? source, ExampleB? destination)
    {
        if (source == null)
        {
            return null;
        }

        if (destination == null)
        {
            return Map(source);
        }
        
        destination.Id = source.Id;
        destination.Name = source.Name;
        destination.Date = source.Date;
        
        if(!string.IsNullOrEmpty(Context.Code))
        {
            destination.Code = Context.Code;
        }
        return destination;
    }
}

Auto-Generated Code

If you want that the mapper to be auto-generated, you need to use the MapperAttribute and partial definition into the class mapper

[Mapper(typeof(ExampleA), typeof(ExampleB))]
public partial class ExampleMapper : IMapper
{
}

The application

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMappers();
var app = builder.Build();

app.MapGet("/", (IMapperFactory mapperFactory) =>
{
    var mapper = mapperFactory.GetMapper<ExampleA, ExampleB>()!;
    var exampleA = new ExampleA
    {
        Id = "1",
        Name = "Test",
        Date = "2023-01-01"
    };
    var exampleB = mapper.Map(exampleA);
    return exampleB;
});

app.Run();

Manual customization

If you need customize the auto-generated mapper, just override Before or/and After methods:

[Mapper(typeof(ExampleA), typeof(ExampleB))]
public partial class ExampleMapper : IMapper
{
    partial void AfterMap(ExampleA source, ExampleB destination)
    {
        if(source.Name == "Test")
        {
            destination.Name = "Empty";
        }
    }
}

Debugging

Inside MapperGenerator on Initialize method use:

#if DEBUG
    SpinWait.SpinUntil(() => Debugger.IsAttached);
#endif 
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

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.2.4 225 5/5/2024
1.2.3 87 5/4/2024
1.2.2 75 5/4/2024
1.2.1 88 5/3/2024
1.2.0 43 5/3/2024
1.1.9 113 4/23/2024
1.1.8 83 4/23/2024
1.1.7 74 4/23/2024
1.1.6 83 4/23/2024
1.1.5 472 11/18/2023
1.1.4 405 8/2/2023
1.1.3 177 7/15/2023
1.1.2 152 7/15/2023
1.1.1 144 7/15/2023
1.1.0 233 5/18/2023
1.0.0 173 1/8/2023

DTOs mapping without reflection