Configuration AutoMapper:针对给定类型在站点范围内使用IValueFormatter

Configuration AutoMapper:针对给定类型在站点范围内使用IValueFormatter,configuration,automapper,Configuration,Automapper,据我所知,我可以用以下方式配置AutoMapper,在映射过程中,它应该将所有源模型日期格式化为IValueFormatter中定义的规则,并将结果设置为映射模型 ForSourceType<DateTime>().AddFormatter<StandardDateFormatter>(); ForSourceType<DateTime?>().AddFormatter<StandardDateFormatter>(); 我正在使用AutoMap

据我所知,我可以用以下方式配置AutoMapper,在映射过程中,它应该将所有源模型日期格式化为IValueFormatter中定义的规则,并将结果设置为映射模型

ForSourceType<DateTime>().AddFormatter<StandardDateFormatter>();
ForSourceType<DateTime?>().AddFormatter<StandardDateFormatter>();

我正在使用AutoMapper v1

那里有一个抽象类,它完成了大部分的grunt工作,称为ValueFormatter

我的代码:

public class DateStringFormatter : ValueFormatter<DateTime>
{
    protected override string FormatValueCore(DateTime value)
    {
        return value.ToString("dd MMM yyyy");
    }
}
公共类DateStringFormatter:ValueFormatter { 受保护的重写字符串FormatValueCore(日期时间值) { 返回值.ToString(“dd-MMM-yyyy”); } } 然后在我的个人资料课上:

public sealed class ViewModelMapperProfile : Profile
{
    ...

    protected override void Configure()
    {
        ForSourceType<DateTime>().AddFormatter<DateStringFormatter>();

        CreateMap<dto, viewModel>()
            .ForMember(dto => dto.DateSomething, opt => opt.MapFrom(src => src.DateFormatted));
公共密封类ViewModelMapperProfile:Profile
{
...
受保护的覆盖无效配置()
{
ForSourceType().AddFormatter();
CreateMap()
.ForMember(dto=>dto.DateSomething,opt=>opt.MapFrom(src=>src.DateFormatted));

}我正在使用AutoMapper v1

那里有一个抽象类,它完成了大部分的grunt工作,称为ValueFormatter

我的代码:

public class DateStringFormatter : ValueFormatter<DateTime>
{
    protected override string FormatValueCore(DateTime value)
    {
        return value.ToString("dd MMM yyyy");
    }
}
公共类DateStringFormatter:ValueFormatter { 受保护的重写字符串FormatValueCore(日期时间值) { 返回值.ToString(“dd-MMM-yyyy”); } } 然后在我的个人资料课上:

public sealed class ViewModelMapperProfile : Profile
{
    ...

    protected override void Configure()
    {
        ForSourceType<DateTime>().AddFormatter<DateStringFormatter>();

        CreateMap<dto, viewModel>()
            .ForMember(dto => dto.DateSomething, opt => opt.MapFrom(src => src.DateFormatted));
公共密封类ViewModelMapperProfile:Profile
{
...
受保护的覆盖无效配置()
{
ForSourceType().AddFormatter();
CreateMap()
.ForMember(dto=>dto.DateSomething,opt=>opt.MapFrom(src=>src.DateFormatted));

}

我遇到了相同的问题,并找到了修复方法。请尝试更改:

ForSourceType<DateTime>().AddFormatter<StandardDateFormatter>();
ForSourceType().AddFormatter();

Mapper.ForSourceType().AddFormatter();

我遇到了相同的问题,并找到了修复方法。请尝试更改:

ForSourceType<DateTime>().AddFormatter<StandardDateFormatter>();
ForSourceType().AddFormatter();

Mapper.ForSourceType().AddFormatter();

仅供参考-AddFormatter方法在3.0版本中已过时。您可以改用ConvertUsing:

Mapper.CreateMap<DateTime, string>()
    .ConvertUsing<DateTimeCustomConverter>();

public class DateTimeCustomConverter : ITypeConverter<DateTime, string>
{
    public string Convert(ResolutionContext context)
    {
        if (context.SourceValue == null)
            return null;
        if (!(context.SourceValue is DateTime))
            return context.SourceValue.ToNullSafeString();
        return ((DateTime)context.SourceValue).ToShortDateString();
    }
}
Mapper.CreateMap()
.ConvertUsing();
公共类DateTimeCustomConverter:ITypeConverter
{
公共字符串转换(ResolutionContext上下文)
{
if(context.SourceValue==null)
返回null;
如果(!(context.SourceValue是DateTime))
返回context.SourceValue.ToNullSafeString();
return((DateTime)context.SourceValue).ToSortDateString();
}
}

仅供参考-AddFormatter方法在3.0版本中已过时。您可以改用ConvertUsing:

Mapper.CreateMap<DateTime, string>()
    .ConvertUsing<DateTimeCustomConverter>();

public class DateTimeCustomConverter : ITypeConverter<DateTime, string>
{
    public string Convert(ResolutionContext context)
    {
        if (context.SourceValue == null)
            return null;
        if (!(context.SourceValue is DateTime))
            return context.SourceValue.ToNullSafeString();
        return ((DateTime)context.SourceValue).ToShortDateString();
    }
}
Mapper.CreateMap()
.ConvertUsing();
公共类DateTimeCustomConverter:ITypeConverter
{
公共字符串转换(ResolutionContext上下文)
{
if(context.SourceValue==null)
返回null;
如果(!(context.SourceValue是DateTime))
返回context.SourceValue.ToNullSafeString();
return((DateTime)context.SourceValue).ToSortDateString();
}
}

感谢您的帮助。我尝试了这些示例,但仍然没有成功。我将继续为每个字段添加格式化程序。感谢您的帮助。我尝试了这些示例,但仍然没有成功。我将继续为每个字段添加格式化程序。