Giter Site home page Giter Site logo

dynamicmapper's Introduction

DynamicMapper Build Status

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

// Compiles mappers for all classes under a specific namespace at startup to map from a IDataReader source object;
var mapperContainer = new DynamicMapperContainer<IDataReader>((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<MyPocoClass>(out Action<IDataReader, MyPocoClass> 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

dynamicmapper's People

Contributors

rem0o avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.