XMapper 3.0.2

.NET 6.0
NuGet\Install-Package XMapper -Version 3.0.2
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 XMapper --version 3.0.2
<PackageReference Include="XMapper" Version="3.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add XMapper --version 3.0.2
#r "nuget: XMapper, 3.0.2"
#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 XMapper as a Cake Addin
#addin nuget:?package=XMapper&version=3.0.2

// Install XMapper as a Cake Tool
#tool nuget:?package=XMapper&version=3.0.2

XMapper

The object-to-object mapper for C# that can be setup in a readable one-liner. No Attributes, no configuration - just write intuitive and safe code that works immediately. <p align="center"> <img src="https://avatars.githubusercontent.com/u/103217522?s=150&v=4" alt="XMapper logo"/> </p>

Available via NuGet.

For testing of all your object-to-object mappers with one line of unit test code, see XMapper.Testing.

Introduction

There's only one class in this package: XMapper<TSource, TTarget>. It has a single parameter of type PropertyList.

These are all the public methods:

  • IgnoreSourceProperty
  • IgnoreTargetProperty
  • IncludeAction
  • Map (2 overloads)

Hovering over XMapper and its methods in your editor will provide guiding documentation.

Examples

using XMapper;

Map to new

var dummy1 = new Dummy1
{
    MyString = "All members with the same name and type can be copied automatically...",
    MyIntArray = new [] { 2, 3, 5 },
    MyObjectList = new List<MyObject> { new MyObject { Hello = "...even a list of objects!" }, new MyObject() },
};

var mapper = new XMapper<Dummy1, Dummy2>(PropertyList.Source);

var dummy2 = mapper.Map(dummy1);

Map to exising

var d1 = new Dummy1 { SomeString = "I will be copied", Id = "I won't be copied" };
var d2 = new Dummy2 { SomeString = "I will be overwritten", Id = "I will stay" };

var mapper = new XMapper<Dummy1, Dummy2>(PropertyList.Target)
    .IgnoreTargetProperty(x => x.Id);

mapper.Map(d1, d2);

Custom mapping of ValueTypes

var d1 = new Dummy1 { ... };
var d2 = new Dummy2 { ... };

var mapper = new XMapper<Dummy1, Dummy2>(PropertyList.Source)
    .IgnoreSourceProperty(x => x.Number)
    .IncludeAction((source, target) =>
    {
        if (target.MyInt != "An important value")
        {
            target.MyInt = source.Number * 10);
        }
    });

mapper.Map(d1, d2);

Map enumerable members to another target type

From Array to Array with differently reference-typed elements:

var d1Xd2 = new XMapper<Dummy1, Dummy2>(PropertyList.Source);
var mapper = new XMapper<DummyA, DummyB>(PropertyList.Source)
    .IgnoreSourceProperty(x => x.Dummy1Array)
    .IncludeAction((source, target) => target.Dummy2Array = source.Dummy1Array?.Select(x => d1Xd2.Map(x)).ToArray());

From Array to List:

var mapper = new XMapper<DummyA, DummyB>(PropertyList.Source)
    .IgnoreSourceProperty(x => x.XIntArray)
    .IncludeAction((source, target) => target.XIntList = source.XIntArray?.ToList());

Map non-enumerable reference-typed members to another target type

var dummyA = new DummyA
{
    Dummy1Property = new Dummy1
    {
        ...
    },
    ...
};

var d1Xd2 = new XMapper<Dummy1, Dummy2>(PropertyList.Source);
var mapper = new XMapper<DummyA, DummyB>(PropertyList.Target)
    .IgnoreTargetProperty(x => x.Dummy2Property)
    .IncludeAction((dummyA, dummyB) =>
    {
        if (dummyA.Dummy1Property == null)
        {
            dummyB.Dummy2Property = null;
        }
        else
        {
            d1Xd2.Map(dummyA.Dummy1Property, dummyB.Dummy2Property ??= new());
        }
    });

var dummyB = mapper.Map(dummyA);
var dummy2 = dummyB.Dummy2Property;

Map just a few properties from a reference-typed member

var dummyA = new DummyA
{
    ...
    Dummy1Property = new Dummy1
    {
        Name = "Not DummyA, but Dummy1",
        Address = "Deep",
        ...
    },
    ...
};

var mapper = new XMapper<DummyA, DummyB>(PropertyList.Target)
    .IgnoreTargetProperty(x => x.Name)
    .IgnoreTargetProperty(x => x.Address)
    .IncludeAction((source, target) =>
    {
        target.Name = source.Dummy1Property.Name;
        target.Address = source.Dummy1Property.Address;
    });

var dummyB = mapper.Map(dummyA);
var name = dummyB.Name;

Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on XMapper:

Package Downloads
XMapper.Testing

Automate the testing of all your object-to-object mappers with one line of unit test code

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.2 385 4/22/2022
3.0.1 98 4/20/2022
3.0.0 103 4/17/2022
2.0.1 105 4/16/2022
2.0.0 108 4/13/2022
1.0.1 103 4/11/2022
1.0.0 89 4/10/2022
1.0.0-alpha 65 4/10/2022