The proposals so far all seem to be way overkill, especially with all the IOC and AOP stuff.
The User
class needs an email address, so create an EmailAddress
class and have the User
class accept one via a property and/or its constructor. That validation can be as simple as whether the input EmailAddress
reference is null or not.
The EmailAddress
class can be a simple but generally reusable implementation (consider basing it on the RFC document). It should be immutable and should throw an exception from its constructor on invalid input.
Ideally, the EmailAddress
class should be composed of an EmailUserId
class (based on the RFC?) and a InternetDomain
class (based on the RFC?), since an email address is a composite data structure. Again, each of those classes should manage immutable instances and should throw an exception on construction with invalid input.
"Validation" strikes me as not a "thing" but rather a generic "action". Therefore, it lends itself to being a method rather than a class. In this case, I tend to implement the validation in each of these classes as a private static method (valid(input)
) that is invoked from the constructor, in languages like Java or C#. Often, it becomes useful to expose that functionality publicly in the form of a question (isValid(input)
).
EDIT:
Are you suggesting that every distinct data type I need to validate should have it's own class?
That is one solid way of addressing the issue, commonly known as a Value Type (thanks for the reminder, Frank). The result will be a few (dozen or two) well-defined, reusable classes like perhaps EmailAddress
, PhoneNumber
, PersonName
, etc. The presented alternative is likely to result in a "god class" with a mixture of functionality that is not reusable, not easy to test, and difficult to maintain.
There are other ways to partition the solution, but my suggestion does have the advantage of being mature, well understood, and consistent with a large set of solid design principles. I would certainly recommend trying it before attempting to invent your own.