automapper 1.1.0
D automapper
To use this package, run the following command in your project's root directory:
Manual usage
Put the following dependency into your project's dependences section:
AutoMapper
AutoMapper is an object-object mapper for D like C# AutoMapper.
It use compile-time generated mapper to try to avoid any overhead.
You can run the benchmark located in tests/benchmark
:
$ cd tests/benchmark
$ dub test
Build documentation
This project is well documented:
$ dub build --build=ddox
Full exemple
import std.stdio;
import automapper;
class Order
{
Customer customer;
Product product;
}
class Product
{
float price;
string name;
}
class Customer
{
string name;
string city;
string email;
}
class OrderDTO
{
string customerName;
string customerCity;
string customerEmail;
float productPrice;
string productName;
}
auto makeOrder()
{
auto order = new Order();
order.customer = new Customer();
order.customer.name = "boolangery";
order.customer.city = "Annecy";
order.customer.email = "foo.bar@gmail.com";
order.product = new Product();
order.product.price = 42;
order.product.name = "universe";
return order;
}
void main()
{
// create a compile-time generated mapper to map from Order to OrderDTO,
// and from OrderDTO to Order.
auto mapper = MapperConfiguration!(
CreateMap!(Order, OrderDTO)
.ReverseMap!())
.createMapper();
auto initial = makeOrder();
auto dto = mapper.map!OrderDTO(initial); // map Order to OrderDTO
auto order = mapper.map!Order(dto); // map back OrderDTO to Order
assert(order.customer.name == initial.customer.name);
assert(order.customer.city == initial.customer.city);
assert(order.customer.email == initial.customer.email);
assert(order.product.price == initial.product.price);
assert(order.product.name == initial.product.name);
// more suitable runtime mapper
RuntimeAutoMapper m = mapper.createRuntimeContext();
}
Run Time
To be as fast as possible, AutoMapper do by deafult all its magic at compile-time (See example bellow). Mapping check are static asserted.
It implies that AutoMapper type is specialized using templates:
alias AM = AutoMapper!(MapperConfiguration!(CreateMap!(Order, OrderDTO, ReverseMapConfig)));
AM mapper = MapperConfiguration!(
CreateMap!(Order, OrderDTO)
.ReverseMap!())
.createMapper();
It may be difficult to pass this complex object along your framework. Its why you can use a slower version, that check things at runtime.
The example above became:
RuntimeAutoMapper mapper = MapperConfiguration!(
CreateMap!(Order, OrderDTO)
.ReverseMap!())
.createMapper()
.createRuntimeContext();
Custom member mapping
import automapper;
import std.datetime;
class A {
long timestamp;
}
class B {
SysTime timestamp;
}
auto am = MapperConfiguration!(
CreateMap!(long, SysTime)
.ConvertUsing!((long ts) => SysTime(ts)),
CreateMap!(A, B))
.createMapper();
- 1.1.0 released 5 years ago
- boolangery/d-automapper
- MIT
- Authors:
- Dependencies:
- none
- Versions:
-
1.1.0 2019-Jan-24 1.0.0 2019-Jan-15 ~master 2019-Jan-24 - Download Stats:
-
-
0 downloads today
-
0 downloads this week
-
0 downloads this month
-
12 downloads total
-
- Score:
- 0.6
- Short URL:
- automapper.dub.pm