Skip to content

Commit 62b0f70

Browse files
committed
Merge branch 'release/2.0.0'
2 parents 725f93b + 3e24fb5 commit 62b0f70

19 files changed

+412
-230
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55

66
---
77

8+
## [2.0.0](https://github.com/Digipolitan/dependency-injector-swift/releases/tag/v2.0.0)
9+
10+
Update all API
11+
Better syntax to register provider
12+
13+
---
14+
815
## [1.1.1](https://github.com/Digipolitan/dependency-injector-swift/releases/tag/v1.1.1)
916

1017
remove unused methods

DGDependencyInjector.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "DGDependencyInjector"
3-
s.version = "1.1.1"
3+
s.version = "2.0.0"
44
s.summary = "Dependency injector made in pure swift"
55
s.homepage = "https://github.com/Digipolitan/dependency-injector-swift"
66
s.license = { :type => "BSD", :file => "LICENSE" }

DGDependencyInjector.xcodeproj/project.pbxproj

Lines changed: 50 additions & 20 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ DGDependencyInjector
77
[![Platform](https://img.shields.io/cocoapods/p/DGDependencyInjector.svg?style=flat)](http://cocoadocs.org/docsets/DGDependencyInjector)
88
[![Twitter](https://img.shields.io/badge/twitter-@Digipolitan-blue.svg?style=flat)](http://twitter.com/Digipolitan)
99

10-
Dependency injector made in pure Swift. Compatible for swift server-side and swift for iOS
10+
Dependency injector Swift. Compatible for swift server-side and swift for iOS
1111

1212
## Installation
1313

@@ -25,13 +25,11 @@ pod 'DGDependencyInjector'
2525

2626
## The Basics
2727

28-
First you must create a DependencyModule and register some providers
28+
First you must create a Module and register some providers
2929

3030
```swift
31-
let module = DependencyModule()
32-
module.register(type: IAnimal.self) { _ in
33-
return Dog(name: "Athina")
34-
}
31+
let module = Module()
32+
module.bind(IAnimal.self).to(Dog.self)
3533
```
3634

3735
IAnimal is a protocol that MUST be implemented by the Dog class
@@ -44,10 +42,14 @@ public protocol IAnimal {
4442
func scream() -> String
4543
}
4644

47-
open class Dog: IAnimal {
45+
open class Dog: IAnimal, Injectable {
4846

4947
public var name: String
5048

49+
public required convenience init(injector: Injector, arguments: [String : Any]?) throws {
50+
self.init(name: arguments?["name"] as? String ?? "Athina")
51+
}
52+
5153
init(name: String) {
5254
self.name = name
5355
}
@@ -61,13 +63,13 @@ open class Dog: IAnimal {
6163
After that, you must register your module inside an injector
6264

6365
```swift
64-
DependencyInjector.shared.register(module: module)
66+
Injector.default.register(module: module)
6567
```
6668

6769
Finally, inject an IAnimal and retrieve a concrete class registered inside your module
6870

6971
```swift
70-
if let animal = DependencyInjector.shared.inject(type: IAnimal.self) {
72+
if let animal = try? Injector.default.inject(IAnimal.self) {
7173
print(animal.name) // print Athina
7274
print(animal.scream()) // print Barking
7375
}
@@ -80,37 +82,37 @@ if let animal = DependencyInjector.shared.inject(type: IAnimal.self) {
8082
Register a provider that handle arguments :
8183

8284
```swift
83-
let module = DependencyModule()
84-
module.register(type: IAnimal.self) { (_, arguments) -> IAnimal? in
85+
let module = Module()
86+
module.bind(IAnimal.self).with { (_, arguments) -> IAnimal? in
8587
if let name = arguments?["name"] as? String {
8688
return Dog(name: name)
8789
}
8890
return nil
8991
}
90-
DependencyInjector.shared.register(module: module)
92+
Injector.default.register(module: module)
9193
```
9294

9395
Inject an IAnimal with arguments Dictionary<String, Any> :
9496

9597
```swift
96-
if let animal = DependencyInjector.shared.inject(type: IAnimal.self, arguments: ["name": "Athina"]) {
98+
if let animal = Injector.default.inject(IAnimal.self, arguments: ["name": "Athina"]) {
9799
print(animal.name) // print Athina
98100
print(animal.scream()) // print Barking
99101
}
100-
if let otherAnimal = DependencyInjector.shared.inject(type: IAnimal.self, arguments: ["name": "Yoda"]) {
102+
if let otherAnimal = Injector.default.inject(IAnimal.self, arguments: ["name": "Yoda"]) {
101103
print(otherAnimal.name) // print Yoda
102104
print(otherAnimal.scream()) // print Barking
103105
}
104106
```
105107

106-
##Contributing
108+
## Contributing
107109

108110
See [CONTRIBUTING.md](CONTRIBUTING.md) for more details!
109111

110112
This project adheres to the [Contributor Covenant Code of Conduct](CODE_OF_CONDUCT.md).
111113
By participating, you are expected to uphold this code. Please report
112114
unacceptable behavior to [contact@digipolitan.com](mailto:contact@digipolitan.com).
113115

114-
##License
116+
## License
115117

116118
DGDependencyInjector is licensed under the [BSD 3-Clause license](LICENSE).

Sources/Binder.swift

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* This class bind the real implementation behind an interface
3+
* @author Benoit BRIATTE http://www.digipolitan.com
4+
* @copyright 2017 Digipolitan. All rights reserved.
5+
*/
6+
public final class Binder<T> {
7+
8+
private var _singleton: Bool
9+
private var _provider: Provider<T>?
10+
private var _type: Injectable.Type?
11+
private var _handler: Provider<T>.ProviderHandler?
12+
13+
public init() {
14+
self._singleton = false
15+
}
16+
17+
@discardableResult
18+
public func to(_ type: Injectable.Type) -> Self {
19+
self._type = type
20+
return self
21+
}
22+
23+
@discardableResult
24+
public func to(_ object: T) -> Self {
25+
return self.to(SingletonProvider(object: object))
26+
}
27+
28+
@discardableResult
29+
public func to(_ provider: Provider<T>) -> Self {
30+
self._provider = provider
31+
return self
32+
}
33+
34+
@discardableResult
35+
public func with(_ handler: @escaping Provider<T>.ProviderHandler) -> Self {
36+
self._handler = handler
37+
return self
38+
}
39+
40+
@discardableResult
41+
public func singleton() -> Self {
42+
self._singleton = true
43+
return self
44+
}
45+
46+
func provider() -> Provider<T>? {
47+
if self._provider == nil {
48+
if self._singleton {
49+
if let type = self._type {
50+
self._provider = SingletonProvider(type: type)
51+
} else if let handler = self._handler {
52+
self._provider = SingletonProvider(handler: handler)
53+
}
54+
} else {
55+
if let type = self._type {
56+
self._provider = Provider(type: type)
57+
} else if let handler = self._handler {
58+
self._provider = Provider(handler: handler)
59+
}
60+
}
61+
}
62+
return self._provider
63+
}
64+
}

Sources/DependencyModule.swift

Lines changed: 0 additions & 94 deletions
This file was deleted.

Sources/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.1.1</string>
18+
<string>2.0.0</string>
1919
<key>CFBundleVersion</key>
2020
<string>1</string>
2121
<key>NSPrincipalClass</key>

Sources/Injectable.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public protocol Injectable {
2+
3+
init(injector: Injector, arguments: [String: Any]?) throws
4+
}

0 commit comments

Comments
 (0)