118

GitHub - kawoou/Deli: Deli is an easy-to-use Dependency Injection Container.

 6 years ago
source link: https://github.com/kawoou/Deli
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

README.md

Deli

Swift 68747470733a2f2f696d672e736869656c64732e696f2f62616467652f43617274686167652d636f6d70617469626c652d3442433531442e7376673f7374796c653d666c6174 Version License CI Status Platform

Deli is an easy-to-use Dependency Injection Container that creates DI containers with all required registrations and corresponding factories.

Table of Contents

Overview

Wanna spaghetti? or not. As your project grows, will experience a complex. We can write the wrong code by mistake.

In Spring framework provides automatic registration using some code rules and throws the wrong Dependency Graph before running. I wanted these features to be in Swift.

Getting Started

Simple setup for the automated configuration files, deli.yml.

If the configuration file does not exist, find the build target for a unique project in the current folders automatically. It works the same even if no scheme or output field is specified.

project: MyProject
scheme: MyScheme
output: Sources/DeliFactory.swift

You’ll have to make your targets Shared. To do this Manage Schemes and check the Shared areas:

shared-build-scheme

Then build with the provided binaries.

$ deli build

Dependency Graph is configured through source code analysis. It is saved as the file you specified earlier.

File contents as below:

class DeliFactory {
    ...
    init() {
        ...
    }
}

Add the generated file to the project and call it from the app's launch point.

drag-and-drop

AppDelegate.swift:

class AppDelegate {
    
    var window: UIWindow?

    let factory = DeliFactory()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        return true
    }
}

Build Phases

Integrate Deli into an Xcode scheme to get warnings and errors displayed in the IDE. Just add a new "Run Script Phase" with:

if which deli >/dev/null; then
  deli build
else
  echo "error: Deli not installed, download from https://github.com/kawoou/Deli"
fi

Build Phase

Alternatively, if you've installed Deli via CocoaPods the script should look like this:

"${PODS_ROOT}/DeliBinary/deli" build

Features

1. Component

The class, struct, and protocol can extend the Component protocol and will be registered automatically in the DI container.

Component can be used as below:

protocol UserService {
    func login(id: String, password: String) -> User?
    func logout()
}

class UserServiceImpl: UserService, Component {
    func login(id: String, password: String) -> User? {
        ...
    }
    func logout() {
        ...
    }

    init() {}
}

If the above code is written, you can use the UserService or UserServiceImpl type to load the dependency instance.

2. Autowired

The Autowired protocol is registered automatically, same as Component protocol. A difference, you can load the required dependencies from DI container.

Autowired can be used as below:

class LoginViewModel: Autowired {
    let userService: UserService

    required init(_ userService: UserService) {
        self.userService = userService 
    }
}

Easy right? So let's look at the code below.

protocol Book {
    var name: String { get }
    var author: String { get }
    var category: String { get }
}
struct Novel: Book {
    var qualifier: String {
        return "Novel"
    }

    var name: String {
        return ""
    }
    var author: String {
        return ""
    }
    var category: String {
        return "Novel"
    }
}
struct HarryPotter: Novel, Component {
    var name: String {
        return "Harry Potter"
    }
    var author: String {
        return "J. K. Rowling"
    }
}
struct TroisiemeHumanite: Novel, Component {
    var name: String {
        return "Troisième humanité"
    }
    var author: String {
        return "Bernard Werber"
    }
}

This code arranged the books through inheritance. You can get all of Book instances like below:

class LibraryService: Autowired {
    let books: [Book]

    required init(_ books: [Book]) {
        self.books = books
    }
}

3. LazyAutowired

If we can remove whole Circular Dependency cases, the world will be better than before, but it cannot be ruled completely. A simple way to solve this problem is to initialize one of the dependency lazily.

Let's try LazyAutowired protocol:

class UserService: Autowired {
    let messageService: MessageService

    required init(_ messageService: MessageService) {
        self.messageService = messageService
    }
}
class FriendService: Autowired {
    let userService: UserService

    required init(_ userService: UserService) {
        self.userService = userService
    }
}
class MessageService: Autowired {
    let friendService: FriendService

    required init(_ friendService: FriendService) {
        self.friendService = friendService
    }
}

If you try to inject a MessageService, Circular Dependency will occurred.

$ deli validate

Error: The circular dependency exists. (MessageService -> FriendService -> UserService -> MessageService)

What if UserService extends LazyAutowired?

class UserService: LazyAutowired {
    let messageService: MessageService!

    func inject(_ messageService: MessageService) {
        self.messageService = messageService
    }

    required init() {}
}

The cycle was broken and the issue was resolved! After MessageService instance successfully created, dependencies can be injected via inject() that UserService needed.

4. Configuration

The Configuration protocol makes the user can register Resolver directly.

Let's look at the code:

class UserConfiguration: Configuration {
    let networkManager = Config(NetworkManager.self, ConfigurationManager.self) { configurationManager in
        let privateKey = "1234QwEr!@#$"
        return configurationManager.make(privateKey: privateKey)
    }

    init() {}
}

You can see privateKey is passed to ConfigurationManager on NetworkManager creation.

This NetworkManager instance is registered in DI container, and it will be managed as singleton. (However, instance behavior can be changed by updating scope argument.)

5. Inject

As written, Autowired is registered in DI container. But you may want to use without registration. That's an Inject.

class LoginView: Inject {
    let viewModel = Inject(LoginViewModel.self)

    init() {}
}

class NovelBookView: Inject {
    let novels: [Book]!

    init() {
        self.novels = Inject([Book].self, qualifier: "Novel")
    }
}

6. Testable

Deli provides methods for testing in AppContext.

It is setTestMode():

public func setTestMode(_ active: Bool, qualifierPrefix: String)

Suppose that a test mode is activating using the method above. Then using the qualifier prefix when gets instances from DI containers(if it exists).

If you register a Mock object for testing in the DI Container, it will be gets first.

/// Register
AppContext.shared.register(
    AccountService.self,
    resolver: {
        let networkManager = AppContext.shared.get(NetworkManager.self, qualifier: "")!
        let libraryService = AppContext.shared.get(LibraryService.self, qualifier: "")!

        return MockAccountService(networkManager, libraryService)
    },
    qualifier: "test",
    scope: .singleton
)

/// Test Mode
AppContext.shared.setTestMode(true, qualifierPrefix: "test")

/// Inject
let accountService = Inject(AccountService.self)

if accountService is MockAccountService {
  print("Test Mode")
} else {
  print("Normal Mode")
}

/// Result
> Test Mode

An example of a test code is Deli.xcodeproj.

Installation

Cocoapods:

Simply add the following line to your Podfile:

pod 'Deli'

Carthage:

github "kawoou/Deli"

Command Line

$ deli help
Available commands:

   build      Build the Dependency Graph.
   generate   Generate the Dependency Graph.
   help       Display general or command-specific help
   upgrade    Upgrade outdated.
   validate   Validate the Dependency Graph.
   version    Display the current version of Deli

Contributing

Any discussions and pull requests are welcomed.

If you want to contribute, submit a pull request.

Requirements

  • Swift 3.1+

Attributions

This project is powered by

License

Deli is under MIT license. See the LICENSE file for more info.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK