简体   繁体   English

Objective-C,台风,将程序集作为参数传递

[英]Objective-C, Typhoon, passing an Assembly as a parameter

In my application that uses Typhoon library, I've created an AppAssembly that is being initialized in SceneDelegate like this:在我使用 Typhoon 库的应用程序中,我创建了一个AppAssembly ,它在SceneDelegate中被初始化,如下所示:

self.appAssembly        = [[AppAssembly new] activated];

my appAssembly looks like this我的appAssembly看起来像这样

- (Person *)me;
- (Dog *)dog;
- (Cookie *)cookie;
- (DogInteractionVC *)dogVC;
- (HowManyCookiesVC *)howManyCookiesVC;

From SceneDelegate I want to transit to dogVC , Then, from the dogVC , I want to transit to howManyCookiesVC我想从SceneDelegate过渡到dogVC ,然后从dogVC过渡到howManyCookiesVC

Calling the instance of dogVC from SceneDelegate is quite easy as I do have an access to it:SceneDelegate调用dogVC的实例非常简单,因为我可以访问它:

self.viewController     = [self.appAssembly dogVC];

I do not understand how to pass the very same appAssembly instance to a dogVC and then to howManyCookiesVC .我不明白如何将相同的appAssembly实例传递给dogVC ,然后再传递给 howManyCookiesVC When I try to create an instance of AppAssembly in the dogVC , I come across the issue that I believe is called circular dependency .当我尝试在dogVC中创建AppAssembly的实例时,我遇到了我认为称为循环依赖的问题。

There is a guide on GitHub about injecting Assembly itself. GitHub 上有一个关于注入程序集本身的指南。 So I created a property appAssembly in a dogVC of type TyphoonComponentFactory .所以我在TyphoonComponentFactory类型的dogVC 中创建了一个属性 appAssembly Here is how my initializing method inside my appAssembly for a dogVC looks like:这是我在appAssembly中为dogVC初始化的方法如下所示:

- (DogInteractionVC *)dogVC {
   return [TyphoonDefinition withClass:[DogInteractionVC class]
                         configuration:^(TyphoonDefinition *definition) {
      
      [definition useInitializer:@selector(initWithPerson:)
                      parameters:^(TyphoonMethod *initializer) {
         
         [initializer injectParameterWith:[self me]];
      }];
      
      [definition injectProperty:@selector(appAssembly) with:self];
   }];
}

I think the part injectProperty:@selector(appAssembly) is wrong, but I spent a long time understanding that, and I am afraid that I cannot go any further than this without some help from the community.我认为部分injectProperty:@selector(appAssembly)是错误的,但我花了很长时间才理解这一点,而且如果没有社区的帮助,恐怕我不能再进一步 go。 Any help is appreciated.任何帮助表示赞赏。 Thank you.谢谢你。

Side note: Dear community, I am very close to being blocked from posting, as my last posts have not been well received.旁注:亲爱的社区,我非常接近被禁止发帖,因为我最近的帖子没有受到好评。 I believe that question has everything that it needs.我相信这个问题有它需要的一切。 If I am wrong, please let me know before putting your thumbs down so I can understand my mistakes.如果我错了,请在竖起大拇指之前让我知道,以便我理解我的错误。 Thank you.谢谢你。

Dependency Injection:依赖注入:

Typhoon helps to apply the dependency injection pattern - an object oriented software approach whereby: Typhoon 有助于应用依赖注入模式 - 一种面向 object 的软件方法,其中:

  • The key actors and their interactions with other core software components in a system a declared at the composition root .关键参与者及其与系统中其他核心软件组件的交互 a 在组合根中声明。

In this way:这样:

  • We can de-duplicate configuration of shared objects.我们可以删除共享对象的重复配置。
  • We can have the benefits of singletons without overly tight coupling.我们可以在没有过度紧耦合的情况下享受单例的好处。

Using Dependency Injection in a Mobile App:在移动应用程序中使用依赖注入:

When we use dependency injection in a mobile app, we start at the app delegate to launch a view controller.当我们在移动应用程序中使用依赖注入时,我们从应用程序委托开始启动视图 controller。

  • The first controller will depend on some singleton services to do its job.第一个 controller 将依赖一些 singleton 服务来完成其工作。
  • We may then wish to transition from one view controller to another.然后我们可能希望从一个视图 controller 转换到另一个视图。 We can load an 'object graph' consisting of the view controller and it dependencies.我们可以加载一个由视图 controller 及其依赖项组成的“对象图”。
  • When the next controller is presented, we can release the current one.当下一个 controller 出现时,我们可以发布当前的一个。

Factory Pattern:工厂模式:

To transition from one controller to another we can use Typhoon as a factory for emitting built instances.要从一个 controller 过渡到另一个,我们可以使用 Typhoon 作为发射构建实例的工厂。 The factory pattern allows us to:工厂模式允许我们:

  • Obtain an instance (ie a view controller) with a mix of runtime and static dependencies.获取一个混合了运行时和 static 依赖项的实例(即视图控制器)。

So to transition from one view controller to another we can inject Typhoon assembly to be used as a factory to obtain the next view controller.因此,要从一个视图 controller 过渡到另一个视图,我们可以注入 Typhoon 组件用作工厂以获得下一个视图 controller。 To inject the assembly as a factory, docs are here .要将程序集作为工厂注入,文档在这里

Scopes:范围:

Depending on navigation style, controllers will typically retained in memory as long as used and then released.根据导航风格,控制器通常会保留在 memory 只要使用然后释放。 Meanwhile services or other shared infrastructure will be shared.同时将共享服务或其他共享基础设施。

  • The default scope is TyphoonScopeObjectGraph默认的 scope 是TyphoonScopeObjectGraph
  • To create a shared class use definition.scope = TyphoonScopeSingleton as documented here .要创建共享 class 使用definition.scope = TyphoonScopeSingleton ,如此所述。

Pilgrim:朝圣:

Typhoon is, at least in my opinion, the best and most flexible DI library for objective-C.至少在我看来,Typhoon 是 objective-C 最好和最灵活的 DI 库。 Meanwhile, if you use Swift you might like to try simpler and better: pilgrim.ph同时,如果您使用 Swift,您可能想尝试更简单更好的: pilgrim.ph

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

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