AutoMapper SourceMemberNameTransformer

发布于 2024-12-01 05:03:18 字数 383 浏览 0 评论 0原文

我想为我在许多域对象和关联视图模型上拥有的属性注册自定义命名约定。

域对象有一个枚举属性 PublishStatus,视图模型有一个布尔属性 Published

如果我创建一个自定义 SourceMemberNameTransformer 就像 如何让 AutoMapper 处理自定义命名约定?,我是否只返回与 PublishStatus 不匹配的属性的原始字符串?

I wanted to register a custom naming convention for a property that I have on a number of domain objects and associated view models.

The domain object has an enum property PublishStatus and the view model has a boolean property Published.

If I create a custom SourceMemberNameTransformer like on How do I get AutoMapper to deal with a custom naming convention?, do I just return the original string for properties that don't match PublishStatus?

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

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

发布评论

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

评论(1

离线来电— 2024-12-08 05:03:18

如果我正确理解你的问题,你可能需要一个 ValueResolver。像这样:

public class PublishStatusResolver : ValueResolver<PublishStatus, bool>
{
    protected override bool ResolveCore(PublishStatus status)
    {
        return status == PublishStatus.Published;
    }
}

然后在映射代码中你可以这样称呼它:

.ForMember(dto => dto.Published, 
           opt => opt.ResolveUsing<PublishStatusResolver>()
                     .FromMember(src => src.PublishStatus))

If I understand your question properly you probably want a ValueResolver. Something like:

public class PublishStatusResolver : ValueResolver<PublishStatus, bool>
{
    protected override bool ResolveCore(PublishStatus status)
    {
        return status == PublishStatus.Published;
    }
}

Then in mapping code you call it like:

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