I have checked the idea of having a protocol implemented by the assembly and injecting the assembly as a property to a view controller that expects something that implements that protocol. This is explained here: https://github.com/appsquickly/Typhoon/wiki/backed-by-a-factory-protocol (with a FriendListViewController example).
However, without using storyboards, how should I instantiate the FriendListViewController so that Typhoon injects the assembly?
In other examples, I have seen that you have to do the following:
FriendListViewController *flvc = [[[MyAppAssembly new] activate] friendListController];
Is this the correct way? If this is correct, then anytime I need a dependency I could always use
[[[MyAppAssembly new] activate] aDependencyX]
why bothering to inject the assembly?
Thanks.
It would be incorrect to re-instantiate a new instance of Typhoon as shown above.
There are two ways to instantiate Typhoon.
You can use either way (the former is recommended if you're using storyboards), but you should only do this once per application, or set of assemblies.
After you've instantiated Typhoon, you can proceed from one object graph to another, by injecting the assembly into the object that will be instantiating a new object from Typhoon. The Typhoon sample application for Objective-C or Swift shows plenty of examples of this.
In a legacy application, where you're gradually introduce Typhoon, you may wish to instantiate an instance from Typhoon from a class that was not itself instantiated from Typhoon. In this case you can place the following in your AppDelegate:
[self.assembly makeDefault];
And a definition for AppDelegate in your assembly:
- (AppDelegate *)appDelegate
{
return [TyphoonDefinition withClass:[PFAppDelegate class]
configuration:^(TyphoonDefinition *definition)
{
[definition injectProperty:@selector(assembly) with:self];
};
}];
}
And then obtain it elsewhere with:
MyAssembly* assembly = [MyAssembly defaultAssembly];
Plist Integration
Plist integration is a way to instantiate Typhoon. We put some lines in your AppInfo.plist file et voila - Typhoon is running. From there you should inject the Typhoon assembly into the classes that need it. Without storyboards, start with the AppDelegate or root view controller.
By injecting the TyphoonAssembly itself we can use load up the object-graph for a particular use-case - eg a top-level view controller with all dependencies injected. When that use-case has completed, the whole object graph can be discarded.