/PropMapper

Object mapper for .NET. Flat and basic, but FAST.

Primary LanguageC#MIT LicenseMIT

PropMapper

Property mapper for .NET. Flat and basic, but VERY FAST.

Just one cs-file, only 80 lines of code.

Installation

Drop the cs-file into your project OR install via Nuget

Install-Package PropMapper

(this will will simply add the .cs file to your project, not a DLL-reference, so you're free of dependencies)

Usage

Just one line of code:

//instantiating a new object
DestType destObject = PropMapper<SourceType, DestType>.From(srcObj);

or

//using with existing objects
PropMapper<SourceType, DestType>.CopyTo(srcObj, destObj);

Benchmarks

Mapping a simple object with 50 properties, over 100k iterations.

Results:

Mapper Results
Automapper 32490ms
Automapper with cached config object 335ms
PropMapper 25ms
Manual code 10ms

PropMapper is more than 13 times faster. Here's the class we tested on:

public class Tester
{
	public string prop1 { get; set; }
	public string prop2 { get; set; }
	public string prop3 { get; set; }
	public int iprop1 { get; set; }
	//etc. 50 times
}

Under the hood

We use compiled Expression trees, created in the static constructor and cached in a var, which makes it really fast.

Use case: casting base class to derived class

public class Person
{
	public string FirstName { get; set; }
	public string LastName { get; set; }
}
public class Employee : Person
{
	public string Title { get; set; }
	
	public Employee(Person person)
	{
		PropMapper<Person, Employee>.CopyTo(person, this);
	}
}

What's next?

Currently it's just one C# file, with unit tests, a proper library and more benchmarks coming later. The tool is in heavy use in other projects of ours, so it's regularly unit-tested anyway.