-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfactory.go
160 lines (144 loc) · 3.66 KB
/
factory.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package ddd
import (
"fmt"
"github.com/antlinker/ddd/path"
"github.com/pkg/errors"
)
// DomainManager 领域管理者
// 通过领域管理者可以查询所有的领域对象
type DomainManager interface {
GetDomain(domainid string) Domain
RegDomain(d Domain)
FindNode(path string) DomainNode
FindDomain(path string) Domain
FindAggregateRoot(path string) AggregateRoot
FindAggregate(path string) Aggregate
FindDomainService(path string) DomainService
FindEntity(path string) Entity
FindRepository(path string) Repository
}
type domainManage struct {
domains map[string]Domain
}
func (m *domainManage) RegDomain(d Domain) {
_, ok := m.domains[d.DomainID()]
if ok {
panic(fmt.Sprintf("已经注册了一个同名的领域 :%v", d.DomainID()))
}
m.domains[d.DomainID()] = d
}
func (m *domainManage) GetDomain(id string) Domain {
return m.domains[id]
}
func (m *domainManage) FindNode(pstr string) (DomainNode, error) {
p := path.FromString(pstr)
if p.IsInvalid() {
return nil, errors.Errorf("错误的域路径%v", pstr)
}
i := p.Next()
if i.Kind() != path.Domain {
return nil, errors.Errorf("领域路径必须从领域开始")
}
d := m.GetDomain(i.CurName())
if d == nil {
return nil, ErrorNoFoundPath
}
var cur DomainNode = d
for i = p.Next(); i != nil; i = p.Next() {
switch i.Kind() {
case path.Domain:
switch cp := cur.(type) {
case Domain:
cur = cp.SubDomain(i.CurName())
default:
return nil, ErrorNoFoundPath
}
case path.AggregateRoot:
switch cp := cur.(type) {
case Domain:
cur = cp.AggregateRootByID(i.CurName())
default:
return nil, ErrorNoFoundPath
}
case path.Aggregate:
switch cp := cur.(type) {
case AggregateRoot:
cp.GetAggregate(i.CurName())
default:
return nil, ErrorNoFoundPath
}
case path.Service:
switch cp := cur.(type) {
case Domain:
cur = cp.ServiceByID(i.CurName())
default:
return nil, ErrorNoFoundPath
}
case path.Repository:
switch cp := cur.(type) {
case Domain:
cur = cp.RepositoryByID(i.CurName())
case AggregateRoot:
cur = cp.Repository()
default:
return nil, ErrorNoFoundPath
}
case path.Entity:
switch cp := cur.(type) {
case Domain:
cur = cp.EntityByID(i.CurName())
default:
return nil, ErrorNoFoundPath
}
}
}
return cur, nil
}
func (m *domainManage) FindDomain(pstr string) (Domain, error) {
if n, err := m.FindNode(pstr); err != nil {
return nil, err
} else if d, ok := n.(Domain); ok {
return d, nil
}
return nil, ErrorNodeKindNotMatch
}
func (m *domainManage) FindAggregateRoot(pstr string) (AggregateRoot, error) {
if n, err := m.FindNode(pstr); err != nil {
return nil, err
} else if d, ok := n.(AggregateRoot); ok {
return d, nil
}
return nil, ErrorNodeKindNotMatch
}
func (m *domainManage) FindAggregate(pstr string) (Aggregate, error) {
if n, err := m.FindNode(pstr); err != nil {
return nil, err
} else if d, ok := n.(Aggregate); ok {
return d, nil
}
return nil, ErrorNodeKindNotMatch
}
func (m *domainManage) FindDomainService(pstr string) (DomainService, error) {
if n, err := m.FindNode(pstr); err != nil {
return nil, err
} else if d, ok := n.(DomainService); ok {
return d, nil
}
return nil, ErrorNodeKindNotMatch
}
func (m *domainManage) FindEntity(pstr string) (Entity, error) {
if n, err := m.FindNode(pstr); err != nil {
return nil, err
} else if d, ok := n.(Entity); ok {
return d, nil
}
return nil, ErrorNodeKindNotMatch
}
func (m *domainManage) FindRepository(pstr string) (Repository, error) {
if n, err := m.FindNode(pstr); err != nil {
return nil, err
} else if d, ok := n.(Repository); ok {
return d, nil
}
return nil, ErrorNodeKindNotMatch
}