.net 类型转换器和域使用
一般来说,有什么理由不使用 TypeConverter 来进行涉及与 UI 无关的自定义类型的转换工作呢?
我正在考虑比隐含和隐含的案件更复杂的案件。显式转换。
任何设计指南和链接的链接这样做的样本?
干杯,
Berryl
UPDATE
这是转换的动机,一种 Party - PartyRelationship 模式,其中一个 Party 可以有一个或多个 PartyName。当参与方是人时,所需名称是 PersonName。
PersonName 具有与 PartyName 不同的属性,并且是 ValueObject。它用于 ui / 格式化类型任务,而 PartyName 是一个实体并保存到数据库中。
所以需要进行转换: - 从数据库加载以进行演示:(PartyName --> PersonName) - 将新联系人添加到数据库:(PersonName --> PartyName)
In general, is there any reason not to use a TypeConverter for conversion chores involving custom types that have nothing to do with UI?
Im thinking about cases more complex than implict & explicit conversions.
Any links to design guidelines & samples for doing so?
Cheers,
Berryl
UPDATE
Here is the motivation for the conversion, a Party - PartyRelationship pattern where a Party can have One or many PartyNames. When the Party is a Person, the One required name is a PersonName.
The PersonName has attributes that are different from a PartyName and is a ValueObject. It is used for ui / formatting type tasks whereas the PartyName is an Entity and is persisted to the database.
So conversions are needed:
- Load from DB for presentation: (PartyName --> PersonName)
- Add new contact to DB: (PersonName --> PartyName)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个很好的链接,它解释了
TypeConverter< /code> 并实现
IConvertible
。但是,基本上,TypeConverter
是为在设计时进行类型转换而构建的,并且最有用。例如,这就是 XAML 将类型与 XML 相互转换的方式。IConvertible
通常比TypeConverter
更灵活、更快(它不使用反射)并且更容易实现。除非您需要 TypeConverter 提供的额外功能(例如标准值列表),否则我建议您使用接口选项并使用 Convert 静态方法。Here's a good link that explains the difference between a
TypeConverter
and implementingIConvertible
. But, basically,TypeConverter
is built for, and most useful for, doing type conversions at design time. For example, it's how XAML converts types to XML and back.IConvertible
is generally more flexible, faster (it doesn't use reflection), and easier to implement than aTypeConverter
. Unless you need the extra features thatTypeConverter
gives you (like the list of standard values), I would recommend you go with the interface option and use theConvert
static methods.