简体   繁体   中英

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:

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

my appAssembly looks like this

- (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

Calling the instance of dogVC from SceneDelegate is quite easy as I do have an access to it:

self.viewController     = [self.appAssembly dogVC];

I do not understand how to pass the very same appAssembly instance to a dogVC and then to 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 .

There is a guide on GitHub about injecting Assembly itself. So I created a property appAssembly in a dogVC of type TyphoonComponentFactory . Here is how my initializing method inside my appAssembly for a dogVC looks like:

- (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. 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:

  • The key actors and their interactions with other core software components in a system a declared at the composition root .

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.

  • The first controller will depend on some singleton services to do its job.
  • We may then wish to transition from one view controller to another. We can load an 'object graph' consisting of the view controller and it dependencies.
  • When the next controller is presented, we can release the current one.

Factory Pattern:

To transition from one controller to another we can use Typhoon as a factory for emitting built instances. The factory pattern allows us to:

  • Obtain an instance (ie a view controller) with a mix of runtime and static dependencies.

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. 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. Meanwhile services or other shared infrastructure will be shared.

  • The default scope is TyphoonScopeObjectGraph
  • To create a shared class use definition.scope = TyphoonScopeSingleton as documented here .

Pilgrim:

Typhoon is, at least in my opinion, the best and most flexible DI library for objective-C. Meanwhile, if you use Swift you might like to try simpler and better: pilgrim.ph

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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