laget.Mapper 1.0.8

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package laget.Mapper --version 1.0.8
NuGet\Install-Package laget.Mapper -Version 1.0.8
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="laget.Mapper" Version="1.0.8" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add laget.Mapper --version 1.0.8
#r "nuget: laget.Mapper, 1.0.8"
#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 laget.Mapper as a Cake Addin
#addin nuget:?package=laget.Mapper&version=1.0.8

// Install laget.Mapper as a Cake Tool
#tool nuget:?package=laget.Mapper&version=1.0.8

laget.Mapper

An extremely simple object-object mapper.

Nuget Nuget

Configuration

This example is shown using Autofac since this is the go-to IoC for us.

await Host.CreateDefaultBuilder()
    .ConfigureContainer<ContainerBuilder>((context, builder) =>
    {
        builder.RegisterMappers();
    })
    .UseServiceProviderFactory(new AutofacServiceProviderFactory())
    .Build()
    .RunAsync();

This is the simplest way to register your mappers but also has some constraints:

  • The mapper implementations must exist in the program that calls RegisterMappers.
  • The mapper implementations must implement the interface IMapper.
await Host.CreateDefaultBuilder()
    .ConfigureContainer<ContainerBuilder>((context, builder) =>
    {
        builder.RegisterMappers(_ =>
        {
            _.TheCallingAssembly();
            _.TheCallingAssembly<T>();
            _.AssemblyContainingType<T>();
            _.Assembly("Mappers");
        });
    })
    .UseServiceProviderFactory(new AutofacServiceProviderFactory())
    .Build()
    .RunAsync();

This is the advanced and more customizable way to register your mappers

  • TheCallingAssembly(); will register mapper implementations from the calling assembly that implements the interface IMapper.
  • TheCallingAssembly<T>(); will register mapper implementations from the calling assembly that implements the interface T.
  • AssemblyContainingType<T>(); will register mapper implementations from the assembly of the provided type, this is useful if you e.g. have class libraries with Mappers and/or custom implementations of IMapper.
  • Assembly("name"); will register mapper implementations in the assembly, will load assembly via the name provided using System.Reflection, that implements the interface IMapper.

Usage

Creating a mapper class

To create a mapper class use the marker interface IMapper to mark the class and the MapperMethod attribute to mark all methods in the class that are mapping methods.

The global mapper will pick up all classes tagged with the IMapper marker interface and register all methods tagged with the MapperMethod that match the following critera

  • The method is not private
  • The method has a non void return value
  • The method has only one parameter
public class ModelMapper : IMapper 
{
    [MapperMethod] // Will be registered as the mapping method for converting Model -> Entity
    public Entity ModelToEntity(Model model) => 
        new Entity 
        {
            // ...
        };
        
    [MapperMethod] // Will be registered as the mapping method for converting Dto -> Model
    public Model ModelFromDto(Dto dto) => 
        new Model 
        {
            // ...
        };
        
    [MapperMethod] // Will not be registered as the mapping method for converting Dto -> Model is already defined above
    public Model ModelFromDto2(Dto dto) => 
        new Model 
        {
            // ...
        };
        
    // Will not be registered as it lacks the MapperMethod attribute
    public Dto DtoFromModel(Model model) =>
        new Dto 
        {
            // ...
        };
    
    [MapperMethod] // Will not be registered as it is a private method
    private Model ModelFromEntity(Entity entity) =>
        new Model 
        {
            // ...
        };
        
    [MapperMethod] // Will not be registered as it returns void
    public void UpdateModelState(Model model) { /* ... */ }
     
    [MapperMethod] // Will not be registered as it doesn't take a parameter
    public int GetConstantInt() => 42;
        
    [MapperMethod] // Will not be registered as it takes multiple arguments
    public Model ModelFromEntityAndDto(Entity entity, Dto dto) =>
        new Model 
        {
            // ...
        };
}

Using the mapper

The mapper can be used in different ways, either through direct access to the static method

var model = new Model();
var dto = Mapper.Mapper.Map<Dto>(model);
var baseClassDto = Mapper.Mapper.Map<ModelBase, DtoBase>(model);

Or using one of the built in extensions on object or IEnumerable<TSource>

var model = new Model();
var dto = model.Map<Dto>();

var dtos = new [] { new Dto() /*, ... */ };
var models = dtos.Map<Model>();

Using the extensions is the recommended way as it provides a clean way of writing Linq chains, however it can be useful to access the mapper directly if you need to map based on an inherited class.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • 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.3.26 144 3/19/2024
1.2.25 102 3/15/2024
1.2.24 110 3/11/2024
1.2.23 298 1/20/2024
1.2.22 203 11/23/2023
1.2.21 107 11/22/2023
1.2.20 147 9/27/2023
1.1.15 142 7/3/2023
1.1.12 243 3/13/2023
1.1.10 214 3/10/2023
1.0.8 226 3/7/2023