I am reading the coordinate values from a file using 3rd tool, and tool casts data to a Point
class collection.
public class Point
{
public string Lon {get;set;}
public string Lat {get;set;}
public string Elevation {get;set;}
}
And I want to map Point
class to PointEntity
using automapper.
public class PointEntity
{
public float Lon {get;set;}
public float Lat {get;set;}
public float Elevation {get;set;}
}
I created a Map using Automapper.
public class LocationProfile : Profile
{
public LocationProfile()
{
CreateMap<Point, PointEntity>();
}
}
And I have created a generic mapper extension to map List.
public static class MapperHelper
{
public static TDest MapTo<TDest>(this object src)
{
return (TDest)AutoMapper.Mapper.Map(src, src.GetType(), typeof(TDest));
}
}
IList<Point> data = readData<Point>("file");
var mapped = data.MapTo<PointEntity>();
But sometimes users may have wrong type entries in file like following.
Lon Lat Elevation
--- --- ---------
11.5 25.6 80.56
12ab 89.87 14.83
1.7 x.8 9.3
In this situation, the code throw exception.
So how can I find which row and value is wrong type? (For example row1 and Lon value is wrong)
Change your design thinking. Mapping is simply transferring one data to another data. And automapper is very good at that task. Keep it simple. So, keep that as simple as possible. I read that you want to perform a validation during mapping and provide information about it. That make mapping more complex. In my design I wouldn't let this happen during mapping. Validate this information before mapping. Iterate this before mapping by using an validation rule class. Or validates the data at the time of reading so that you can immediately inform a line and column position.
Example
Use the ConvertUsing (but not needed if validated before) Step to Implementation
Implementation
Validator
Why using a delegate? Other example by collecting all (also for each parameter [lon, lat, ...]) invalide parameters. Here see now the power using a handler for it.