20

GitHub - BrickmakersGmbH/swift-ioc-container: Swift-IoC-Container is a tool with...

 4 years ago
source link: https://github.com/BrickmakersGmbH/swift-ioc-container
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.

README.md

swift-ioc-container

Actions Status Actions Status

import swift_ioc_container

protocol SuperAwesomeBot {
    func saySomething()
}

class StarWarsBot: SuperAwesomeBot {
    func saySomething() {
        print("The Force will be with you. Always.")
    }
}

And use it like this:

// only needed once
IoC.shared.registerLazySingleton(SuperAwesomeBot.self, { StarWarsBot() }) 


let myBot: SuperAwesomeBot = try! IoC.shared.resolve()
myBot.saySomething()

If you are using Swift 5.1 or newer, you could take advantage of the new property wrapper feature. It's already implemented in this library.

There is no more need to initialize the property manually in the initializer. Just declare the property with the expected protocol type.

@Injected private var myBot: SuperAwesomeBot

After declaring the property as @Injected you can access the previously registered object as you are used to.

An example implementation can be found in Example.swift

Requirements

There are no further requirements beside Swift 5.1, just copy this little lib in your project and start.

Installation

swift_ioc_container is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Swift-IOC-Container'

Authors

Michael Scherbakow, [email protected]
Jonas Österle, [email protected]
Pascal Friedrich, [email protected]
Philipp Manstein, [email protected]

License

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK