Dip

CI Status Version Carthage Compatible License Platform Swift Version Swift Version

Animated Dipping GIF
图片来源:www.kevinandamanda.com

简介

Dip 是一个简单的 依赖注入容器

它的目标是尽可能简单,同时提供其他平台上 DI 容器常用的丰富功能。它的灵感来自 .NETUnity Container 和其他 DI 容器。

基本用法
import Dip

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    // Create the container
    private let container = DependencyContainer { container in
    
        // Register some factory. ServiceImp here implements protocol Service
        container.register { ServiceImp() as Service }
    }

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
        
        // Resolve a concrete instance. Container will instantiate new instance of ServiceImp
        let service = try! container.resolve() as Service
    
        ...
    }
}
更复杂的示例
import Dip

class AppDelegate: UIResponder, UIApplicationDelegate {
	private let container = DependencyContainer.configure()
	...
}

//CompositionRoot.swift
import Dip
import DipUI

extension DependencyContainer {

	static func configure() -> DependencyContainer {
		return DependencyContainer { container in 
			unowned let container = container
			DependencyContainer.uiContainers = [container]
		
			container.register(tag: "ViewController") { ViewController() }
			  .resolvingProperties { container, controller in
				  controller.animationsFactory = try container.resolve() as AnimatonsFactory
			}
    
			container.register { AuthFormBehaviourImp(apiClient: $0) as AuthFormBehaviour }
			container.register { container as AnimationsFactory }
			container.register { view in ShakeAnimationImp(view: view) as ShakeAnimation }
			container.register { APIClient(baseURL: NSURL(string: "https://:2368")!) as ApiClient }
		}
	}

}

extension DependencyContainer: AnimationsFactory { 
    func shakeAnimation(view: UIView) -> ShakeAnimation {
        return try! self.resolve(withArguments: view)
    }
}

extension ViewController: StoryboardInstantiatable {}

//ViewController.swift

class ViewController {
    var animationsFactory: AnimationsFactory?

    private let _formBehaviour = Injected<AuthFormBehaviour>()
    
    var formBehaviour: AuthFormBehaviour? {
        return _formBehaviour.value
    }
	...
}

文档 & 用法示例

Dip 有完整的 文档,并附带一个 Playground,可以让你尝试所有功能并熟悉 API。 你可以在 Dip.xcworkspace 中找到它。

注意:您可能需要先构建 Dip 框架,playground 才能使用它。为此,请选择 Dip scheme 并为 iPhone Simulator 构建。

你可以在 wiki 中找到大量用法示例和有用的技巧。

如果你正在使用 VIPER 架构 - 这里 是一个 VIPER 演示应用程序,它使用 Dip 代替手动依赖注入。

还有一些博客文章描述了如何使用 Dip 以及它的一些实现细节

如果您有任何问题,请提交 issue。 也热烈欢迎 pull request。

特性

安装

您可以使用您最喜欢的依赖管理器安装 Dip

CocoaPods

pod "Dip"

Carthage
github "AliSoftware/Dip"

要为 Swift 2.3 构建,请使用 --toolchain com.apple.dt.toolchain.Swift_2_3 选项运行 Carthage。

Swift Package Manager
.Package(url: "https://github.com/AliSoftware/Dip", majorVersion: 5, minor: 0)

运行测试

在 OSX 上,您可以从 Xcode 运行测试。 在 Linux 上,您需要安装 Swift Package Manager,并使用它来构建和运行测试,使用以下命令:swift build --clean && swift build && swift test

鸣谢

此库由 Olivier Halligon 创建,并由 Ilya Puchka 维护。

DipMIT 许可证 下可用。 有关更多信息,请参见 LICENSE 文件。

README.md 顶部的动画 GIF 来自 这个食谱,来自 Kevin & Amanda 的美味博客。 去试试这个食谱吧!

用作 SampleApp 启动屏幕和图标的图像来自 Matthew Hine 并且属于 CC-by-2.0