-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathBaseAggregateRoot.java
More file actions
69 lines (59 loc) · 1.81 KB
/
BaseAggregateRoot.java
File metadata and controls
69 lines (59 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package io.github.dddplus.model;
import io.github.dddplus.model.spcification.ISpecification;
import io.github.dddplus.model.spcification.Notification;
import lombok.Builder;
/**
* BaseAggregateRoot is a handy reusable {@link IAggregateRoot}.
*
* @param <Entity>
*/
public abstract class BaseAggregateRoot<Entity> implements IAggregateRoot {
@Builder.Default
protected DirtyMemento memento = new DirtyMemento();
@Builder.Default
protected Exchange exchange = new Exchange();
protected final void dirty(IDirtyHint hint) {
memento.register(hint);
}
protected final void mergeDirtyWith(IMergeAwareDirtyHint hint) {
memento.merge(hint);
}
public <T extends IDirtyHint> T firstHintOf(Class<T> hintClass) {
return memento.firstHintOf(hintClass);
}
/**
* 业务归约要满足.
*
* @param specification 业务归约
*/
public void assureSatisfied(ISpecification<Entity> specification) {
Notification notification = Notification.build();
if (!specification.isSatisfiedBy((Entity) this, notification)) {
whenNotSatisfied(notification);
}
}
/**
* 业务规约没有满足时如何处理.
*
* @param notification 未满足原因
*/
protected abstract void whenNotSatisfied(Notification notification);
/**
* 临时交换区赋值.
*/
public void xSet(String key, Object value) {
exchange.set(key, value);
}
/**
* 从临时交换区里取值.
*/
public <T> T xGet(String key, Class<T> valueType) throws ClassCastException {
return exchange.get(key, valueType);
}
/**
* 临时交换区方便的布尔类型取值.
*/
public boolean xIs(String key) throws ClassCastException {
return exchange.is(key);
}
}