Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/jitbit/PropMapper
- Owner: jitbit
- License: mit
- Created: 2019-03-11T15:30:54.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-10-12T09:06:37.000Z (about 3 years ago)
- Last Synced: 2024-03-26T20:57:27.332Z (8 months ago)
- Topics: c-sharp, dotnet, mapper, object-mapper
- Language: C#
- Homepage: https://www.jitbit.com
- Size: 24.4 KB
- Stars: 33
- Watchers: 8
- Forks: 11
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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.