Getting started

What is Expression Mapper?

Expression Mapper is an object-to-object mapper. Object-object mapping works by transforming an input object of one type into an output object of a different type. Expression Mapper uses conventions for generating mapping strategy, almost zero configuration is needed to map two types. Expression Mapper can be customized with expression trees.

Why use Expression Mapper?

Expression Mapper creates fast mapping code, even faster than handwritten mapping. Convention-based mapping reduces lines of code in large projects. Expression Mapper provides very powerfull and safe customization by expression trees.

How to use Expression Mapper?

Hello World program with Expression Mapper.
using System;
using ExpressionMapper;
public class ExpressionMapperHelloWorld
{
    public static void Main(params string[] args)
    {
        Func<string,string> mapper = Mapper.Create<string,string>();
        Console.WriteLine(mapper("Hello World!"));
    }
}

Mapper is a static facade for creating mapping functions. Create method takes two type parameter that represent source and destination types and returns mapping function.

More complex example.

We have two different classes CompexA and ComplexB
public class A
{
    public int P1 { get; set; }
    public string P2 { get; set; }
    public float P3 { get; set; }
}

public class B
{
    public string P1 { get; set; }
    public string P2 { get; set; }
    public double P3 { get; set; }
}

public class ComplexA
{
    public A P1 { get; set; }
    public int P2 { get; set; }
    public DateTime P3 { get; set; }
    public A P4 { get; set; }
}

public class ComplexB
{
    public B P1 { get; set; }
    public float P2 { get; set; }
    public string P3 { get; set; }
    public B P4 { get; set; }
}

Mapping ComplexA to ComplexB
var now = DateTime.Now;

var ca = new ComplexA
{
    P1 = new A { P1 = 1, P2 = "2", P3 = 3.45f },
    P2 = 6,
    P3 = now,
    P4 = new A { P1 = 7, P2 = "8", P3 = 9.0f }
};
var mapper = Mapper.Create<ComplexA, ComplexB>();
var cb = mapper(ca);

Test code
Assert.AreEqual("1", cb.P1.P1);
Assert.AreEqual("2", cb.P1.P2);
Assert.IsTrue((3.45 - cb.P1.P3) < 0.0000001);

Assert.IsTrue((6 - cb.P2) < 0.0000001);
Assert.AreEqual(now.ToString(), cb.P3);

Assert.AreEqual("7", cb.P4.P1);
Assert.AreEqual("8", cb.P4.P2);
Assert.IsTrue((9.0 - cb.P4.P3) < 0.0000001);

Expression Mapper uses Convert class to convert built-in types.

Last edited Jan 25, 2010 at 11:26 AM by gandjustas, version 1

Comments

No comments yet.