在我的DDD项目中,我试图用java枚举实现状态模式。ddd状态模式验证通知
我在验证具有取决于状态的行为的实体方法时遇到问题。
验证我使用通知模式。
我遵循“始终有效的实体”的方法,以便在每次操作中我首先调用“isValidForOperation”验证方法。
下面的代码,只需将相关的简单:
实体:
public class Task extends AggregateRoot<TaskId> {
...
private State state;
...
// Operation with behaviour depending on the state
// It's a transition from "ASSIGNED" state to "IN_PROGRESS" state
// I apply the state pattern here
public void start() {
State next = this.state.start (this);
this.setState (next);
}
...
}
的java的枚举建模状态:
public enum State {
ASSIGNED {
public State start (Task task) {
// Validation method to ensure the operation can be done
assertTaskIsValidForStart (task);
// Business logic
...
// Return the next state
return (State.IN_PROGRESS);
}
}
...
// more enum values for other states
...
// Default implementation of "start" operation
// It will be executed when the current state is not "ASSIGNED"
// So an error would be generated
public State start (Task task) {
// I can't apply notification pattern here !!!
// I would have to throw an exception
}
}
的验证方法如下通知模式。 它收集通知对象中的所有可能的错误。 此通知对象传递给异常。 引发异常,然后应用程序层捕获它并将所有错误消息返回给客户端。
public void assertTaskIsValidForStart (Task task) {
Notification notification = new Notification();
if (errorCondition (task)) {
notification.addError(...);
}
...
// more errors
...
if (notification.hasErrors()) {
throw new TaskNotValidForStartException (notification.errors());
}
}
时出现的错误是关于状态之间的转换无效怎么能应用(在连词与状态模式)的通知模式?
任何想法?
UPDATE:
我找到了解决办法。我把整个操作取决于实体中的状态,并且将状态模式更细化,仅仅用于所需的代码。通过这种方式,我应用该模式来计算下一个状态,以便我可以检查是否允许转换并应用通知模式。
代码:
public class Task extends AggregateRoot<TaskId> {
...
private State state;
...
// Operation with behaviour depending on the state
// It's a transition from "ASSIGNED" state to "IN_PROGRESS" state
// I apply fine-grained state pattern here
public void start() {
// Validation method to ensure the operation can be done
// One of the validations will be if the transition is allowed
assertTaskIsValidForStart (this);
// Business logic
// If it depends on the state, I would apply state pattern delegating to another method
...
// Set the next state
State next = this.nextStateForStart();
this.setState (next);
}
...
public State currentState() {
return this.state;
}
...
public State nextStateForStart() {
return this.currentState().nextStateForStart();
}
...
}
public enum State {
ASSIGNED {
public State nextStateForstart() {
return (State.IN_PROGRESS);
}
}
...
// more enum values for other states
...
// Default implementation of "start" transition
// It will be executed when the current state is not "ASSIGNED"
public State nextStateForstart() {
return null;
}
}
public void assertTaskIsValidForStart (Task task) {
Notification notification = new Notification();
// Validate the transition is allowed
if (task.nextStateForStart() == null) {
notification.addError(...);
}
...
// more errors
...
if (notification.hasErrors()) {
throw new TaskNotValidForStartException (notification.errors());
}
}
Hello @mbnx。你在哪里返回下一个状态?执行“execute”方法不能,因为它返回通知。 – choquero70
可以使用StateContext接口将状态设置为传递给execute方法。 – mbnx