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

Singleton auto injection #320

Closed
sdkdimon opened this issue Feb 17, 2015 · 1 comment
Closed

Singleton auto injection #320

sdkdimon opened this issue Feb 17, 2015 · 1 comment

Comments

@sdkdimon
Copy link

Hi. When some object injected, can Typhoon automatically also inject that object into some singleton object, on the fly?

Example:

@interface ViewModelAssembly ()

@property(strong,nonatomic) DataControllerAssemly *dataControllerAssemly;
@property(strong,nonatomic) CoreAssembly *coreAssemly;
@end

@implementation ViewModelAssembly

-(id)splashScreenViewModel{
    return [TyphoonDefinition withClass:[VCSplashScreenViewModel class] configuration:^(TyphoonDefinition *definition) {
        [definition injectProperty:@selector(backgroundImageName) with:@"pm_splash"];
        [definition injectProperty:@selector(authDataController) with:[_dataControllerAssemly authDataController]];
        [definition injectProperty:@selector(locationDataController) with:[_dataControllerAssemly locationDataController]];
        #Inject created VCSplashScreenViewModel instance into localize manager
        [[[self coreAssemly] localizeManager] injectMethod:@selector(registerObject:) parameters:^(TyphoonMethod *method) {
          # [method injectParameterWith:definition];
            [method injectParameterWith:[self splashScreenViewModel]];
        }];
    }];
}

@end

Where core assembly is:

@implementation CoreAssembly

-(id)localizeManager{
    return [TyphoonDefinition withClass:[LocalizeManager class] configuration:^(TyphoonDefinition *definition) {
        [definition useInitializer:@selector(manager)];
        [definition setScope:TyphoonScopeLazySingleton];
    }];
}
@end
@sdkdimon
Copy link
Author

Found solution.
I am forgot for ViewControllerAssembly, for what all it.

@interface ViewControllerAssembly ()
@property(strong,nonatomic) CoreAssembly *coreAssembly;
@property(strong,nonatomic) ViewModelAssembly *viewModelAssembly;
@end

@implementation PMViewControllerAssembly

-(id)splashScreenViewController{
    return [TyphoonDefinition withClass:[VCSplashScreen class] configuration:^(TyphoonDefinition *definition) {
        [definition useInitializer:@selector(initWithNibName:bundle:) parameters:^(TyphoonMethod *initializer) {
            [initializer injectParameterWith:@"SplashScreenView"];
            [initializer injectParameterWith:[NSBundle mainBundle]];
        }];
       #Inject viewModel, but before invoke registerObject mehtod on localize manager with that view model
       [definition injectProperty:@selector(viewModel) with:[_coreAssembly localizeManagerRegister:[_viewModelAssembly splashScreenViewModel]]];
    }];
}
@interface ViewModelAssembly ()

@property(strong,nonatomic) DataControllerAssemly *dataControllerAssemly;
@property(strong,nonatomic) CoreAssembly *coreAssemly;
@end

@implementation ViewModelAssembly

-(id)splashScreenViewModel{
    return [TyphoonDefinition withClass:[VCSplashScreenViewModel class] configuration:^(TyphoonDefinition *definition) {
        [definition injectProperty:@selector(backgroundImageName) with:@"pm_splash"];
        [definition injectProperty:@selector(authDataController) with:[_dataControllerAssemly authDataController]];
        [definition injectProperty:@selector(locationDataController) with:[_dataControllerAssemly locationDataController]];
}

@end
@implementation CoreAssembly

-(id)localizeManager{
    return [TyphoonDefinition withClass:[LocalizeManager class] configuration:^(TyphoonDefinition *definition) {
        [definition useInitializer:@selector(manager)];
        [definition setScope:TyphoonScopeLazySingleton];
    }];
}

-(id)localizeManagerRegister:(id)obj{
    return [TyphoonDefinition withFactory:[self localizeManager] selector:@selector(registerObject:) parameters:^(TyphoonMethod *factoryMethod) {
        [factoryMethod injectParameterWith:obj];
    }];
}

@end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant