Skip to content

ILObjectsMapper

Aghyad khlefawi edited this page Apr 21, 2017 · 1 revision

A simple ObjectMapper implementation. This implementation is much faster than BasicObjectMapper because it uses generated IL to map object using dynamic methods created at runtime. The only drawback of this implementation is that it can't be used in Portable class libraries.

Remarks

The current implementation only supports one level of mapping but it's very fast for simple POCO objects mapping

C# Example:

public class Person : IUniqueObject<int>
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public int GetKey
    {
        get { return ID; }
    }
}
public class PersonDbModel : IUniqueObject<int>
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public int GetKey
    {
        get { return ID; }
    }
}

public void map()
{
    IObjectMapper mapper = new ILObjectsMapper();
    PersonDbModel sourceItem = new PersonDbModel {ID = 1, FirstName = "Aghyad"};

    //Auto registration
    mapper.RegisterMap<PersonDbModel, Person>();
    //result will have the same values as sourceItem
    Person result = mapper.Map<Person>(sourceItem);

    //Manual registration
    mapper.RegisterMap<PersonDbModel, Person>((source, target) =>
    {
        target.ID = source.ID;
        target.FirstName = source.FirstName;
    });
    //result2 will have the same values as sourceItem
    Person result2 = mapper.Map<Person>(sourceItem);
}
Clone this wiki locally