简体   繁体   English

iOS 依赖注入(台风)

[英]iOS Dependencies injection (typhoon)

I found nice tutorial that show how dependency injection works.我找到了很好的教程,展示了依赖注入是如何工作的。

But I still can't understand for which purposes we need it?但我还是不明白我们需要它的目的是什么?

It was understandable for me how to use abstraction, but I don't understand for which purposes we need this code below:对我来说如何使用抽象是可以理解的,但我不明白我们需要下面这段代码的目的是什么:

- (BNRItemsViewController *)itemsViewController {
  return [TyphoonDefinition withClass:[BNRItemsViewController class] configuration:^(TyphoonDefinition *definition) {
    [definition injectProperty:@selector(itemStore) with:[self store]];
  }];
}

- (id<BNRItemStore>)store {
  return [TyphoonDefinition withClass:[BNRItemStoreImpl class] configuration:^(TyphoonDefinition *definition) {
    [definition useInitializer:@selector(sharedStore) parameters:^(TyphoonMethod *initializer) {
      definition.scope = TyphoonScopeLazySingleton;
    }];
  }];
}

What is actually itemsViewController and how it will be called and how we get the items without setting it outside.什么是itemsViewController以及如何调用它以及如何在不将其设置在外部的情况下获取项目。

What the main goal for injection?注射的主要目标是什么? So I can seems simple do this:所以我可以看起来很简单:

BNRItemsViewController *vc = ...
vc.itemStore = [BNRItemStore ...]

what is the advantage for me using library?使用图书馆对我有什么好处?

There are a couple advantages that Typhoon gives you: Typhoon 为您提供了几个优势:

1) In the case of: 1) 在以下情况下:

BNRItemsViewController *vc = ...
vc.itemStore = [BNRItemStore ...]

you are manually instantiating the view controller, and then setting the itemStore to a concrete class (BNItemStore) which is now hard-coded into this and any other class that instantiates BNRItemsViewController.您正在手动实例化视图控制器,然后将 itemStore 设置为一个具体类 (BNItemStore),该类现在被硬编码到该类和任何其他实例化 BNRItemsViewController 的类中。 But Typhoon injects the itemStore property automatically at initialization, regardless of where that initialization happens .但是 Typhoon 在初始化时会自动注入 itemStore 属性,而不管初始化发生在哪里 Even if a storyboard segue initializes the BNRItemsViewController class (no manual instantiation in your code), it will return from initialization with the itemStore property already set automatically by the dependency injection framework.即使 storyboard segue 初始化了 BNRItemsViewController 类(您的代码中没有手动实例化),它也会从初始化返回,并且 itemStore 属性已经由依赖注入框架自动设置。 And it will be injected with the same value every place it is instantiated, without your code having to set that property to a hard-coded concrete class every time.它将在每个实例化的地方注入相同的值,而无需您的代码每次都将该属性设置为硬编码的具体类。

2) You don't have to change your code in one or more places to change what concrete implementation of the BNRItemStore protocol is passed into the BNRItemsViewController. 2) 您不必在一处或多处更改您的代码来更改将 BNRItemStore 协议的具体实现传递给 BNRItemsViewController。 Instead, you define what set of concrete injections will occur throughout the app in your info.plist, where you specify the Typhoon assembly that will be used.相反,您可以在 info.plist 中定义整个应用程序中将发生的具体注入集,您可以在其中指定将使用的 Typhoon 程序集。 This means that you can have a test assembly, a staging assembly, a prod assembly, and have all the proper dependencies required for each of those scenarios injected throughout the app, controlled from a single location and not hard-coded into your actual source code.这意味着您可以拥有一个测试程序集、一个暂存程序集、一个生产程序集,并拥有在整个应用程序中注入的每个场景所需的所有适当依赖项,从一个位置进行控制,而不是硬编码到您的实际源代码中. In other words, you can change the entire graph of what dependencies will be used for any given build of your app without touching the source code, and based on what type of context the app is running in (test, dev, release, etc.)换句话说,您可以在不接触源代码的情况下,根据应用程序运行的上下文类型(测试、开发、发布等)更改将用于任何给定应用程序构建的依赖项的整个图表。 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM