Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initializer, property and collection injection: replace instanceof with polymorphism #162

Closed
jasperblues opened this issue Feb 2, 2014 · 0 comments

Comments

@jasperblues
Copy link
Member

We can clean up this long ugly method by using polymorphism on the TyphoonInitializer base:

//FIXME: replace parameter.type == with polymorphism
- (void)configureInvocation:(NSInvocation*)invocation withFactory:(TyphoonComponentFactory*)factory
{
    NSArray* injectedParameters = [self injectedParameters];
    for (id <TyphoonInjectedParameter> parameter in injectedParameters)
    {
        if (parameter.type == TyphoonParameterInjectionTypeReference)
        {
            TyphoonParameterInjectedByReference* byReference = (TyphoonParameterInjectedByReference*) parameter;
            [[factory stack] peekForKey:byReference.reference]; //Raises circular dependencies exception if already initing.
            id reference = [factory componentForKey:byReference.reference];
            [invocation setArgument:&reference atIndex:parameter.index + 2];
        }
        else if (parameter.type == TyphoonParameterInjectionTypeStringRepresentation)
        {
            TyphoonParameterInjectedWithStringRepresentation* byString = (TyphoonParameterInjectedWithStringRepresentation*) parameter;
            [self setArgumentFor:invocation index:byString.index + 2 textValue:byString.textValue requiredType:[byString resolveType]];
        }
        else if (parameter.type == TyphoonParameterInjectionTypeObjectInstance)
        {
            TyphoonParameterInjectedWithObjectInstance* byInstance = (TyphoonParameterInjectedWithObjectInstance*) parameter;
            id value = byInstance.value;
            BOOL isValuesIsWrapper = [value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSValue class]];

            if (isValuesIsWrapper && [byInstance isPrimitiveParameter])
            {
                [self setPrimitiveArgumentForInvocation:invocation index:parameter.index + 2 fromValue:value];
            }
            else
            {
                [invocation setArgument:&value atIndex:parameter.index + 2];
            }
        }
        else if (parameter.type == TyphoonParameterInjectionTypeAsCollection)
        {
            TyphoonParameterInjectedAsCollection* asCollection = (TyphoonParameterInjectedAsCollection*) parameter;

            //FIXME: This shouldn't be a concern of the TyphoonComponentFactory, but of the collection type.
            id collection = [factory buildCollectionWithValues:asCollection.values requiredType:asCollection.collectionType];
            [invocation setArgument:&collection atIndex:parameter.index + 2];
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants