Uber Needle- Swift 的依赖注入框架

网友投稿 1059 2022-10-21

Uber Needle- Swift 的依赖注入框架

Uber Needle- Swift 的依赖注入框架

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

Needle 主要实现以下目标:

通过确保依赖注入代码的编译时安全来提供可靠性确保代码生成是高性能的兼容所有 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 { /// 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) }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:C. Liebig's Barrels (贪心)
下一篇:系统集成行业步入多平台应用与服务创新阶段,小程序化助力行业破局
相关文章

 发表评论

暂时没有评论,来抢沙发吧~