dev.nikolaz dev.nikolaz - 1 month ago 15
iOS Question

Typhoon Inject Property to a new object

I am start to use DI containers on iOS and I don't have idea how to inject property in this case:

Imagine we have:

Assembly.m

- (ClassA *)classA {
return [TyphoonDefinition withClass:[ClassA class] configuration:^(TyphoonDefinition *definition) {
[definition injectProperty:@selector(url) with:@"http://inject:URL"];
}];
}


And two classes

ClassA

@interface ClassA : NSObject
@property (nonatomic,readwrite) NSString *url;
@end


ClassB

@interface ClassB : ClassA
@end


And

ViewController.m

@interface ViewController ()
@end

@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];

ClassB *cB = [ClassB new];
NSLog(@"%@",cB.url);
}


I want to where I create new object by ClassB, all objects has injection with url property from my DI container (assembly)
Yes I can add property to my ViewController and this injection works fine, but I need inject property to new object.

Maybe I don't understand all principles but I should ask. Thx

Answer

Try this.

- (id)classAObject{
    return [TyphoonDefinition withClass:[ClassA class]
                          configuration:^(TyphoonDefinition *definition){
                              [definition injectProperty:@selector(url) with:@"http://inject:URL"];
                          }];
}

- (id)classBObject{
    return [TyphoonDefinition withParent:[self classAObject] class:[ClassB class]];
}

KSTTestAssembly *assembly = [[KSTTestAssembly alloc] init];
[assembly activate];

id classB = [assembly classBObject];