objective-ctyphoon

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.


Solution

  • Dependency Injection:

    Typhoon helps to apply the dependency injection pattern - an object oriented software approach whereby:

    In this way:

    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.

    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:

    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.

    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