Currently Automapper is playing a major role in mapping DTO into Business object or vice versa. Likewise I planned to utilize his DynamicMap method to map DataReader value into DTO object, since it will reduce multiple lines of code to retrieve field ordinal and the lot of un-boxing method calls to retrieve the value from DataReader based on ordinal. However I bit worried about the performance of the DynamicMap method, so to analyze the created a sample program to evaluate the performance of manual and DynamicMap
Created a employee class with 6 different types of fields to check the performance of mapping 1000 records by Automapper and manual mappingpublic class EmployeeThe following manual mapping is taking 00:00:00.0020151 time to map the records into DTO from DataReader
{
public string Name { set; get; }
public int Id { set; get; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public DateTime DateOfBirth { get; set; }
public long Wage { get; set; }
}
private static void MapReaderIntoDTOObject(DataTableReader reader, List<Employee> employees)However in background the same lines of code are executing for 1000 times
{
while (reader.Read())
{
var employee = new Employee()
{
Name = reader.GetString(0),
Address1 = reader.GetString(2),
Address2 = reader.GetString(3),
DateOfBirth = reader.GetDateTime(4),
Wage = reader.GetInt64(5),
Id = reader.GetInt32(1)
};
employees.Add(employee);
}
}
Replaced the same mapping with AutoMapper.DynamicMap method, however found the execution time 00:00:00.0230023 is bit higher than manual, but as mentioned it will have single to map the entire values
employees.AddRange(AutoMapper.Mapper.DynamicMap<DbDataReader, IList<Employee>>(reader));
Also did the comparison of AutoMapper.Map (since in our business layer we are using to map DTO to BO and vice versa) with manual mapping it is also bit slower than manual mapping.
The overall execution time of each mapping is (it will be vary on each run based on system load)
Manual Mapping Of DataReader into DTO objects: 00:00:00.0020151The time difference in between manual and DynamicMapping mapping is 0.0209872, I felt it is acceptable one when compare to the manual mapping with AutoMapper.Map (Also it will reduce lot of manual process, like getting ordinal, conditional check to assign default value, etc.,).
AutoMapper Mapping of DataReader into DTO objects: 00:00:00.0230023
Manual Mapping of Object to DTO objects: 00:00:00.0010004
AutoMapper Mapping of Object into DTO objects: 00:00:00.0030005
Sample source can be found in this link.