IObjectMapper - Aghyad-Khlefawi/Coddee GitHub Wiki
This interface defines the required functionality for an object mapper. The way an object mapper works is that first you need to register the types you want to convert between then call the Map method to convert to the target type.
| Name | Description |
|---|---|
| RegisterMap[TSource, TTarget] | Register mapping information from source type to target type |
| RegisterMap[TSource, TTarget](Action[TSource, TTarget]) | Register mapping information from source type to target type using a defined action |
| RegisterTwoWayMap[TType1, TType2] | Register mapping information between two types |
| Map[TTarget](object) | Map an object to a new object of specific type |
| MapCollection[TTarget](IList) | Map a collection of objects to a specific type |
| MapInstance[TSource, TTarget](TSource,TTarget) | Maps the values of the source object to the target object |
Before mapping the object you must register it's type first using on of the register methods.
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 BasicObjectMapper();
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);
}