微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Uber Needle Swift 的依赖注入框架

程序名称:Uber Needle

授权协议: Apache

操作系统: OS X

开发语言: Swift

Uber Needle 介绍

Needle 是 Uber 开发的一个 Swift 的依赖注入框架。和其他 DI 框架(如
Cleanse,
Swinject ) 不同的是,Needle 鼓励层次化的 DI
结构以及利用代码生成器来确保编译时安全。这样我们在修改应用代码的时候可以更有信心,如果能编译通过就表示其执行就会正常。Needle 更像是 Dagger
for the JVM
.

Needle 主要实现以下目标:

  1. 通过确保依赖注入代码的编译时安全来提供可靠性
  2. 确保代码生成是高性能
  3. 兼容所有 iOS 应用架构,包括 RIBs, MVx 等.

示例代码

/// This protocol encapsulates the dependencies acquired from ancestor scopes.
protocol MyDependency: Dependency {
    /// These are objects obtained from ancestor scopes, not newly introduced at this scope.
    var chocolate: Food { get }
    var milk: Food { get }
}

/// This class defines a new dependency scope that can acquire dependencies from ancestor scopes
/// via its dependency protocol, provide new objects on the DI graph by declaring properties,
/// and instantiate child scopes.
class MyComponent: Component<MyDependency> {

    /// A new object, hotChocolate, is added to the dependency graph. Child scope(s) can then
    /// acquire this via their dependency protocol(s).
    var hotChocolate: Drink {
        return HotChocolate(dependency.chocolate, dependency.milk)
    }

    /// A child scope is always instantiated by its parent(s) scope(s).
    var myChildComponent: MyChildComponent {
        return MyChildComponent(parent: self)
    }
}

Uber Needle 官网

https://github.com/uber/needle

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐