-
Notifications
You must be signed in to change notification settings - Fork 36
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
1 parent
a5eb2ee
commit e22801a
Showing
1 changed file
with
30 additions
and
13 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,40 +1,57 @@ | ||
# Container | ||
A IoC Container written in Go | ||
An IoC Container written in Go | ||
|
||
## Documentation | ||
|
||
### Installation | ||
To install this package run following command in the root of your project | ||
|
||
```bash | ||
go get github.com/golobby/ioc | ||
go get github.com/golobby/container | ||
``` | ||
|
||
### Binding | ||
To bind an abstraction to a concrete for further singletion resolution: | ||
To bind an abstraction to a concrete for further singleton resolutions: | ||
|
||
```go | ||
i := ioc.Container{} | ||
i.Singleton(func() Repository { | ||
return &UserRepository{} | ||
container.Singleton(func() Abstraction { | ||
return Implementation | ||
}) | ||
``` | ||
And to bind an abstraction to a concrete for further transient resolution: | ||
|
||
And to bind an abstraction to a concrete for further transient resolutions: | ||
|
||
```go | ||
container.Transient(func() Abstraction { | ||
return Implementation | ||
}) | ||
``` | ||
|
||
For example: | ||
|
||
```go | ||
i := ioc.Container{} | ||
i.Transient(func() Repository { | ||
return &UserRepository{} | ||
import "github.com/golobby/container" | ||
|
||
container.Singleton(func() Mailer { | ||
return &Gmail{} | ||
}) | ||
``` | ||
|
||
### Resolving | ||
|
||
To make (resolve) an abstraction: | ||
To make (resolve) a concrete by its abstraction: | ||
|
||
```go | ||
container.Make(func(a Abstraction) { | ||
// a will be an concrete of Abstraction | ||
}) | ||
``` | ||
|
||
For example: | ||
|
||
```go | ||
i.Make(func(r Repository) { | ||
// r will be an instance of UserRepository | ||
container.Make(func(m Mailer) { | ||
m.Send("[email protected]", "Hello!") | ||
}) | ||
``` | ||
|
||
|