• Stars
    star
    303
  • Rank 137,655 (Top 3 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A statically typed dependency injector for Swift.

DIKit

Build Status

A statically typed dependency injector for Swift.

Overview

DIKit provides interfaces to express dependency graph. A code generator named dikitgen finds implementations of the interfaces, and generate codes which satisfies dependency graph.

The main parts of DIKit are injectable types and provider methods, and both of them are to declare dependencies of types.

Injectable types are types that conform to Injectable protocol.

public protocol Injectable {
    associatedtype Dependency
    init(dependency: Dependency)
}

A conformer of Injectable protocol must have associated type Dependency as a struct. You declare dependencies of the Injectable conformer as stored properties of Dependency type. For example, suppose we have ProfileViewController class, and its dependencies are User, APIClient and Database. Following example code illustrates how to declare dependencies by conforming Injectable protocol.

final class ProfileViewController: Injectable {
    struct Dependency {
        let user: User
        let apiClient: APIClient
        let database: Database
    }

    init(dependency: Dependency) {...}
}

Provider methods are methods of inheritor of Resolver protocol, which is a marker protocol for code generation.

public protocol Resolver {}

Provider methods declares that which non-injectable types can be instantiated automatically. In the example above, APIClient and Database are non-injectable type, but they can be provided in the same ways in most cases. In this situation, define provider methods for the types in an inheritor of Resolver protocol, so that instances of the types are provided automatically.

protocol AppResolver: Resolver {
    func provideAPIClient() -> APIClient
    func provideDatabase() -> Database
}

In short, we have following situation so far:

  • Dependencies of ProfileViewController are User, APIClient and Database.
  • Instances of APIClient and Database are provided automatically.
  • An instance of User must be provided manually to instantiate ProfileViewController.

dikitgen generates following code for the declarations:

extension AppResolver {
    func resolveAPIClient() -> APIClient {
        return provideAPIClient()
    }

    func resolveDatabase() -> Database {
        return provideDatabase()
    }

    func resolveViewController(user: User) -> ProfileViewController {
        let apiClient = resolveAPIClient()
        let database = resolveDatabase()
        return ProfileViewController(dependency: .init(user: User, apiClient: apiClient, database: Database))
    }
}

To use generated code, you have to implement a concrete type of AppResolver.

final class AppResolverImpl: AppResolver {
    let apiClient: APIClient = ...
    let database: Database = ...

    func provideAPIClient() {
        return apiClient
    }

    func provideDatabase() {
        return database
    }
}

Since AppResolver is a protocol, all implementations of provider methods are checked at compile time. If you would like to create mock version of AppResolver for unit testing, define another concrete type of AppResolver. It can be used the same as AppResolverImpl.

Now, you can instantiate ProfileViewController like below:

let appResolver = AppResolverImpl()
let user: User = ...
let viewController = appResolver.resolveViewController(user: user)

Requirements

  • Code generator: Swift 4.1+ / Xcode 9.4+
  • Runtime library: macOS 10.11+ / iOS 9.0+ / watchOS 2.0+ / tvOS 9.0+

Installation

Install code generator dikitgen first.

Mint

mint install ishkawa/DIKit dikitgen

From Source

git clone https://github.com/ishkawa/DIKit.git
cd DIKit
make install

Then, integrate DIKit.framework to your project. There are some option to install DIKit.framework.

  • Manual: Clone this repository and add DIKit.xcodeproj to your project.
  • Carthage: Add a line github "ishkawa/DIKIt" to your Cartfile and run carthage update.

Optionally, insert shell script running dikitgen to early part of build phases.

if which dikitgen >/dev/null; then
  dikitgen ${SRCROOT}/YOUR_PROJECT > ${SRCROOT}/YOUR_PROJECT/AppResolver.generated.swift
else
  echo "warning: dikitgen not installed, download from https://github.com/ishkawa/DIKit"
fi

More Repositories

1

APIKit

Type-safe networking abstraction layer that associates request type with response type.
Swift
1,994
star
2

DataSourceKit

Declarative, testable data source of UICollectionView and UITableView.
Swift
722
star
3

ISRefreshControl

(deprecated) iOS4-compatible UIRefreshControl
Objective-C
228
star
4

ISColumnsController

paginated container view controller.
Objective-C
108
star
5

ISDiskCache

LRU disk cache for iOS.
Objective-C
86
star
6

sandbox

Objective-C
60
star
7

UINavigationController-Transition

extension for custom transition by blocks.
Objective-C
58
star
8

iosdc-2018-demo

Swift
47
star
9

ISBackGesture

equips UIViewController with recognizing swipe to back.
Objective-C
38
star
10

NSRunLoop-PerformBlock

extension of NSRunLoop for waiting.
Objective-C
31
star
11

UIWebView-Progress

integrates NJKWebViewProgress into UIWebView.
Objective-C
28
star
12

storyboard-2018-demo

Swift
27
star
13

ISRemoveNull

extension of NSArray and NSDictionary to remove NSNull from them.
Objective-C
25
star
14

ISInteractiveEdgesNavigationBar

subclass of UINavigationBar which can handle touch events on both edges.
Objective-C
22
star
15

ISInvocationHookProxy

A proxy object that hooks each NSInvocation of target.
Objective-C
20
star
16

ISMemoryCache

NSDictionary-based memory cache.
Objective-C
19
star
17

ios_mvvm_test_example

Swift
18
star
18

ISHTTPOperation

a subclass of NSOperation to wrap asynchronous NSURLConnection.
Objective-C
13
star
19

talks

Swift
13
star
20

wire_example

Go
11
star
21

ISNetwork

minimal NSURLConnection wrapper (NSOperation based)
Objective-C
11
star
22

ISAlternativeRefreshControl

a template for creating custom UIRefreshControl.
Objective-C
11
star
23

ISMethodSwizzling

functions to swizzle instance/class methods.
Objective-C
9
star
24

iOS5UIRefreshControlTotorial

http://atnd.org/events/39505
Objective-C
8
star
25

APIKit-AlamofireAdapter

Alamofire adapter for APIKit.Session (alpha).
Swift
8
star
26

slackstream

Go
7
star
27

ishkawa.github.com

HTML
5
star
28

flutter_add_to_app_demo

Dart
5
star
29

ISCyclicPagesView

Objective-C
5
star
30

ISRevealController

container controller which has slidable UINavigationController and menu UIViewController.
Objective-C
3
star
31

GHFSupport

3
star
32

custom_json_serializable_example

Dart
3
star
33

IRMCommandOperation

an operation to send command to iRemocon.
Objective-C
3
star
34

NSDictionary-URLQuery

Ruby
3
star
35

ISTableViewController

a viewcontroller for the combination of UITableView and NSMutableArray
Objective-C
2
star
36

GHP

Swift
2
star
37

KIFNextExample

an example project for KIF 2.0.0
Objective-C
2
star
38

ISGcovFlusher

Test observer class which calls __gcov_flush() on completion of XCTests.
Ruby
2
star
39

ISFakeInterfaceOrientation

Objective-C
2
star
40

ttw

an extremely minimal twitter client.
Objective-C
2
star
41

slackmute

Go
2
star
42

XCTestCase-ExpectationWithCondition

extension of XCTestCase to create XCTestExpectation which waits for the condition to be fulfilled.
Swift
2
star
43

SenAsyncTestCase

a subclass of SenTestCase which is compatible with asynchronous tests.
Objective-C
2
star
44

AutoIssueExample

Qiita hackathon
Objective-C
1
star
45

ISMessageAlertView

alert view with ISTextView (subclass of UITextView)
Objective-C
1
star
46

NSDate-Twitter

NSDate category for Twitter
1
star
47

UIColor-Code

UIColor by color code.
Objective-C
1
star
48

term_twitter

twitter client for terminal
Python
1
star
49

RunTestsForAllDestinationsExample

Objective-C
1
star
50

CoreDataExperiment

Objective-C
1
star
51

GIRIcon

Objective-C
1
star
52

ISDataManager

Objective-C
1
star
53

ReadHub

source code reader
Objective-C
1
star
54

Newton

ๅคงไบบใฎๅคไผ‘ใฟใฎ่‡ช็”ฑ็ ”็ฉถใ€‚
Objective-C
1
star
55

remind

command to send task to Reminder (iCloud).
Objective-C
1
star
56

PMDR

extremely minimal pomodoro timer
Objective-C
1
star
57

ISTwitterAuthorizeViewController

a view controller to get access token for twitter (using OAuthCore and ISNetwork)
Objective-C
1
star