Skip to content

Instantly share code, notes, and snippets.

@lbargaoanu
Created October 11, 2018 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbargaoanu/82213f93d73642e0c24b515c94914129 to your computer and use it in GitHub Desktop.
Save lbargaoanu/82213f93d73642e0c24b515c94914129 to your computer and use it in GitHub Desktop.
public void Main()
{
var config = new MapperConfiguration(cfg => {
cfg.CreateMissingTypeMaps = false;
cfg.CreateMap<DateTimeDetails, PickupDetails>().ForMember(dest => dest.time, opt => opt.MapFrom(src => src));
cfg.CreateMap<DateTime?, DateString>().ConvertUsing(src => new DateString { Value = src.HasValue ? src.Value.ToString("yyyyMMdd") : String.Empty});
cfg.CreateMap<DateTimeDetails, TimeString>()
.ForMember(dest => dest.timeZoneOffset, opt => opt.MapFrom(src => src.TimeZoneOffset.ToString()))
.ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.Time));
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
var srcObj = new DateTimeDetails{ Time = "06:43:25", TimeZoneOffset = -6 };
//Toggle this with one map then the other
//srcObj.Date = new DateTime(2018, 10, 11);
try
{
mapper.Map<PickupDetails>(srcObj).Dump();
}
catch(Exception ex)
{
ex.ToString().Dump();;
}
}
public class DateTimeDetails
{
public DateTime? Date { get; set; }
public String Time { get; set; }
public Int32 TimeZoneOffset { get; set; }
}
public class PickupDetails
{
public TimeString time { get; set; }
public DateString date { get; set; }
}
public class DateString
{
public String Value { get; set; }
}
public class TimeString
{
public String timeZoneOffset { get; set; }
public String Value { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment