RuntimeAutoMapper

This is a non templated slower version of AutoMapper.

Members

Functions

getMapper
IMapper!(TSource, TDest) getMapper()

Retrive a mapper instance.

map
TDest map(TSource value)
TDest map(TSource source)

Map a source object to a dest object.

transform
TValue transform(TValue value)

Transform a value using registered value transformers.

Variables

converters
Object[TypeInfo] converters;
Undocumented in source.
runtimeMappers
Object[TypeInfo] runtimeMappers;
Undocumented in source.
transformers
Object[TypeInfo] transformers;
Undocumented in source.

Examples

import std.datetime : SysTime;
import automapper;

static class A {
    string foo = "foo";
    long timestamp;
}

static class B {
    string bar;
    SysTime timestamp;
}

RuntimeAutoMapper am = MapperConfiguration!(
    CreateMap!(long, SysTime)
        .ConvertUsing!((long ts) => SysTime(ts)),
    CreateMap!(A, B)
        .ForMember!("bar", "foo"))
            .createMapper().createRuntimeContext();

am.map!B(new A());
import std.datetime : SysTime;

static class Address {
    long zipcode = 42_420;
    string city = "London";
}

static class User {
    Address address = new Address();
    string name = "Foo";
    string lastName = "Bar";
    string mail = "foo.bar@baz.fr";
    long timestamp;
}

static class UserDTO {
    string fullName;
    string email;
    string addressCity;
    long   addressZipcode;
    SysTime timestamp;
    int context;
}

// we would like to map from User to UserDTO
auto am = MapperConfiguration!(
    // create a type converter for a long to SysTime
    CreateMap!(long, SysTime)
        .ConvertUsing!((long ts) => SysTime(ts)),
    // create a mapping for User to UserDTO
    CreateMap!(User, UserDTO)
        // map member using a delegate
        .ForMember!("fullName", (User a) => a.name ~ " " ~ a.lastName )
        // map UserDTO.email to User.mail
        .ForMember!("email", "mail")
        // ignore UserDTO.context
        .Ignore!"context")
        // other member are automatically mapped
        .createMapper();

auto user = new User();
const UserDTO dto = am.map!UserDTO(user);

assert(dto.fullName == user.name ~ " " ~ user.lastName);
assert(dto.addressCity == user.address.city);
assert(dto.addressZipcode == user.address.zipcode);

Naming conventions

static class A {
    int foo_bar_baz = 42;
    string data_processor = "42";
}

static class B {
    int fooBarBaz;
    string dataProcessor;
}

auto am = MapperConfiguration!(
    CreateMap!(A, B)
        .SourceMemberNaming!LowerUnderscoreNamingConvention
        .DestMemberNaming!CamelCaseNamingConvention)
            .createMapper();

A a = new A();
const B b = am.map!B(a);
assert(a.foo_bar_baz == b.fooBarBaz);
assert(a.data_processor == b.dataProcessor);

Type converters

import std.datetime: SysTime;

static class A {
    long timestamp = 1_542_873_605;
}

static class B {
    SysTime timestamp;
}

auto am_delegate = MapperConfiguration!(
    CreateMap!(long, SysTime).ConvertUsing!((long ts) => SysTime(ts)),
    CreateMap!(A, B))
        .createMapper();

A a = new A();
B b = am_delegate.map!B(a);
assert(SysTime(a.timestamp) == b.timestamp);


static class TimestampToSystime : ITypeConverter!(long, SysTime) {
    override SysTime convert(long ts) {
        return SysTime(ts);
    }
}

auto am_class = MapperConfiguration!(
    CreateMap!(long, SysTime).ConvertUsing!TimestampToSystime,
    CreateMap!(A, B))
        .createMapper();

a = new A();
b = am_class.map!B(a);
assert(SysTime(a.timestamp) == b.timestamp);

struct

static struct A {
    int foo;
}

static struct B {
    int foo;
}

auto am = MapperConfiguration!(
    CreateMap!(A, B))
        .createMapper();

A a;
const B b = am.map!B(a);
assert(b.foo == a.foo);

reverse flattening

static class Address {
    int zipcode;
}

static class A {
    Address address;
}

static class B {
    int addressZipcode = 74_000;
}

auto am = MapperConfiguration!(
    CreateMap!(A, B)
        .ReverseMap!())
        .createMapper();

B b = new B();
const A a = am.map!A(b);
assert(b.addressZipcode == a.address.zipcode);

array

import std.algorithm.comparison : equal;

static class Data {
    this() {}
    this(string id) { this.id = id; }
    string id;
}

static class DataDTO {
    string id;
}

static class A {
    int[] foo = [1, 2, 4, 8];
    Data[] data = [new Data("baz"), new Data("foz")];
}

static class B {
    int[] foo;
    DataDTO[] data;
}

auto am = MapperConfiguration!(
    CreateMap!(Data, DataDTO),
    CreateMap!(A, B))
        .createMapper();

MapperConfiguration!(
    CreateMap!(Data, DataDTO),
    CreateMap!(A, B))
        .createMapper();

A a = new A();
B b = am.map!B(a);

assert(b.foo.equal(a.foo));
assert(b.data.length == 2);
assert(b.data[0].id == "baz");
assert(b.data[1].id == "foz");

auto

static class Address {
    long zipcode = 74_000;
    string city = "unknown";
}

static class User {
    Address address = new Address();
    string name = "Eliott";
    string lastName = "Dumeix";
}

static class UserDTO {
    string fullName;
    string addressCity;
    long   addressZipcode;
}

auto am = MapperConfiguration!(
    CreateMap!(User, UserDTO)
        .ForMember!("fullName", (User a) => a.name ~ " " ~ a.lastName ))
        .createMapper();

auto user = new User();
const UserDTO dto = am.map!UserDTO(user);

assert(dto.fullName == user.name ~ " " ~ user.lastName);
assert(dto.addressCity == user.address.city);
assert(dto.addressZipcode == user.address.zipcode);

flattening

static class Address {
    int zipcode = 74_000;
}

static class A {
    Address address = new Address();
}

static class B {
    int addressZipcode;
}

auto am = MapperConfiguration!(
    CreateMap!(A, B)
        .ForMember!("addressZipcode", "address.zipcode"))
        .createMapper();

A a = new A();
const B b = am.map!B(a);
assert(b.addressZipcode == a.address.zipcode);

nest

static class Address {
    int zipcode = 74_000;
}

static class AddressDTO {
    int zipcode;
}

static class A {
    Address address = new Address();
}

static class B {
    AddressDTO address;
}

auto am = MapperConfiguration!(
    CreateMap!(Address, AddressDTO)
        .ForMember!("zipcode", "zipcode")
        .ReverseMap!(),
    CreateMap!(A, B)
        .ForMember!("address", "address")
        .ReverseMap!())
            .createMapper();

A a = new A();
const B b = am.map!B(a);
assert(b.address.zipcode == a.address.zipcode);

// test reversed mapper
am.map!Address(new AddressDTO());

Meta