Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using typhoon assemblied model property in another definition

I have problem with Typhoon framework. I have assembly that construct my data model:

- (DataModel *)dataModel {
    return [TyphoonDefinition withClass:[DataModel class]];
}

now I want to assembly all my view models. In one definition I need to decide if password was already set by user. This information is stored in the data model. So my definition looks like this:

- (id)passViewModel {
    return [TyphoonDefinition withClass:[PasscodeViewModel class] configuration:^(TyphoonDefinition *definition) {
        [definition useInitializer:@selector(initwithType:) parameters:^(TyphoonMethod *initializer) {
            NSNumber *type = [self.modelAssembly dataModel] isPasscodeSet ? @(TypeReturning) : @(TypeNew);
            [initializer injectParameterWith:type];
        }];
    }];
}

The problem is that when definitions are activated, the dataModel is TyphoonDefinition, not DataModel.

Is there any method that can allows me to get value of property of dataModel?

EDIT:

According to the answer below, my assembly looks like this now:

- (UIViewController *)passcodeViewController {
    return [TyphoonDefinition withOption:[(id)[self.modelAssembly dataModel] property:@selector(isPasscodeSet)]
                                     yes:[self passcodeViewController:@(TypeReturning)]
                                      no:[self passcodeViewController:@(TypeNew)]];
}

- (UIViewController *)passcodeViewController:(NSNumber *)entryType {
    return [TyphoonDefinition withClass:[PasscodeViewController class] configuration:^(TyphoonDefinition *definition) {
        [definition injectProperty:@selector(entryType) with:entryType];
        [definition injectProperty:@selector(viewModel) with:[self.viewModelsAssembly passcodeViewModel:entryType]];
    }];
}

I'm using "passcodeViewController" as typhoon key in storyboard. Unfortunately the viewModel and entryType that I try to inject are nil.

like image 316
MichalMoskala Avatar asked Feb 13 '26 17:02

MichalMoskala


1 Answers

You're right, an assembly can be either active or inactive so its not designed to do what you'd like to do. Therefore, there's a special feature for your use-case called Typhoon Option Matcher.

You can return one definition or another based on the start of another object in the (activated) assembly or a runtime argument. Example:

- (id)passViewModel
{
    return [TyphoonDefinition withOption:[(id)[self.modelAssembly dataModel] 
        property:@selector(isPasscodeSet)]
        yes:[self passViewModelWithType:@(TypeReturning)] 
        no:[self passViewModelWithType:@(TypeNew)]];
}

- (id)passViewModelWithType:(NSNumber *)type
{
    return [TyphoonDefinition withClass:[PasscodeViewModel class] 
      configuration:^(TyphoonDefinition *definition) {
        [definition useInitializer:@selector(initwithType:) 
          parameters:^(TyphoonMethod *initializer) {
            [initializer injectParameterWith:type];
        }];
    }];
}

Does this suit your needs? Perhaps you can combine this with factory definitions.

like image 166
Jasper Blues Avatar answered Feb 15 '26 08:02

Jasper Blues



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!