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

TyphoonConfig factories shorthands #220

Closed
alexgarbarev opened this issue May 21, 2014 · 3 comments
Closed

TyphoonConfig factories shorthands #220

alexgarbarev opened this issue May 21, 2014 · 3 comments

Comments

@alexgarbarev
Copy link
Contributor

When moving some business logic from assembly to properties file, I face with problem when I want to specify application settings in properties file.

For example, I want to specify offline mode in properties file. Let's look at how this can be done.
I have next definitions:

- (id)webService;
- (id)afnetworkConnection;
- (id)offlineConnection;

webService definition requires afnetworkConnection/offlineConnection definition as dependency.

Then I have to create ConnectionFactory class:

@interface ConnectionFactory : NSObject
@property (nonatomic) BOOL offline;
@property (nonatomic, strong) MyAssembly *assembly;
- (id) newConnection;
@end

where

- (id)newConnection
{
    if (self.offline) {
        return [self.assembly offlineConnection];
    } else {
        return [self.assembly afnetworkConnection];
    }
}

then, create definitions:

- (id)connectionFactory
{
    return [TyphoonDefinition withClass:[ConnectionFactory class] configuration:^(TyphoonDefinition *definition) {
        [definition injectProperty:@selector(assembly) with:self];
        [definition injectProperty:@selector(offline) with:TyphoonConfig(@"webservice.offline")];
    }];
}

- (id)connection
{
    return [TyphoonDefinition withFactory:[self connectionFactory] selector:@selector(newConnection)];
}

and finally I can inject connection definition into webService definition.

Hope all was clear.

Now I want to introduce extension for TyphoonDefinition to simplify this case.
Instead of adding the 2 definitions and implementing the factory class just adding the one definition:

- (id)connection
{
    return [TyphoonDefinition withConfig:TyphoonConfig(@"webservice.offline") 
                                     yes:[self offlineConnection] 
                                      no:[self afnetworkConnection]];
}

where "webservice.offline" - bool value.

and another method for string value:

- (id)tokenStorage
{
    return [TyphoonDefinition withConfig:TyphoonConfig(@"webservice.tokenStorage") 
                                 mapping: @{ 
                                             @"keychain" : [self keychainStorage], 
                                             @"userDefaults" : [self userDefaultsStorage],
                                             @"database" : [self databaseStorage]
                                 }];
}
@jasperblues
Copy link
Member

Where is the example in tests?

@alexgarbarev
Copy link
Contributor Author

Check the AssemblyWithDefinitionConfiguration.m file

And the tests

@jasperblues
Copy link
Member

Looks good. Last task is to update the documentation. Will do this shortly.

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