SweetMapper 1.0.0

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

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

SweetMapper

Introduction

SweetMapper is a fast and lightweight object-object mapper library that implements object mapping based on expression tree, The entire library has only one public class:SweetMapper, which is easy to use。 It is a great and faster alternative to Automapper and other .Net mappers.

It targets .NET Standard 2.0+ and .NET 4.6.1+

Get it

PM> Install-Package SweetMapper

Basic usage

Mapping to a new object

TargetClass targetObj = SweetMapper<SourceClass, TargetClass>.Map(SourceObj);

Mapping to a new List

List<SourceClass> SourceList = new List<SourceClass>();
SourceList.Add(SourceObj1);
SourceList.Add(SourceObj2);
List<TargetClass> targetList = SweetMapper<SourceClass, TargetClass>.MapList(SourceList);

Similarly, you can map to a new array

TargetClass[] TargetArray = SweetMapper<SourceClass, TargetClass>.MapArray(SourceArray);

Default mapping rule

SweetMapper works with a set of default mapping rules:

  • Must be a public property of the class
  • Property name must be the same
  • Property type must be the same
  • If the source object is null, the target object generated by the map is also null.

Custom mapping rules

SweetMapper can create custom mapping rules by calling the SetConfig() method.,In this way, it is easy to convert properties of different data types:

class SourceClass
{
    public string Name { get; set; }
    public DateTime DoTime { get; set; }
}
class TargetClass
{
    public string Name { get; set; } = "";
    public string DoTime { get; set; } = "";
}
SweetMapper<SourceClass, TargetClass>.SetConfig((source, target) =>
{
    target.DoTime = source.DoTime.ToString("yyyy-MM-dd");
});
SourceClass sourceObj = new SourceClass
{
    Name = "abc",
    DoTime = new DateTime(2019, 10, 30)
};
TargetClass targetObj = SweetMapper<SourceClass, TargetClass>.Map(sourceObj);

At this time,targetObj.Name == "abc" and targetObj.DoTime== "2019-10-30".

From the above results, you can see that the custom mapping rules are based on the default rules, so you don't have to provide a full mapping custom rule.If you want to change this behavior, you can pass the second argument in the SetConfig() method.

SweetMapper<SourceClass, TargetClass>.SetConfig((source, target) =>
{
    target.DoTime = source.DoTime.ToString("yyyy-MM-dd");
},true);

Passing the second parameter value true means that the custom mapping rule will disable the default rule. At this point, the value of targetObj.Name will be an empty string instead of the previous result "abc".

The SetConfig method is very flexible to use, in addition to mapping different types of properties , you can also map between properties of different names.

If you want to delete a defined mapping rule, you can call the ClearConfig() method.

SweetMapper<SourceClass, TargetClass>.ClearConfig();

performance

Mapping 100,000 classes, hardware environment: I3 CPU, 8G memory, time only: 18 milliseconds.

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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.0

    • 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 489 10/26/2019

0