Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchhung committed Oct 13, 2023
1 parent 64a1a90 commit 795957d
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
# dpi
Simple dependency injection
Simple dependency injection base on context.Context from golang


// use ProvideWithContext to provice dependencies to context
main.go

```
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// cleanup
// Provider
ctx = dpi.ProvideWithContext(
ctx,
// eg consumer: DB *database.DBConn `inject:"true"`
database.New("no name"),
// with custom name
// eg consumer: DB *database.DBConn `inject:"true" name:"myAnotherDB"`
dpi.WithName("myAnotherDB", database.New("with name")),
)
// Provider | Consumer
// for circular dependency injection, need to use inject:"true,lazy"
ctx = dpi.ProvideWithContext(ctx,
// eg inject B to A, ServiceB *ServiceB `inject:"true,lazy"`
services.NewServiceA(ctx),
// eg inject A to B, ServiceA *ServiceA `inject:"true,lazy"`
services.NewServiceB(ctx),
)
// consumer
api := NewAPI(ctx)
// wait for lazy injection
dpi.FromContext(ctx).Wait()
api.Print()
}
```

Services Provider | Consumer
Expand Down

0 comments on commit 795957d

Please sign in to comment.