本文介绍了为什么AutoMapper具有IValueFormatter当它具有一个看似强大得多ValueResolver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它看起来像一个 IValueFormatter 需要类型的值对象键,返回类型为<$ C $的值C>字符串,而 ValueResolver< TSource,TDestination> 接受任何类型的值,并返回任何类型的值。因此,它是更加灵活。另外还有一点,以 ValueResolver ,你永远不需要源转换为特定类型的事 - 你在你的类定义中明确定义它

It looks like an IValueFormatter takes a value of type object and returns a value of type string, while a ValueResolver<TSource, TDestination> takes a value of any type and returns a value of any type. So, it's more flexible. There is also the matter that, with a ValueResolver, you never need to cast the source to a particular type--you define it explicitly in your class definition.

鉴于此,为什么要用 IValueFormatter ?它能做什么,不能用 ValueResolver 做些什么呢?我误解它是如何工作的?

Given this, why use IValueFormatter? Does it do anything that can't be done with ValueResolver? Am I misunderstanding how it works?

推荐答案

最大的区别是,格式化可以在成员,轮廓,类型和全局应用水平。所以,你可以在配置文件中做类似ForSourceType.AddFormatter(),现在blammo!现在你的所有小数金钱显示出来。解析器是严格的自定义成员映射。

The big difference is that formatters can be applied at the member, profile, type and global level. So you can do something like "ForSourceType.AddFormatter() in a profile, and now blammo! All your decimals now show up as money. Resolvers are strictly for custom member mapping.

这篇关于为什么AutoMapper具有IValueFormatter当它具有一个看似强大得多ValueResolver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 13:57