1
votes

In a legacy application, where I'm gradually introducing Typhoon, if I want to instantiate an instance from Typhoon from a class that was not itself instantiated from Typhoon, I can place the following in my AppDelegate:

[self.assembly makeDefault];

And the definition for AppDelegate in my assembly:

- (AppDelegate *)appDelegate
{
    return [TyphoonDefinition withClass:[PFAppDelegate class] 
      configuration:^(TyphoonDefinition *definition)
    {
          [definition injectProperty:@selector(assembly) with:self];
    }];
}

Elsewhere I can do the following to get the instance of the assembly:

MyAssembly* assembly = [MyAssembly defaultAssembly];

This is ok, but suppose my application is composed by the following:

  • Main project that contains various child projects.
  • Main project has the pod dependency to Typhoon, assembly is written here and has the definition of objects to be injected in his child projects.

Because child projects do not know of the Main project I cannot #import the assembly and use: MyAssembly* assembly = [MyAssembly defaultAssembly];

How should I proceed in this scenario?

1

1 Answers

1
votes

In a typical Typhoon application you will have:

  • One instance of TyphoonComponentFactory
  • A handful of assemblies separated into you logical architectural layers. These assemblies may reference each other.

Assemblies contain blue-prints (or recipes) for the instantiation of objects. At start up all of this information is collected and stored in a TyphoonComponentFactory.

Once the application has started, the assemblies themselves are essentially discarded, but your assembly interfaces can still act as a facade in front of a TyphoonComponentFactory - method calls form the assembly interface use Objective-C forwarding and are converted into [factory componentForKey].

Therefore

  • You can cast TyphoonComponentFactory to any of your assembly interfaces and it will work.
  • You can cast any of your assembly interfaces to your child assembly interfaces and it will work.
  • You can cast your assembly interfaces to TyphoonComponentFactory.

This advice is assuming you have one instance of TyphoonComponentFactory. In a very complex app, you might have more, one for an engine or library for example and one for the main app, and these parts need not know that the others are backed by Typhoon. But if you have a set of assemblies that reference each other, and are instantiated together at start-up to form one TyphoonComponentFactory then you can cast between them. Just use:

MyChildAssembly *childAssembly = (MyChildAssembly) parentAssembly;