Skip to content

Quick Start

Jasper Blues edited this page Jan 25, 2015 · 49 revisions

Setting up a Dependency Injection container couldn't be more easy.

First, create a sub-class of TyphoonAssembly as follows:

@interface MiddleAgesAssembly : TyphoonAssembly

- (Knight*)basicKnight;

- (Knight*)cavalryMan;

- (id<Quest>)defaultQuest;

@end

Add the method names in the header. This will allow compile-time checking and IDE code-completion. Now simply define instances to be built:


###Perform an Initializer Injection

- (Knight*)basicKnight
{
    return [TyphoonDefinition withClass:[Knight class] 
        configuration:^(TyphoonDefinition* definition)
    {
        [definition useInitializer:@selector(initWithQuest:) 
            parameters:^(TyphoonMethod *initializer) {

            [initializer injectParameterWith:[self defaultQuest]];

        }];
    }];
}

- (id<Quest>)defaultQuest
{
    return [TyphoonDefinition withClass:[CampaignQuest class]];
}

###Obtain built instances from the container as follows:

TyphoonComponentFactory* factory = [[TyphoonBlockComponentFactory alloc] 
    initWithAssemblies:@[

    [MiddleAgesAssembly assembly]
]];

//Code-completion + no 'magic strings'  
Knight* knight = [(MiddleAgesAssembly*) factory basicKnight]; 

And we're done!



Key Concept: Before activation each method returns in a TyphoonAssembly returns a TyphoonDefinition. After activation we'll use the same interface to return built instances. Since the main use for your assembly interfaces will be emitting built components, you can declare the return type as the type being built.