Automapper 구성에 포맷터를 추가하여 모든 DateTime?
필드의 스타일을 지정하려고합니다.Automapper Formatter가 작동하지 않습니다.
Mapper.AddFormatter<DateStringFormatter>();
을 그리고 특정 매핑 자체에 : 나는 세계적으로 내 포매터를 추가 해봤도
Mapper.CreateMap<Post, PostViewModel>()
.ForMember(dto => dto.Published, opt => opt.AddFormatter<DateStringFormatter>());
그러나 작동하는 것 같다 - 항상 정상적인 형식으로 날짜를 출력합니다. 참고로, 여기에 내가 사용하고 뷰 모델 및 구성의 나머지 부분은 다음과 같습니다
public class DateStringFormatter : BaseFormatter<DateTime?>
{
protected override string FormatValueCore(DateTime? value)
{
return value.Value.ToString("d");
}
}
public abstract class BaseFormatter<T> : IValueFormatter
{
public string FormatValue(ResolutionContext context)
{
if (context.SourceValue == null)
return null;
if (!(context.SourceValue is T))
return context.SourceValue == null ? String.Empty : context.SourceValue.ToString();
return FormatValueCore((T)context.SourceValue);
}
protected abstract string FormatValueCore(T value);
}
PostViewModel : 내가 잘못
public int PostID { get; set; }
public int BlogID { get; set; }
public string UniqueUrl { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public string BodyShort { get; set; }
public string ViewCount { get; set; }
public DateTime CreatedOn { get; set; }
private DateTime? published;
public DateTime? Published
{
get
{
return (published.HasValue) ? published.Value : CreatedOn;
}
set
{
published = value;
}
}
을 뭐하는 거지?
감사합니다.
감사합니다 지미, 옵션 2와 함께갔습니다. 이 같은 유형의 필드를 가진 몇 가지 다른 소스 객체가 있습니다. Automapper로 시작하여 지금까지 그것을 좋아합니다. – leftend
어떻게했는지 샘플을 가지고 있습니까? – ntombela