Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rem0o/dynamicmapper
Dynamic POCO mappers using the Roselyn Compiler.
https://github.com/rem0o/dynamicmapper
csharp-script dotnet-standard orm sql
Last synced: about 5 hours ago
JSON representation
Dynamic POCO mappers using the Roselyn Compiler.
- Host: GitHub
- URL: https://github.com/rem0o/dynamicmapper
- Owner: Rem0o
- Created: 2018-10-20T21:18:31.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-03-27T01:46:53.000Z (over 5 years ago)
- Last Synced: 2024-05-02T00:48:54.216Z (7 months ago)
- Topics: csharp-script, dotnet-standard, orm, sql
- Language: C#
- Homepage:
- Size: 15.6 KB
- Stars: 8
- Watchers: 1
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# DynamicMapper [![Build Status](https://travis-ci.com/Rem0o/DynamicMapper.svg?branch=master)](https://travis-ci.com/Rem0o/DynamicMapper)
Create all your POCO mappers dynamically from your POCO types directly using static reflection and the Roselyn compiler.
The mappers can be compiled once dynamically at startup. Once created, the mappers are then cached. Each mapper should give similar performance to manual object mapping directly from your source object.
### Code exemple
```c#
// Compiles mappers for all classes under a specific namespace at startup to map from a IDataReader source object;
var mapperContainer = new DynamicMapperContainer((reader, propertyName) => reader[propertyName])
.CompileMappers(this.GetType().Assembly, t => t.Namespace == typeof(MyPocoClass).Namespace);
// (...)// If the type was not included during the container construction, the container will try to compile the mapper
// for the given class dynamically and cache it for later use.
mapperContainer.GetMapper(out Action mapper);// use mapper here ...
using (var connection = new SqlConnection("some connection string"))
{
SqlCommand command = new SqlCommand("SELECT * FROM SomeTable", connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
var poco = new MyPocoClass();
mapper(reader, poco);
// poco was mapped!
}
reader.Close();
}```
### Some performance testing
Here are some basic performance testing comparing manual mapping, DynamicMapper and Dapper. The results are for demonstration purpose only and may not reflect your personal results.Test conditions:
- Mapping a simple POCO with 7 properties
- 100 000 itterations
- Manual mapping and DynamicMapper both use a basic SqlConnection/SqlCommand/SqlDataReader implementation like the code exemple above.
- Dapper uses the IDbConnection.Query extension method.Results:
| Mapper | Time (ms) |
| -------------- | --------- |
| DynamicMapper | 917 |
| Manual mapping | 669 |
| Dapper | 1061 |