@@ -15,8 +15,12 @@ Dew streamlines Go application development by providing a unified interface for
15
15
- [ Motivation] ( #motivation )
16
16
- [ Terminology] ( #terminology )
17
17
- [ Convention for Actions and Queries] ( #convention-for-actions-and-queries )
18
+ - [ Example for ` Action ` :] ( #example-for-action )
19
+ - [ Example for ` Query ` :] ( #example-for-query )
18
20
- [ Installation] ( #installation )
19
21
- [ Example] ( #example )
22
+ - [ Hello Action Example] ( #hello-action-example )
23
+ - [ Hello Query Example] ( #hello-query-example )
20
24
- [ Usage] ( #usage )
21
25
- [ Setting Up the Bus] ( #setting-up-the-bus )
22
26
- [ Dispatching Actions] ( #dispatching-actions )
@@ -55,14 +59,14 @@ graph LR
55
59
Middleware
56
60
end
57
61
58
- Client -->|1. Dispatch Action| Bus
59
- Client -->|2. Execute Query| Bus
60
- Bus -->|3. Apply| Middleware
61
- Middleware -->|4a. Route Action| ActionHandler
62
- Middleware -->|4b. Route Query| QueryHandler
63
- ActionHandler -->|5a. Handle & Modify State| DB[(Database)]
64
- QueryHandler -->|5b. Fetch Data| DB
65
- ActionHandler & QueryHandler -->|6. Return Result| Client
62
+ Client -->|1. Dispatch Action| Bus
63
+ Client -->|2. Execute Query| Bus
64
+ Bus -->|3. Apply| Middleware
65
+ Middleware -->|4a. Route Action| ActionHandler
66
+ Middleware -->|4b. Route Query| QueryHandler
67
+ ActionHandler -->|5a. Handle & Modify State| DB[(Database)]
68
+ QueryHandler -->|5b. Fetch Data| DB
69
+ ActionHandler & QueryHandler -->|6. Return Result| Client
66
70
67
71
style Dew fill:#e6f3ff,stroke:#333,stroke-width:2px,color:#000
68
72
style Bus fill:#b3e0ff,stroke:#333,stroke-width:2px,color:#000
@@ -95,7 +99,7 @@ Dew follows these conventions for `Action` and `Query` interfaces:
95
99
- ** Action Interface** : Each action must implement a ` Validate ` method to ensure the action's data is valid before processing.
96
100
- ** Query Interface** : Each query implements the ` Query ` interface, which is an empty interface. Queries don't require a ` Validate ` method as they don't modify application state.
97
101
98
- Example:
102
+ ### Example for ` Action ` :
99
103
100
104
``` go
101
105
// MyAction represents an Action
@@ -111,6 +115,12 @@ func (a *MyAction) Validate(ctx context.Context) error {
111
115
return nil
112
116
}
113
117
118
+ ```
119
+
120
+ ### Example for ` Query ` :
121
+
122
+ ``` go
123
+
114
124
// MyQuery represents a Query
115
125
type MyQuery struct {
116
126
AccountID string
@@ -129,7 +139,7 @@ go get github.com/go-dew/dew
129
139
130
140
See [ examples] ( examples ) for more detailed examples.
131
141
132
- Basic usage:
142
+ ### Hello Action Example
133
143
134
144
``` go
135
145
package main
@@ -179,6 +189,53 @@ func (h *HelloHandler) HandleHelloAction(ctx context.Context, cmd *HelloAction)
179
189
}
180
190
```
181
191
192
+ ### Hello Query Example
193
+
194
+ ``` go
195
+ package main
196
+
197
+ import (
198
+ " context"
199
+ " fmt"
200
+ " github.com/go-dew/dew"
201
+ )
202
+
203
+ // HelloQuery is a simple query that returns a greeting message.
204
+ type HelloQuery struct {
205
+ // Name is the name of the user.
206
+ Name string
207
+
208
+ // Result is the output of the query.
209
+ // You can define any struct as the result.
210
+ Result string
211
+ }
212
+
213
+ func main () {
214
+ // Initialize the Command Bus.
215
+ bus := dew.New ()
216
+
217
+ // Register the handler for the HelloAction.
218
+ bus.Register (new (HelloHandler))
219
+
220
+ // Create a context with the bus.
221
+ ctx := dew.NewContext (context.Background (), bus)
222
+
223
+ // Execute the query.
224
+ query , err := dew.Query (ctx, &HelloQuery{Name: " Dew" })
225
+ if err != nil {
226
+ fmt.Println (" Error:" , err)
227
+ } else {
228
+ fmt.Printf (" Result: %+v \n " , query.Result )
229
+ }
230
+ }
231
+
232
+ type HelloHandler struct {}
233
+ func (h *HelloHandler ) HandleHelloQuery (ctx context .Context , cmd *HelloQuery ) error {
234
+ cmd.Result = fmt.Sprintf (" Hello, %s !" , cmd.Name )
235
+ return nil
236
+ }
237
+ ```
238
+
182
239
## Usage
183
240
184
241
### Setting Up the Bus
0 commit comments