Skip to content

Commit d316546

Browse files
authored
docs: Add a basic example for query (#12)
1 parent 919b8a7 commit d316546

File tree

1 file changed

+67
-10
lines changed

1 file changed

+67
-10
lines changed

README.md

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ Dew streamlines Go application development by providing a unified interface for
1515
- [Motivation](#motivation)
1616
- [Terminology](#terminology)
1717
- [Convention for Actions and Queries](#convention-for-actions-and-queries)
18+
- [Example for `Action`:](#example-for-action)
19+
- [Example for `Query`:](#example-for-query)
1820
- [Installation](#installation)
1921
- [Example](#example)
22+
- [Hello Action Example](#hello-action-example)
23+
- [Hello Query Example](#hello-query-example)
2024
- [Usage](#usage)
2125
- [Setting Up the Bus](#setting-up-the-bus)
2226
- [Dispatching Actions](#dispatching-actions)
@@ -55,14 +59,14 @@ graph LR
5559
Middleware
5660
end
5761
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
6670
6771
style Dew fill:#e6f3ff,stroke:#333,stroke-width:2px,color:#000
6872
style Bus fill:#b3e0ff,stroke:#333,stroke-width:2px,color:#000
@@ -95,7 +99,7 @@ Dew follows these conventions for `Action` and `Query` interfaces:
9599
- **Action Interface**: Each action must implement a `Validate` method to ensure the action's data is valid before processing.
96100
- **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.
97101

98-
Example:
102+
### Example for `Action`:
99103

100104
```go
101105
// MyAction represents an Action
@@ -111,6 +115,12 @@ func (a *MyAction) Validate(ctx context.Context) error {
111115
return nil
112116
}
113117

118+
```
119+
120+
### Example for `Query`:
121+
122+
```go
123+
114124
// MyQuery represents a Query
115125
type MyQuery struct {
116126
AccountID string
@@ -129,7 +139,7 @@ go get github.com/go-dew/dew
129139

130140
See [examples](examples) for more detailed examples.
131141

132-
Basic usage:
142+
### Hello Action Example
133143

134144
```go
135145
package main
@@ -179,6 +189,53 @@ func (h *HelloHandler) HandleHelloAction(ctx context.Context, cmd *HelloAction)
179189
}
180190
```
181191

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+
182239
## Usage
183240

184241
### Setting Up the Bus

0 commit comments

Comments
 (0)