An open API service indexing awesome lists of open source software.

https://github.com/peterprokop/bogracz

Bogracz is dependency injection manager with some desirable properties like thread safety and support for type-safe arguments
https://github.com/peterprokop/bogracz

Last synced: about 2 months ago
JSON representation

Bogracz is dependency injection manager with some desirable properties like thread safety and support for type-safe arguments

Awesome Lists containing this project

README

        

Bogracz
======

# Intro

Bogracz is really small and nifty dependency injection (DI) framework.

Compared to Swinject it has some nice properties like thread safety
and type-safe arguments.

# Installation

## Carthage
- `touch Cartfile`
- `nano Cartfile`
- Put `github "peterprokop/Bogracz" == 1.0.0` into Cartfile
- Save it: `ctrl-x`, `y`, `enter`
- Run `carthage update`
- Add `Bogracz.framework` to your carthage [copy-frameworks phase](https://github.com/Carthage/Carthage#quick-start)
- Add `import Bogracz` in files where you plan to use it

# Usage
Do `import Bogracz`
Then create a container: `let container = DependencyContainer()`

Then you can add dependencies as static (dependency will be created once and you are responsible for it):

```swift
let myService: MyServiceProviding = MyService()
container.add(MyServiceProviding.self, instance: myService)
```

... dynamic (dependency will be created every time you get it):
```swift
container.add(MyServiceProviding.self, block: { _ in
return MyService()
})
```

... or dynamic with config:
```swift
let config = MyServiceConfig(answer: 42)

container.add(MyServiceProviding.self, block: { (r, config: MyServiceConfig) in
return MyService(config: config)
})
```

Then you can get your dependency in following way:

For dependencies without config -
```swift
let myService = container.get(MyServiceProviding.self)
```

For configurable dependencies -
```swift
let myService = container.get(MyServiceProviding.self, config: config)
```

# Dependency Injection
Some useful articles about this topic:
- https://martinfowler.com/articles/injection.html
- https://www.martinfowler.com/articles/dipInTheWild.html
- https://www.martinfowler.com/bliki/InversionOfControl.html