This project is read-only.
Given these classes that we want to map between:

public class GuiPerson
{
    public String Id { get; set; }
}

public class DomainPerson
{
    public int Id { get; set; }
}

We want to map between the Id properties, but they have different types (int and string). To solve this all we need to do is to add a converter, and then carry on as normal.

var mapping = new Mapping<DomainPerson, GuiPerson>();
mapping.AddConverter(Converting.BetweenIntAndString());
mapping.AutoRelateEqualNames();

var domainPerson = new DomainPerson {Id = 3};
var guiPerson = new GuiPerson();

mapping.Map(domainPerson, guiPerson);

Assert.Equal("3",guiPerson.Id);

The IntString-converter is built in, and you get it by calling: Converting.BetweenIntAndString(). More built in converters will be added in future releases.

Building your own converters

The easiest way to create your own typeconverter is to use the QuickConverter. It takes two delegates that describe how to convert between the two types, in both directions.

var converter = new QuickConverter<int, bool>(fromInt => fromInt != 0, fromBool => fromBool?1:0);
Assert.Equal(false, converter.MapTowardsRight(0));
Assert.Equal(true, converter.MapTowardsRight(13));
Assert.Equal(1, converter.MapTowardsLeft(true));
Assert.Equal(0, converter.MapTowardsLeft(false));

The first parameter is how to convert from int to bool, and the second is how to convert from bool to int.

You add the QuickConverter as a normal converter to the mapping, like this:

mapping.AddConverter(converter);

Last edited Aug 8, 2009 at 8:07 AM by ToreVestues, version 5

Comments

No comments yet.