Typhoon not injecting property (without storyboard)

佐手、 提交于 2019-12-01 22:17:01

Typhoon is a dependency injection container, meaning it doesn't instrument, swizzle or otherwise touch your classes. Therefore to get an instance of a class with dependencies injected, we have to ask Typhoon.

Why not for Storyboards?

When using plist integration, Typhoon registers TyphoonStoryboard in place of UIStoryboard to emit instances based on definitions in the storyboard - works just like a normal storyboard with the added benefit that dependencies are injected.

In other places, to get instances with dependencies injected, we use the assembly interface, and ask Typhoon.

Solution Steps:

Add a definition for ViewControllerB in your assembly. (In Objective-C you can also use auto-injection macros if you wish).

- (ViewControllerB *)viewControllerB
{
    return [TyphoonDefinition withClass:[ViewControllerB class] 
        configuration:^(TyphoonDefinition *definition) {

        [definition useInitializer:@selector(initWithNibName:bundle:) 
            parameters:^(TyphoonMethod *initializer) {

            [initializer injectParameterWith:@"ViewControllerB"];
            [initializer injectParameterWith:[NSBundle mainBundle]];
        }];

        [definition injectProperty:@selector(assembly) with:self];
    }];
}

Add a property to ViewControllerB:

//Start with this, next task is to back this with a protocol
@property(nonatomic, strong) AppAssembly *assembly;

Now change the the button action that instantiates ViewControllerC:

ViewControllerC *viewControllerC = self.assembly.viewControllerC
[self.navigationController pushViewController:viewControllerC animated:YES];

Now my application class depends on my TyphoonAssembly, what if I don't want this?

All Typhoon assemblies can be backed by a protocol, so that your application sees only a provider of instances, and not Typhoon. If you ever wished to migrate from Typhoon, simply provide an alternative implementation of that protocol - you'll still have a robust architecture in place.

So, I had to first ask my AppAssembly for an instance of viewControllerB with the assembly injected and then I was able to use it to get an instance of viewControllerC.

AppAssembly code:

- (ViewControllerB *)viewControllerB
{
    return [TyphoonDefinition withClass:[ViewControllerB class]
                          configuration:^(TyphoonDefinition *definition) {
                              [definition useInitializer:@selector(initWithNibName:bundle:)
                                              parameters:^(TyphoonMethod *initializer) {
                                                  [initializer injectParameterWith:@"ViewControllerB"];
                                                  [initializer injectParameterWith:nil];
                                              }];

                              [definition injectProperty:@selector(assembly) with:self];
                          }];
}

- (ViewControllerC *)viewControllerC
{
    return [TyphoonDefinition withClass:[ViewControllerC class]
                          configuration:^(TyphoonDefinition *definition) {
                              [definition useInitializer:@selector(initWithNibName:bundle:)
                                              parameters:^(TyphoonMethod *initializer) {
                                                  [initializer injectParameterWith:@"ViewControllerC"];
                                                  [initializer injectParameterWith:nil];
                                              }];

                              [definition injectProperty:@selector(name) with:@"Injected string"];
    }];
}

ViewControllerA code:

@implementation ViewControllerA

- (IBAction)buttonAction:(id)sender
{
    ViewControllerB *viewControllerB = [[[AppAssembly new] activate] viewControllerB];
    [self.navigationController pushViewController:viewControllerB animated:YES];
}

@end

ViewControllerB code:

@implementation ViewControllerB

- (IBAction)buttonAction:(id)sender 
{
    ViewControllerC *viewControllerC = [self.assembly viewControllerC];
    [self.navigationController pushViewController:viewControllerC animated:YES];
}

@end

ViewControllerC code:

@implementation ViewControllerC

- (void)viewDidLoad 
{
    [super viewDidLoad];
    self.title = self.name;
}

@end

As you can see I had to do this: ViewControllerB *viewControllerB = [[[AppAssembly new] activate] viewControllerB]; Not sure if there is another way. Because of doing that I was able to have the assembly injected in ViewControllerB, and that let me do this: ViewControllerC *viewControllerC = [self.assembly viewControllerC]; However, I noticed that I could also do ViewControllerC *viewControllerC = [[[AppAssembly new] activate] viewControllerC];, so not sure which the better approach is. Anyway, I think I had to call new and activate at least once.

Thanks.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!