Explicit.Mapper 1.0.0

.NET Standard 2.0
NuGet\Install-Package Explicit.Mapper -Version 1.0.0
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.
dotnet add package Explicit.Mapper --version 1.0.0
<PackageReference Include="Explicit.Mapper" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Explicit.Mapper --version 1.0.0
#r "nuget: Explicit.Mapper, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Explicit.Mapper as a Cake Addin
#addin nuget:?package=Explicit.Mapper&version=1.0.0

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

ExplicitMapper

A mapping framework that is completely explicit.

No magic

Type mapping and member mapping must be fully defined.
If it's not explicitly mapped, it won't be mapped.

Easy to use

Base classes are supplied for mapping classes (reference types), structs (value types), and strings.
Collection and asynchronous mapping is supported without additional effort.
Common mappings are included out-of-the-box. For example, the XmlSerializerMapper is a specialist type of StringMapper.

DI framework friendly

Complex or nested mappings can be constructed through constructor injection in your favourite DI framework.
Examples can be found in the Dependency Injection page.
All mappers implement the same generic interface for simple installation and use.

public interface IMapper<TSource, TDestination>
{
    // Map a source object to a destination object.
    TDestination Map(TSource source);

    // Map a source collection to a destination collection.
    IEnumerable<TDestination> Map(IEnumerable<TSource> source);

    // Map a source object to a destination object asynchronously.
    Task<TDestination> MapAsync(TSource source);

    // Map a source collection to a destination collection asynchonously.
    Task<IEnumerable<TDestination>> MapAsync(IEnumerable<TSource> source);

    // Map a source collection to a destination collection concurrently and asynchronously.
    Task<IEnumerable<TDestination>> MapParallel(IEnumerable<TSource> source);
}

Mapper examples

Class mapper for reference types

The ClassMapper is used to map reference types. It is an abstract class, it implements the IMapper interface, and it includes a single abstract Map method your mapper must implement.

protected abstract void Map(TSource source, TDestination destination);

The source object and a new instance of the destination class are supplied. The method must map from the source to the destination.

protected override void Map(UserRegistrationModel source, Address destination)
{
    destination.AddressLine1 = source.AddressLine1;
    
    destination.AddressLine2 = source.City;

    destination.PostalCode = source.Postcode;
}

Struct mapper for value types

The StructMapper is used to map value types. It is an abstract class, it implements the IMapper interface, and it includes a single abstract Map method your mapper must implement.

protected abstract void Map(TSource source, out TDestination destination);

The source object is supplied alongside an 'out' destination parameter. The method must assign to the out parameter from the source.

protected override void Map(UserRegistrationModel source, out DateTime destination)
{
    destination = new DateTime(source.YearOfBirth, source.MonthOfBirth, source.DayOfBirth);
}

String Mapper

The StringMapper is a special case mapper for strings. Like the other mapper base classes it is abstract, it implements the IMapper interface, and it includes a single abstract Map method your mapper must implement.

protected abstract void Map(TSource source, StringBuilder destination);

The source object and a destination StringBuilder are supplied. The method must use the StringBuilder to build the string from the source object.

Asynchronous collection mapping

Two asynchronous collection mapping methods are available, MapAsync and MapParallel.

public async Task<IEnumerable<TDestination>> MapAsync(IEnumerable<TSource> source);

public async Task<IEnumerable<TDestination>> MapParallel(IEnumerable<TSource> source);

Their signatures are the same, but their behaviour is different:

MapAsync executes the collection mapping asynchronously, but within the async Task sequentially maps each item in the collection.

o----MapAsync(items)----------------------------------------o
          \                                           /
            Map(items[0]) Map(items[1]) Map(items[2])

MapParallel executes the mapping of each item in the collection asynchronously, completing the async mapping when all items have been mapped.

o----MapParallel(items)-------------------------------------o
          \               /
          \ Map(items[0])
          \ Map(items[1])
            Map(items[2])

The most appropriate asynchronous collection mapping method to use will depend on each specific use case. The size of the collection and the complexity of the mapping should be considered.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • .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.0.0 324 2/13/2020

Initial release.