-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tung.tq
committed
Oct 18, 2023
1 parent
76ee943
commit ee257b2
Showing
1 changed file
with
65 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,29 @@ | ||
# Service Locator Library for Dependency Injection in Go | ||
|
||
#### (Not actually the Service Locator pattern) | ||
# A Library for Dependency Injection in Go | ||
|
||
[![svloc](https://github.com/QuangTung97/svloc/actions/workflows/go.yml/badge.svg)](https://github.com/QuangTung97/svloc/actions/workflows/go.yml) | ||
[![Coverage Status](https://coveralls.io/repos/github/QuangTung97/svloc/badge.svg?branch=master)](https://coveralls.io/github/QuangTung97/svloc?branch=master) | ||
|
||
## Why this library? | ||
|
||
* It is simpler than [uber/fx](https://github.com/uber-go/fx), yet still powerful | ||
* Static typing | ||
* Static typing & no use of reflection | ||
* Easy to use and easy to replace objects for unit testing / integration testing | ||
* Easy to read & navigate with IDEs | ||
* Safe to use in multiple goroutines | ||
|
||
#### This library is NOT actually using the Service Locator pattern | ||
|
||
### Limitations | ||
|
||
* Runtime wiring of objects | ||
* Deep stack calls | ||
* ``Override`` functions only works outside of ``new functions`` in ``Register`` | ||
|
||
## Installtion | ||
|
||
```bash | ||
go get github.com/QuangTung97/[email protected] | ||
``` | ||
|
||
## Examples | ||
|
||
|
@@ -42,6 +56,10 @@ Assume ``Service`` with method ``Hello``: | |
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type Service struct { | ||
rp Repo | ||
} | ||
|
@@ -79,7 +97,13 @@ var serviceLoc = svloc.Register[*Service](func(unv *svloc.Universe) *Service { | |
}) | ||
``` | ||
|
||
Add use in ``main()``, by first creates a new ``Universe``. | ||
The 3 newly created objects: ``usernameLoc``, ``repoLoc``, ``serviceLoc`` | ||
are all immutable objects and safe to use concurrently. | ||
|
||
The ``svloc.PreventRegistering`` will not allow ``Register`` functions to be called after that point. | ||
Usually at the start of the ``main()`` function. | ||
|
||
To use in ``main()``, first creates a new ``Universe``. | ||
Then call ``MustOverride()`` on ``usernameLoc`` to provide the username string. | ||
And then call the ``serviceLoc.Get()`` with that ``Universe``, | ||
All of the wiring will happen automatically: | ||
|
@@ -161,4 +185,39 @@ func main() { | |
svc := serviceLoc.Get(unv) | ||
svc.Hello() | ||
} | ||
``` | ||
``` | ||
|
||
Using ``OnShutdown`` and ``Shutdown``: | ||
|
||
```go | ||
package main | ||
|
||
var repoLoc = svloc.Register[Repo](func(unv *svloc.Universe) Repo { | ||
unv.OnShutdown(func() { | ||
fmt.Println("Shutdown Repo") | ||
}) | ||
|
||
return NewRepo( | ||
usernameLoc.Get(unv), | ||
) | ||
}) | ||
|
||
var serviceLoc = svloc.Register[*Service](func(unv *svloc.Universe) *Service { | ||
unv.OnShutdown(func() { | ||
fmt.Println("Shutdown Service") | ||
}) | ||
return NewService(repoLoc.Get(unv)) | ||
}) | ||
|
||
func main() { | ||
svloc.PreventRegistering() | ||
|
||
unv := svloc.NewUniverse() | ||
defer unv.Shutdown() | ||
|
||
usernameLoc.MustOverride(unv, "user01") | ||
|
||
svc := serviceLoc.Get(unv) | ||
svc.Hello() | ||
} | ||
``` |