Skip to content

Commit 257ef51

Browse files
committed
docs: Add a basic example for query
1 parent 919b8a7 commit 257ef51

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

README.md

Lines changed: 59 additions & 2 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)
@@ -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+
result, err := dew.Query(ctx, &HelloQuery{Name: "Dew"})
225+
if err != nil {
226+
fmt.Println("Error:", err)
227+
} else {
228+
fmt.Printf("Result: %+v\n", 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)