我有一个Email对象有两个属性,标签和值。系统用户需要验证他们的电子邮件,然后才能在系统中使用它。验证过程非常简单:域对象属性和封装
- 设置了电子邮件
- 激活码发送一封电子邮件,激活码验证电子邮件是有效的
电子邮件对象如下所示:
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)
}
一切工作正常,除了activationCode属性不电子邮件对象中正确感觉。它并没有描述对象的状态,它仅用于电子邮件验证过程。所以我修改了我的代码来引入一个名为ActivationToken的新对象。该对象将用于封装activationCode。这里的电子邮件对象
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
}
- 的新版本是这样一个声音域设计还是我复杂的我对象没有
- 是否requestVerification方法属于Email对象首先还是应放置在其他地方;在服务中,或在一个过程中。
- 有没有办法,我可以按照解决类似的问题
更新
我想解释一下,为什么我连第二重构后保留电子邮件域对象的requestVerfication方法部分的任何设计模式做法。
我有一个直接与域对象通过调度员以下列方式进行交互的远程接口:
remote://email/6/do/requestVerification
本来我想继续通过域对象渠道后端所有的通信和是否有需要与我使用IOC将其注入域对象并使用域对象作为代理的服务进行交互。远程接口和域对象之间的分离看起来很干净,但是结果却是一个非常糟糕的想法,因为它扼杀了域设计,并将无用的依赖性引入了与外部对象无关(在这种情况下为EmailVerificationService),这与该行为无关或域的状态方面对象
另一种解决方案来解决这个可能是保持在其中其自然地所属的业务的requestVerification方法和引入新的语法的通信协议如:
remote://service/email/do/requestVerification
你们有什么感想 ?
谢谢
-Ken
请参阅我的问题的更新,解释为什么我没有选择服务方法的原因。您的意见非常感谢。谢谢 – ken 2010-09-03 14:34:09