自动映射器格式化程序不工作

发布于 2024-08-09 07:13:50 字数 1732 浏览 3 评论 0原文

我正在尝试将格式化程序添加到我的 Automapper 配置中,以设置所有 DateTime? 字段的样式。我尝试在全局添加我的格式化程序:

Mapper.AddFormatter<DateStringFormatter>();

以及在特定映射本身上:

Mapper.CreateMap<Post, PostViewModel>()
            .ForMember(dto => dto.Published, opt => opt.AddFormatter<DateStringFormatter>());

但这似乎都不起作用 - 它总是以正常格式输出日期。作为参考,这里是我正在使用的 ViewModel 以及其余配置:

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;
        }
    }

我做错了什么?

谢谢!

I'm trying to add a formatter to my Automapper configuration to style all DateTime? fields. I've tried adding my formatter globally:

Mapper.AddFormatter<DateStringFormatter>();

And on the specific mapping itself:

Mapper.CreateMap<Post, PostViewModel>()
            .ForMember(dto => dto.Published, opt => opt.AddFormatter<DateStringFormatter>());

But neither seems to work - it always outputs the date in the normal format. For reference, here is the ViewModel I'm using, and the rest of the configuration:

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;
        }
    }

What am I doing wrong?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅冕 2024-08-16 07:13:50

仅当目标成员类型为“string”类型时才应用格式化程序。由于“Published”的类型为“DateTime?”,因此永远不会应用格式化程序。这里有几个选项:

  • 将 Published 属性添加到 Post 对象,其行为如上所述
  • 为 Published 属性创建自定义解析器,该解析器首先解析 DateTime?属性逻辑中的值,然后在已发布时将目标成员类型更改为字符串。首先,解析器将执行。接下来,格式化程序获取自定义解析器的结果,最后将结果值设置为 Published
  • 执行所有自定义类型 ->视图中的字符串格式,使用 HtmlHelper 之类的东西

我们通常选择 1),除非显示的值确实仅适用于该视图,那么我们选择选项 2)。

Formatters are only applied when the destination member type is of type "string". Since "Published" is of type "DateTime?", the formatter never gets applied. You have a few options here:

  • Add a Published property to the Post object, with the behavior laid out above
  • Create a custom resolver for the Published property, which first resolves the DateTime? value from the property logic, then change the destination member type to string on Published. First, the resolver will execute. Next, the formatter takes the result of the custom resolver, and finally, the resulting value is set on Published
  • Do all of your custom Type -> String formatting in the View, with things like an HtmlHelper

We usually go for 1), unless the value displayed is truly only for this view, then we go for option 2).

神妖 2024-08-16 07:13:50

尝试这样做:

Mapper.CreateMap<DateTime?, string>().ConvertUsing(d => d.Value.ToString("d"));

您可以更改功能以满足您的要求。

Try doing it this way:

Mapper.CreateMap<DateTime?, string>().ConvertUsing(d => d.Value.ToString("d"));

You can change the function to meet your requirements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文