我应该在域模型中使用 System.Net.Mail.MailAddress 还是仅使用字符串?
正如这个问题中详细介绍的,System.Uri
是反映我对 URI 意图的不错选择。但是电子邮件地址呢?
它似乎不那么明确,因为 MailAddress
在其 DisplayName
属性中有额外的信息,不太适合。
As covered well in this question, System.Uri
is a good choice to reflect my intention for URIs. But what about for email addresses?
It seems not as clear-cut, because MailAddress
has extra information in its DisplayName
property that is not a great fit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不想或不需要,则不必使用
DisplayName
。MailAddress
类型通过在其构造函数中抛出异常来确保电子邮件地址格式正确。我认为,出于这个原因,使用MailAddress
而不是String
是值得的,而且还因为它传达了意图并且增加了很少的开销。You don't have to use the
DisplayName
if you don't want or need to. TheMailAddress
type ensures that an email address is well formed by throwing an exception in it's constructor otherwise. I would argue that it is worth usingMailAddress
overString
for this reason and also because it communicates intent and adds little overhead.System.Uri 有很多有用的功能,但 System.Net.Mail.MailAddress 没有,因此是否使用它并不是一个关键的决定。 FWIW,我已经以两种方式实现了它(但在不同的地方!)——字符串和MailAddress。
System.Uri has a lot of useful functionality, but System.Net.Mail.MailAddress doesn't, so whether to use it is not a critical decision. FWIW, I have implemented it both ways (but in different places!) -- string and MailAddress.