.net 类型转换器和域使用

发布于 2024-11-24 13:29:57 字数 543 浏览 4 评论 0原文

一般来说,有什么理由不使用 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)

enter image description here

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

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

发布评论

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

评论(1

肥爪爪 2024-12-01 13:29:57

这是一个很好的链接,它解释了 TypeConverter< /code> 并实现 IConvertible。但是,基本上,TypeConverter 是为在设计时进行类型转换而构建的,并且最有用。例如,这就是 XAML 将类型与 XML 相互转换的方式。

IConvertible 通常比 TypeConverter 更灵活、更快(它不使用反射)并且更容易实现。除非您需要 TypeConverter 提供的额外功能(例如标准值列表),否则我建议您使用接口选项并使用 Convert 静态方法。

Here's a good link that explains the difference between a TypeConverter and implementing IConvertible. 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 a TypeConverter. Unless you need the extra features that TypeConverter gives you (like the list of standard values), I would recommend you go with the interface option and use the Convert static methods.

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