Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jitbit/PropMapper

Object mapper for .NET. Flat and basic, but FAST.
https://github.com/jitbit/PropMapper

c-sharp dotnet mapper object-mapper

Last synced: 5 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# PropMapper

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

Just [one cs-file](PropMapper.cs), only 80 lines of code.

## Installation

Drop the cs-file into your project OR install via [Nuget](https://www.nuget.org/packages/PropMapper/)

`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:

```cs
//instantiating a new object
DestType destObject = PropMapper.From(srcObj);
```

or

```cs
//using with existing objects
PropMapper.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:

```cs
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

```cs
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.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](https://www.jitbit.com/) of ours, so it's regularly unit-tested anyway.