Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit 7b76f62

Browse files
committed
fix 🚀
1 parent a55dc7c commit 7b76f62

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

_posts/2020-03-19-dependecy-injection-swift.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ authors: [fabrizio_duroni]
1616

1717
---
1818

19-
The open source Swift world is full of useful framework. You can find almost everything you need (there are [rare cases where you need to write something that still doesn't exist](https://www.fabrizioduroni.it/2018/05/07/born-id3tageditor-mp3id3tagger.html id3 tag editor) out there). Anyway, a lot of the frameworks and libraries you will find out do more than you need. See for example the world of the dependecies injection framework. We have a lot of alternatives from which we can choose: [Swinject](https://github.com/Swinject/Swinject "dependecies injection swift Swinject"), [Weaver](https://github.com/scribd/Weaver "dependecies injection swift Weaver") etc. This frameworks come with a lot of features like: object graph construction, injection with property wrappers, instance persistence etc. This are all useful feature, but if your needs are very limited (just a dependecies container register/resolver using protocol and classes) the previous frameworks gives you just a big overhead and complexity on you code. This is why for my recent project I tried to write my own very simple dependencies injector container by leveraging the power of Swift Metatype and the Hashable protocol. Let's go and see the how I created it :smirk:.
19+
The open source Swift world is full of useful framework. You can find almost everything you need (there are [rare cases where you need to write something that still doesn't exist](https://www.fabrizioduroni.it/2018/05/07/born-id3tageditor-mp3id3tagger.html id3 tag editor) out there). Anyway, a lot of the frameworks and libraries you will find out do more than you need. See for example the world of the dependencies injection framework. We have a lot of alternatives from which we can choose: [Swinject](https://github.com/Swinject/Swinject "dependencies injection swift Swinject"), [Weaver](https://github.com/scribd/Weaver "dependecies injection swift Weaver") etc. This frameworks come with a lot of features like: object graph construction, injection with property wrappers, instance persistence etc. This are all useful feature, but if your needs are very limited (just a dependecies container register/resolver using protocol and classes) the previous frameworks gives you just a big overhead and complexity on you code. This is why for my recent project I tried to write my own very simple dependencies injector container by leveraging the power of Swift Metatype and the Hashable protocol. Let's go and see the how I created it :smirk:.
2020

2121
#### Implementation
2222

2323
The main class of dependencies injector container is composed by 2 classes: `DependeciesContainer` and `DependencyKey`.
2424
The main one stores in the field `dependecies` a dictionary of all the dependencies registered using an instance of the second one. `DependeciesContainer` exposes two methods:
2525

26-
* `register<T>(type: T.Type, name: String? = nil, service: Any)`, that lets you register a new dependency in the container. It accepts 3 parameter. The first one is the `Type` of the dependency. The second one is a string that let's you identify different named variations of the same dependencies (e.g. you register `Cat` and `Dog`, two different implementation of an hypothetical `Animal` protocol). The 3 one is the instance saved in the `dependecies` dictionary.
26+
* `register<T>(type: T.Type, name: String? = nil, service: Any)`, that lets you register a new dependency in the container. It accepts 3 parameter. The first one is the `Type` of the dependency. The second one is a string that let's you identify different named variations of the same dependencies (e.g. you register `Cat` and `Dog`, two different implementation of an hypothetical `Animal` protocol). The third one is the instance saved in the `dependecies` dictionary.
2727

2828
* `resolve<T>(type: T.Type, name: String? = nil) -> T?`, that lets you get an instance previously registered. This method accept the same first two parameter of the previous method. It will return null if none of the registered instance has a combination of `type` and `name` as the one received as parameters.
2929

0 commit comments

Comments
 (0)