Giter Site home page Giter Site logo

dogusteknoloji / batmap Goto Github PK

View Code? Open in Web Editor NEW
218.0 31.0 25.0 194 KB

๐Ÿฆ‡ Convention-based, fast object mapper.

Home Page: https://dogusteknoloji.github.io/BatMap/

License: MIT License

C# 100.00%
mapper projector mapping dto entity

batmap's Introduction

BatMap - The Mapper we deserve, not the one we need.

๐Ÿฆ‡ BatMap Opininated (yet another) mapper, mainly to convert between EF Entities and DTOs.

Supports .Net Standard 1.0.

Build status Coverage Status NuGet Badge Join the chat at https://gitter.im/NaNaNaNaBatMap/Lobby GitHub issues GitHub license

GitHub stars GitHub forks

Let's first obey the number one rule for mappers, a benchmark (using BenchmarkDotNet):

Method Mean
HandWritten 1.143 ms
BatMap ๐Ÿ’ฅ 2.000 ms
SafeMapper 2.933 ms
Mapster 2.942 ms
AutoMapper 3.497 ms
TinyMapper 4.172 ms
ExpressMapper 6.955 ms
FastMapper 9.203 ms

Results may (probably) vary. Latest run can bee seen on Appveyor Build.

  • Fast (enough)
  • NOT over-engineered, code is really simple
  • Instantiatable mapper
  • Convention based, zero configuration static shortcut exists too (obviously named Mapper)
  • Does not crash when faced with circular-dependencies during registration
  • In fact, can resolve recurring instances to same target instance (yaay no StackOverflowException!)
  • Can project IQueryable<TSource> to IQueryable<TTarget> with respect to includes (via auto-detection or with custom parameters)
  • and much more...

API

Registration with static API:

Mapper.RegisterMap<Customer, CustomerDTO>();

or use an instance:

var mapper = new MapConfiguration(dynamicMapping: DynamicMapping.MapAndCache, preserveReferences: true);
mapper.RegisterMap<Customer, CustomerDTO>();

Note: You don't have to register type mappings when using a MapConfiguration with Dynamic Mapping enabled (like the static API uses).

You can customize expressions for members:

mapper.RegisterMap<Order, OrderDTO>(b => b.MapMember(o => o.Price, (o, mc) => o.Count * o.UnitPrice));

Map an object:

Mapper.Map<CustomerDTO>(customer);

Map an enumerable:

customers.MapTo<Customer, CustomerDTO>(preserveReferences: true);  // extension methods FTW!

Project a query:

customerQuery.ProjectTo<CustomerDTO>(checkIncludes: true);

or with expanding specific navigations:

customerQuery.ProjectTo<Customer, CustomerDTO>(c => c.Addresses, c => c.Orders);

Note: If you want to change mapping behavior, create a class that inherits from ExpressionProvider, override CreateMemberBinding and inject an instance of your class to MapConfiguration.

Where can I get it?

You can install BatMap from the package manager console:

PM> Install-Package BatMap

Documentation

You might want to visit wiki for more.


Developed with โค๏ธ at DoฤŸuลŸ Teknoloji.

batmap's People

Contributors

gitter-badger avatar jtone123 avatar umutozel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

batmap's Issues

A bit strange benchmark results

It's a bit strange that HandWritten is not the first. BatMap is a really nice project, but benchmark result looks a bit strange. Looks like HandWrittensample has a bug, yes it's possible to do this kind of mapping but it's a bit inefficient. try to use ConverAll method, it'll remove all ToList methods (except one, IList does not have ConvertAll

like this

public void HandWritten()
{
	var customerDTOs = _customers.Select(c => new CustomerDTO
	{
		Id = c.Id,
		Addresses = c.Addresses.ConvertAll(a => new AddressDTO
		{
			City = new CityDTO
			{
				Id = a.City.Id,
				Name = a.City.Name,
				Population = a.City.Population
			},
			Detail = a.Detail,
			Id = a.Id
		}),
		CompanyName = c.CompanyName,
		Endorsement = c.Endorsement,
		Orders = c.Orders.ConvertAll(o => new OrderDTO
		{
			Id = o.Id,
			OrderDetails = o.OrderDetails.ConvertAll(od => new OrderDetailDTO
			{
				Id = od.Id,
				Count = od.Count,
				UnitPrice = od.UnitPrice
			}),
			OrderNo = o.OrderNo,
			Price = o.Price
		}),
		Phone = c.Phone
	}).ToList();
}

BTW, I think other mappers support collection mapping, so it's possible to map without ToList

For instance, there's no Select

        public void BatMap() {
            var customerDTOs = _customers.Map<Customer, CustomerDTO>().ToList();
        }

but here's Select and ToList

        [Benchmark]
        public void TinyMapper() {
            var customerDTOs = _customers.Select(c => Nelibur.ObjectMapper.TinyMapper.Map<Customer, CustomerDTO>(c)).ToList();
        }

        [Benchmark]
        public void ExpressMapper() {
            var customerDTOs = _customers.Select(c => global::ExpressMapper.Mapper.Map<Customer, CustomerDTO>(c)).ToList();
        }

for any performance comparisonce, it's really impotant compare the same things in an equeal environment.

Map<TIn, TOut>(src, dest)

apply mapping to an existing object.

possible use-case
user posts an update (using a DTO) to an existing object. the service loads the existing object from the DB, and then use the map(dto, entity) to apply the updates as required.

Example Usage

public void Update(PersonDto personDto) {
    var person = _session.Get<Person>(personDto.id);
    _mapper.Map(personDto, person);
}

note that the dto may only contain a subset of the entity properties

"Ambiguous match found" when getting property

Reported by: tungtrungvn

BatMap version

1.1.12

Steps to reproduce

It happens when we use "new" keyword to hide property from parent class

Expected behavior

Should use property from inherited class

Actual behavior

Throws "Ambiguous match found" exception

Doesn't detect ThenInclude navigations from queries

BatMap version

1.1.2

Steps to reproduce

Using ThenInclude with queries

Expected behavior

Should map all Include and ThenInclude navigations

Actual behavior

Only maps Include navigations, skips ThenInclude navigations

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.