I have an Email object with two properties, label and value. System user need to verify their email before they can use it in the system. The verification process is very simple:
- Set an activation code for the email
- Send an email with the activation code to verify that the email is valid
The email object looks like the following:
class Email {
String label
String value
EmailStatus status
String activationCode
boolean requestVerification() {
// Set the activationCode that will be refereced to change the email status
this.activationCode = UUID
// Raise an event to send a notification email by a communication service
EventManager.fire('emailVerificationRequest',this)
}
All works fine, except that the activationCode property doesn't feel right within the Email object. It doesn't describe in anyway the state of the object and it's only used in the email validation process. So I modified my code to introduce a new object called ActivationToken. The object will be used to encapsulate the activationCode. Here's the new version of the email object
class Email {
String label
String value
EmailStatus status
boolean requestVerification() {
new ActivationToken(target:this,code:UUID,expiryDate:new Date()).save()
// Raise an event to send a notification email by a communication service
EventManager.fire('emailVerificationRequest',this)
}
class ActivationToken {
Email target
String code
Date expiryDate
}
- Is this a sound Domain Design or am I complicating my object for nothing
- Does the requestVerification method belong to the Email object in the first place or should it be placed elsewhere; in a service, or within a process.
- Is there any design pattern that I can follow to tackle similar problems
Update
I wanted to explain why i kept the requestVerfication method part of the Email domain object even after the second refactoring approach.
I have a remote interface that interacts directly with domain objects through a dispatcher in the following fashion:
remote://email/6/do/requestVerification
Originally i wanted to keep all the communication with the backend channeled through the domain objects and if there is a need to interact, say, with a service i was using IOC to inject it into the domain object and use the domain object as a proxy. The separation between the remote interface and the domain object looked clean but that turned out to be a very bad idea as it beaks the domain design and introduces useless dependency to external object (in this case EmailVerificationService ) that has nothing to do with the behavior or the state aspects of the domain object
The other solution to solve this could be to keep the requestVerification method in a service where it naturally belongs and introduce a new syntax to the communication protocol such as:
remote://service/email/do/requestVerification
What do you guys think ?
Thank you
-ken