Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
miladrahimi committed Sep 30, 2019
1 parent 0c7210e commit 2014bcb
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
[![Build Status](https://travis-ci.org/golobby/container.svg?branch=master)](https://travis-ci.org/golobby/container)

# Container
An IoC Container written in Go
An IoC Container written in Go.
It provides simple, fluent and easy-to-use APIs to make dependency injection in GoLang very easier.

## Documentation

### Supported Versions
It requires Go `v1.11` or newer versions.

### Installation
To install this package run following command in the root of your project

Expand All @@ -21,6 +25,8 @@ container.Singleton(func() Abstraction {
})
```

It invokes the resolver function once and always return the same object each time you call `make()` method.

And to bind an abstraction to a concrete for further transient resolutions:

```go
Expand All @@ -29,43 +35,53 @@ container.Transient(func() Abstraction {
})
```

For example:
It invokes the resolver function to provide a brand new object each time you call `make()` method.

Take a look at examples below:

Singleton example:

```go
import "github.com/golobby/container"
container.Singleton(func() Database {
return &MySQL{}
})
```

container.Singleton(func() Mailer {
return &Gmail{}
Transient example:

```go
container.Transient(func() Shape {
return &Rectangle{}
})
```

### Resolving

To make (resolve) a concrete by its abstraction:
To make a concrete by its abstraction:

```go
container.Make(func(a Abstraction) {
// a will be concrete of Abstraction
// a will be a concrete of Abstraction
})
```

For example:

```go
container.Make(func(m Mailer) {
// m is an instance of Gmail
m.Send("[email protected]", "Hello!")
container.Make(func(db Database) {
// db is an instance of MySQL
db.Query("...")
})
```

You can resolve multiple abstractions:

```go
container.Make(func(m Mailer, s Shape) {
// m is an instance of Gmail
// s is an instance of Shape (like Rectangle)
m.Send("[email protected]", "Hello!");
println(s.Area())
container.Make(func(db Database, s Shape) {
// db is an instance of MySQL
// s is an instance of Rectangle
db.Query("...")
s.Area()
})
```

Expand Down

0 comments on commit 2014bcb

Please sign in to comment.