I have a DataReader which contains the result of a stored procedure. The naming convention for the columns use underscores for spaces.
I have been able to successfully map between IDataReader and IEnumerable, but only if the fields match exactly. I do not want to the naming convention used in the stored procedures to dictate the way I name fields in my objects. And the same is true on the database side. I do not think I would be successful enforcing Pascal Case upon the DBAs.
I would like to avoid having to use ForMember() foreach field I need to map. That would defeat the purpose of using AutoMapper.
I found a previous post on the topic which I have used as a reference in my tests. I have not been able to get the correct configuration/mapping for the test to pass successfully. I am hoping someone can assist.
public class DataReaderTests
{
private DTOObject _result;
private IDataReader _dataReader;
protected override void Establish_context()
{
Mapper.Initialize(cfg =>
{
cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
cfg.CreateMap<IDataReader, IEnumerable<DTOObject>>();
});
_dataReader = new DataBuilder().BuildDataReader();
_result = Mapper.Map<IDataReader, IEnumerable<DTOObject>>(_dataReader).FirstOrDefault();
}
[Test]
public void Then_a_column_containing_phone_number_should_be_read()
{
Assert.That(_result.PhoneNumber, Is.EqualTo(_dataReader[FieldName.PhoneNumber]));
}
}
public class DataBuilder
{
public IDataReader BuildDataReader()
{
var resultData = new DataTable();
resultData.Columns.Add(FieldName.PhoneNumber, typeof(string));
var resultDataRow = resultData.NewRow();
resultDataRow[FieldName.PhoneNumber] = "111-222-3333";
resultData.Rows.Add(resultDataRow);
return resultData.CreateDataReader();
}
}
internal class FieldName
{
public const String Id = "id";
public const String Name = "name";
public const String PhoneNumber = "phone_number";
public const String CreateDate = "create_date";
}
public class DTOObject
{
public Guid Id { get; set; }
public string Name { get; set; }
public string PhoneNumber { get; set; }
public DateTime CreatedDate { get; set; }
}