You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would call it a very elaborated proof of concept, though we use it in production atm.
6
-
We are open for every kind of feedback, improvements of the generated code/templates, discussion of the input format etc.
7
-
8
5
## TLDR;
9
6
10
-
We propose a method to get rid of duplicate use of class values and namestrings, by generating explicit functions for registering and resolving.
7
+
Swinject-CodeGen provides a method to get rid of duplicate use of class values and namestrings, by generating explicit functions for registering and resolving using Swinject.
11
8
Doing this, we also can generate typed tuples to use when resolving, thus allowing better documented and less error-prone code.
12
9
13
10
## Installation
14
-
15
-
We aim to support carthage in the near future
16
-
17
11
### Cocoapods
18
12
19
13
Add
@@ -24,24 +18,38 @@ pod 'Swinject-CodeGen', :git => 'https://github.com/Swinject/Swinject-CodeGener
24
18
25
19
to your podfile.
26
20
21
+
### Carthage
22
+
23
+
Add
24
+
25
+
```
26
+
github "Swinject/Swinject-CodeGeneration"
27
+
```
28
+
29
+
to your Cartfile.
30
+
27
31
## Integration
28
32
1. Define your dependencies in a .csv or .yml file (see below and example file)
29
33
2. Add a call to generate the code as build script phase:
3. Add the generated file (here: `extensions/baseContainerExtension.swift`) to xcode
36
46
4. Repeat if you need to support multiple targets/have multiple input files.
37
47
38
48
The code is then generated at every build run.
39
49
40
50
## The Issue
41
51
42
-
We love using swinject for dependency injection.
43
-
However, we would like to stay to the DRY principle and really love types.
44
-
When using swinject, lots of duplicate definitions appear, whenever we do a
52
+
When using Swinject, lots of duplicate definitions appear, whenever we do a
45
53
46
54
```Swift
47
55
container.register(PersonType.self, name: "initializer") { r in
@@ -99,7 +107,15 @@ We decided to use `;` as delimiter instead of `,` to allow the use of tuples as
99
107
#### Additional Commands
100
108
The ruby parser allows using `//` and `#` for comments.
101
109
Empty lines are ignored and can be used for grouping.
102
-
`#= <header>` can be used to specify additional headers, e.g. `#ADD_HEADER import KeychainAccess`
110
+
111
+
`#= <header>` can be used to specify additional lines, e.g. `#= import KeychainAccess`
112
+
113
+
#### Dictionaries and Arrays as Parameters
114
+
When using typed dictionaries or arrays as parameters, use `Array<Type>` instead of `[Type]` and `Dictionary<TypeA, TypeB>` instead of `[TypeA:TypeB]`:
The resulting extension files are added to xcode and given appropriate target settings.
372
-
373
375
## Migration
374
376
The script also generates migration.sh files (when using the -m switch), which use sed to go through the code and replace simple cases (i.e. no arguments) of resolve and register.
375
377
No automatic migration is available for cases with arguments, yet.
376
378
Simply call the .sh file from the root of the project and compare the results in a git-GUI.
377
379
378
380
## Results
379
-
We currently use the code generation in a medium-sized app, with a total of ~130 lines of definitions across 3 .csv files (shared definitions, iOS only, tvOS only).
380
-
We found our code to become much more convenient to read and write, due to reduced duplication and autocompletion. We also have a much better overview the classes available through dependency injection.
381
+
We currently use the code generation in two medium-sized apps across tvOS and iOS.
382
+
383
+
We found our code to become much more convenient to read and write, due to reduced duplication and autocompletion.
384
+
We also have a much better overview the classes available through dependency injection.
381
385
Changing some definition immediately leads to information, where an error will occur.
382
386
We were able to replace all our occurences of `.resolve(` and `.register(` using the current implementation.
383
387
384
-
385
388
## Contributors
386
-
The main contributors for this are[Daniel Dengler](https://github.com/ddengler), [David Kraus](https://github.com/davidkraus) and [Wolfgang Lutz](https://github.com/lutzifer).
389
+
The original idea for combining CodeGeneration and Swinject came from[Daniel Dengler](https://github.com/ddengler), [David Kraus](https://github.com/davidkraus) and [Wolfgang Lutz](https://github.com/lutzifer).
0 commit comments