ios - 在另一个定义中使用台风组装模型属性

标签 ios objective-c dependency-injection typhoon

我对 Typhoon 框架有疑问。我有构建数据模型的程序集:

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

现在我想组装我所有的 View 模型。在一个定义中,我需要确定用户是否已经设置了密码。此信息存储在数据模型中。所以我的定义是这样的:

- (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];
        }];
    }];
}

问题是当定义被激活时,dataModel 是 TyphoonDefinition,而不是 DataModel。

有什么方法可以让我获取dataModel的属性值吗?

编辑:

根据下面的回答,我的程序集现在是这样的:

- (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]];
    }];
}

我在 Storyboard中使用“passcodeViewController”作为台风键。不幸的是,我尝试注入(inject)的 viewModel 和 entryType 为零。

最佳答案

你是对的,一个程序集可以是事件的也可以是非事件的,所以它不是为了做你想做的事而设计的。因此,您的用例有一个名为 Typhoon Option Matcher 的特殊功能.

您可以根据(激活的)程序集中另一个对象的开始或运行时参数返回一个或另一个定义。示例:

- (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];
        }];
    }];
}

这符合您的需求吗?也许您可以将其与 factory definitions 结合使用.

关于ios - 在另一个定义中使用台风组装模型属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31696108/

相关文章:

ios - 添加新授权过程后 MapKit 崩溃

objective-c - 收听我的文本字段的值更改

iphone - CAScrollLayer 不滚动!

dependency-injection - Windows Phone 7 的依赖注入(inject)

c# - 使用 Unity 有条件地注入(inject)依赖项

iOS - Cocos2d、Box2d 或 Chipmunk

ios - 如何在 iOS/Swift 中处理时区的时间间隔

objective-c - 使用@property(copy) 与@property(retain) 的经验法则是什么?

c# - 是否可以将不同的接口(interface)绑定(bind)到实现所有接口(interface)的类的同一个实例?

ios - 大型移动应用服务器端数据接收采用什么技术