I've defined an ApplicationAssembly in Typhoon.
So what I want to do is say: "This class X needs to be injected with something conforming to the Foo protocol. This is a RealFoo, this is a TestFoo. When I'm running X in real life, I want it to get a RealFoo, but when I'm running my integration tests, I want it to get a TestFoo".
How can I do this?
There are several recommended ways to do this:
Use the Typhoon Patcher
Typhoon-patcher allows loading a base assembly, but with one or more components patched out with another definition, or a given object instance. Here's an example of patching out a component with a mock:
MiddleAgesAssembly* assembly = [MiddleAgesAssembly assembly];
TyphoonComponentFactory* factory = [TyphoonBlockComponentFactory factoryWithAssembly:assembly];
TyphoonPatcher* patcher = [[TyphoonPatcher alloc] init];
[patcher patchDefinition:[assembly knight] withObject:^id
{
Knight* mockKnight = mock([Knight class]);
[given([mockKnight favoriteDamsels]) willReturn:@[
@"Mary",
@"Janezzz"
]];
return mockKnight;
}];
[factory attachPostProcessor:patcher];
Knight* knight = [factory componentForKey:@"knight"];
Group Environment Dependent Components Together
Another approach is to group environment dependent components together. If you're using the XML style assembly, you can load a different set of files for production vs test scenarios, including the base assembly and any environment dependent files.
The same thing can be achieved in the block-based assembly, as follows:
TyphoonComponentFactory* factory = [[TyphoonBlockComponentFactory alloc] initWithAssemblies:@[
[MiddleAgesAssembly assembly],
[StarWarsAssembly assembly]
]];
Knight* cavalryMan = [(MiddleAgesAssembly*) factory cavalryMan];
Knight* stormTrooper = [(StarWarsAssembly*) factory stormTrooper];
For more information consult Modularization of Assemblies in the Typhoon documentation, or check out the sample app, which contains an example of this.
Use a TyphoonConfig
Another approach is to use TyphoonConfig. Details for this feature are here.
Edit:
The above example is for Typhoon 2.0. This still works fine with Typhoon 3.0, but somewhat neater is assembly activation:
MiddleAgesAssembly *assembly = [[MiddleAgesAssembly new] activate];
Knight *knight = [assembly knight];
- In Typhoon 3.0 you only need to declare collaborating assemblies if they are backed by a protocol not a concrete type, or if you wish to override one of your assemblies.
- You can resolve components from the collaborating assemblies with eg
[assembly.colloaboratingAssembly stormTrooper]
来源:https://stackoverflow.com/questions/19838570/typhoon-how-to-get-an-instance-conforming-to-a-protocol-for-production-and-ano