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

Commit b4cb225

Browse files
committed
New post 🚀
1 parent b0011e6 commit b4cb225

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

_images/posts/dp-container.jpg

43.8 KB
Loading

_drafts/2020-03-19-dependecy-injection-swift.md renamed to _posts/2020-03-19-dependecy-injection-swift.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
22
layout: post
3-
title: "How to: create your SUPER simple dependency injector framework in Swift"
4-
description: "There are a lot of dependency injection framework in the open source swift world with really cool features like object graph, persistence etc. But what if all you need is a lightweight dependencies container? In this post I will show you how to create it by leveraging the Metatype Type, the Hashable protocol and the Equatable protocol."
3+
title: "How to: create your SUPER simple dependency injector container in Swift"
4+
description: "There are a lot of dependency injection framework in the open source swift world with really cool features like object graph, persistence etc. But what if all you need is a lightweight dependencies container? In this post I will show you how to create it by leveraging the Metatype Type, Generics, the Hashable protocol and the Equatable protocol."
55
date: 2020-03-19
6-
image: /assets/images/posts/XXXXXXXXXXXXX
6+
image: /assets/images/posts/dp-container.jpg
77
tags: [swift, ios, apple, mobile application development]
88
comments: true
99
math: false
@@ -12,7 +12,7 @@ seo:
1212
authors: [fabrizio_duroni]
1313
---
1414

15-
*There are a lot of dependency injection framework in the open source swift world with really cool features like object graph, persistence etc. But what if all you need is a lightweight dependencies container? In this post I will show you how to create it by leveraging the Metatype Type and the Hashable protocol and the Equatable protocol.*
15+
*There are a lot of dependency injection framework in the open source swift world with really cool features like object graph, persistence etc. But what if all you need is a lightweight dependencies container? In this post I will show you how to create it by leveraging the Metatype Type, Generics, the Hashable protocol and the Equatable protocol.*
1616

1717
---
1818

@@ -27,7 +27,7 @@ The main one stores in the field `dependecies` a dictionary of all the dependenc
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

30-
This is the implementation of `DependeciesContainer`.
30+
As you can see both method extensively uses generics in order to be able to accept any possible class or protocol you may want to use. This is the implementation of `DependeciesContainer`.
3131

3232
```swift
3333
class DependeciesContainer {
@@ -89,7 +89,7 @@ class Cat: Animal {
8989
func isAlive() -> Bool {
9090
true
9191
}
92-
92+
9393
func miaow() -> String {
9494
return "miaow"
9595
}
@@ -99,7 +99,7 @@ class Dog: Animal {
9999
func isAlive() -> Bool {
100100
true
101101
}
102-
102+
103103
func bark() -> String {
104104
return "wooof"
105105
}
@@ -109,7 +109,7 @@ class Tiger: Animal {
109109
func isAlive() -> Bool {
110110
true
111111
}
112-
112+
113113
func roar() -> String {
114114
return "roar"
115115
}
@@ -124,7 +124,7 @@ class SickPerson: Person {
124124
func breath() -> String {
125125
return "fiuu"
126126
}
127-
127+
128128
func cough() -> String {
129129
return "cough"
130130
}
@@ -149,13 +149,13 @@ print((person as! SickPerson).cough()) // "cough\n"
149149
dc.register(type: Animal.self, name: "Cat", service: Cat())
150150
dc.register(type: Animal.self, name: "Tiger", service: Tiger())
151151
let cat = dc.resolve(type: Animal.self, name: "Cat")!
152-
print(String(describing: cat))
152+
print(String(describing: cat)) // "__lldb_expr_9.Cat\n"
153153
print(cat.isAlive())
154-
print((cat as! Cat).miaow())
154+
print((cat as! Cat).miaow()) //"miaow\n"
155155
let tiger = dc.resolve(type: Animal.self, name: "Tiger")!
156-
print(String(describing: tiger))
156+
print(String(describing: tiger)) // "__lldb_expr_9.Tiger\n"
157157
print(tiger.isAlive())
158-
print((tiger as! Tiger).roar())
158+
print((tiger as! Tiger).roar()) // "roar\n"
159159
```
160160

161161
#### Conclusion

_scripts/build.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env zsh
2+
3+
# Build website
4+
bundle exec jekyll build
43.8 KB
Loading

gulpfile.babel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ gulp.task('service-worker-css-error-urls', (done) => {
278278
serviceWorkerUrlFor('css-error', done)
279279
});
280280

281-
gulp.task('jekyll-build', (done) => exec(`bundle exec jekyll build`, (err, stdout, stderr) => done()))
281+
gulp.task('jekyll-build', (done) => exec(`./_scripts/build.sh`, (err, stdout, stderr) => done()))
282282

283283
const build = gulp.series(
284284
'css-home',

0 commit comments

Comments
 (0)