Git Product home page Git Product logo

safemapper's Introduction

SafeMapper

Build status Coverage Status

The goal with SafeMapper is to have an exception free and extremly fast mapper between primitive and complex types.

The project is influenced by AutoMapper, EmitMapper and FastMapper.

##Key features

  • High speed performance
  • Map between primitive types and string
  • Map array and collections
  • Exception free conversion between most types, fallback to default value for the type
  • Support for both Structs and Classes
  • Support for nested objects
  • Support for public Fields and Properties
  • Support for interfaces IEnumerable, IList, ICollection etc
  • Enum conversion to and from int and string
  • Support for Enums with DisplayAttribute/DescriptionAttribute
  • Conversion from NameValueCollection to Object
  • Conversion from Object to NameValueCollection
  • Mapping support to and from IDictionary<string, T>
  • Simple circular reference check on type-level
  • Configuration support to enable mapping fields/properties with different names

##Installation

http://nuget.org/packages/SafeMapper/

Install-Package SafeMapper

##Usage

The fastest way is to get a converter delegate and reuse that if you should convert many objects

var person = new Person();
var converter = SafeMap.GetConverter<Person, PersonDto>();
var personDto = converter(person);

But it is also possible to call Convert directly

var person = new Person();
var personDto = SafeMap.Convert<Person, PersonDto>(person);

By default members with the same names are mapped to each other, but this can be configured before calling the convert method. It is also possible to configure mapping between properties/fields and methods.

SafeMap.CreateMap<Person, PersonDto>(
    cfg =>
    {
        cfg.Map(x => x.Id, x => x.PersonId);
        cfg.Map(x => x.GetName(), x => x.PersonName);
        cfg.Map(x => x.Length, (x, v) => x.SetPersonLength(v));
        cfg.Map(x => x.Age, "PersonAge");
    });

##Speed test

Converting 100000 objects between CustomerDto and Customer, see SafeMapper.Tests.Model for definition

SafeMapper:     13.7 ms
EmitMapper:     17.0 ms
FastMapper:    142.1 ms
ValueInjector: 337.3 ms
AutoMapper:   2092.0 ms

More speed tests

##Planned features

  • Full Support for circular references between objects
  • Support for flattening
  • Plugin support to extend core functionality (Converters and Membermapping etc)
  • Map from Database queries to c# object
  • Map Lucene documents to c# object

##License

Copyright (C) 2015 Magnus Unger

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions 
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
IN THE SOFTWARE.

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.