@@ -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
3735IAnimal 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 {
6163After 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
6769Finally, 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) {
8082Register 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
9395Inject 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
108110See [ CONTRIBUTING.md] ( CONTRIBUTING.md ) for more details!
109111
110112This project adheres to the [ Contributor Covenant Code of Conduct] ( CODE_OF_CONDUCT.md ) .
111113By participating, you are expected to uphold this code. Please report
112114unacceptable behavior to [ contact@digipolitan.com ] ( mailto:contact@digipolitan.com ) .
113115
114- ##License
116+ ## License
115117
116118DGDependencyInjector is licensed under the [ BSD 3-Clause license] ( LICENSE ) .
0 commit comments