using System.Globalization;
public static void Main()
var container = Bootstrapper.RegisterTypes(new UnityContainer());
var runner = container.Resolve<Application>();
private readonly IMapper _mapper;
public Application(IMapper mapper)
var patientDto = new UKPatientDTO { PersonalInformation = new UKPerson { GivenName = "Jimmy", SurName = "Chandra" }, Temperature = "37.8" };
var patient = _mapper.Map<Patient>(patientDto);
Console.WriteLine($"{patient.PersonalInformation.FirstName} {patient.PersonalInformation.LastName} ({patient.Temperature} Celcius)");
var r = _mapper.Map<UKPatientDTO>(patient);
Console.WriteLine($"{r.PersonalInformation.GivenName} {r.PersonalInformation.SurName} ({patient.Temperature} Celcius)");
public class Bootstrapper
public static IUnityContainer RegisterTypes(IUnityContainer c)
var config = new MapperConfiguration(cfg => cfg.AddProfile<MyProfile>());
c.RegisterInstance(config.CreateMapper());
c.RegisterType<Application>();
public sealed class StringToNullableDecimalConverter : ITypeConverter<string, decimal?>
public decimal? Convert(string source, decimal? destination, ResolutionContext context)
if (source is null) return default;
if (string.IsNullOrWhiteSpace(source)) return default;
var upperSource = source.ToUpperInvariant().Trim();
if (upperSource == "NAN" || upperSource == "-INFINITY") return default;
if (decimal.TryParse(source, NumberStyles.Any, CultureInfo.CreateSpecificCulture("en"), out result))
if (decimal.TryParse(source, NumberStyles.Any, CultureInfo.CreateSpecificCulture("es"), out result))
var ci = CultureInfo.CreateSpecificCulture("en");
ci.NumberFormat = new NumberFormatInfo { NumberGroupSeparator = " ", NumberDecimalSeparator = "," };
if (decimal.TryParse(source, NumberStyles.Any, ci, out result))
if (source.EndsWith("%"))
return decimal.Parse(source.Replace("%", ""));
public class MyProfile : Profile
CreateMap<string, decimal?>().ConvertUsing<StringToNullableDecimalConverter>();
CreateMap<UKPerson, USPerson>()
.ForMember(d => d.FirstName, o => o.MapFrom(s => s.GivenName))
.ForMember(d => d.LastName, o => o.MapFrom(s => s.SurName))
CreateMap<UKPatientDTO, Patient>()
.ForMember(d => d.PersonalInformation, o => o.MapFrom(s => s.PersonalInformation))
public string FirstName { get; set; }
public string LastName { get; set; }
public USPerson(string firstName, string lastName)
public string GivenName { get; set; }
public string SurName { get; set; }
public class UKPatientDTO
public UKPerson PersonalInformation { get; set; }
public string Temperature { get; set; }
public USPerson PersonalInformation { get; set; }
public decimal? Temperature { get; set; }