SweetMapper 1.0.0

Simple and Fast Object Mapper

Install-Package SweetMapper -Version 1.0.0
dotnet add package SweetMapper --version 1.0.0
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SweetMapper, 1.0.0"
For F# scripts that support #r syntax, copy this into the source code to reference the package.

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.

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.

Release Notes

0

  • .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 History

Version Downloads Last updated
1.0.0 183 10/26/2019