I understand that I can configure AutoMapper as follows, and during the comparison, it must format all the dates of the original model with the rules defined in IValueFormatter and set the result for the associated model.
ForSourceType<DateTime>().AddFormatter<StandardDateFormatter>();
ForSourceType<DateTime?>().AddFormatter<StandardDateFormatter>();
I get no effect for my mapped class with this. It only works when I do the following:
Mapper.CreateMap<Member, MemberForm>().ForMember(x => x.DateOfBirth, y => y.AddFormatter<StandardDateFormatter>());
Am I Matching DateTime? Member.DateOfBirth to string MemberForm.DateOfBirth . The formatter basically creates a short string of dates from the date.
Is there something that I am missing when setting the standard formatting for this type?
thank
public class StandardDateFormatter : IValueFormatter
{
public string FormatValue(ResolutionContext context)
{
if (context.SourceValue == null)
return null;
if (!(context.SourceValue is DateTime))
return context.SourceValue.ToNullSafeString();
return ((DateTime)context.SourceValue).ToShortDateString();
}
}