.netapi-designdomain-modelmailaddress

Should I use System.Net.Mail.MailAddress in my domain model, or just strings?


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.


Solution

  • You don't have to use the DisplayName if you don't want or need to. The MailAddress 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 using MailAddress over String for this reason and also because it communicates intent and adds little overhead.