在其他组件中重用组件

时间:2016-12-13 07:28:49

标签: ios dependency-injection typhoon

我正试图将台风融入我的项目中。

我有三个类,DBManager,ProductDataManager和CustomerDataManager。 ProductDataManager和CustomerDataManager都依赖DBManager进行数据库操作。

@interface ProductDataManager ()
@property (nonatomic, strong) DBManager *dbManager;
@end

@implementation ProductDataManager

- (instancetype)initWithDBManager:(DBManager*)dbManager
{
    self = [super init];
    if (self) {
        _dbManager = dbManager;
    }
    return self;
}


@interface CustomerDataManager ()
@property (nonatomic, strong) DBManager *dbManager;
@end

@implementation CustomerDataManager

- (instancetype)initWithDBManager:(DBManager*)dbManager
{
    self = [super init];
    if (self) {
        _dbManager = dbManager;
    }
    return self;
}

现在我为他们创建了三个程序集。

@interface DBManagerAssembly ()

@end

@implementation DBManagerAssembly

- (DBManager *)dbManager {
    return [TyphoonDefinition withClass:[DBManager class] configuration:^(TyphoonDefinition *definition) {
        }];
        definition.scope = TyphoonScopeSingleton;
    }];
}






@interface CustomerDataManagerAssembly ()
@property (nonatomic, strong) DBManagerAssembly *dbManagerAssembly;
@end

@implementation CustomerDataManagerAssembly

- (CustomerDataManager *)customerDataManager {
    return [TyphoonDefinition withClass:[CustomerDataManager class] configuration:^(TyphoonDefinition *definition) {
        [definition useInitializer:@selector(initWithDBManager:) parameters:^(TyphoonMethod *initializer) {
            [initializer injectParameterWith:self.dbManager];
        }];
    }];
}

- (DBManager *)dbManager {
    return [self.dbManagerAssembly dbManager];
}





@interface ProductDataManagerAssembly ()
@property (nonatomic, strong) DBManagerAssembly *dbManagerAssembly;
@end

@implementation ProductDataManagerAssembly

- (ProductDataManager *)productDataManager {
    return [TyphoonDefinition withClass:[ProductDataManager class] configuration:^(TyphoonDefinition *definition) {
        [definition useInitializer:@selector(initWithDBManager:) parameters:^(TyphoonMethod *initializer) {
            [initializer injectParameterWith:self.dbManager];
        }];
    }];
}

- (DBManager *)dbManager {
    return [self.dbManagerAssembly dbManager];
}

在AppDelegate中激活它们

- (NSArray *)initialAssemblies
{
    return @[[DBManagerAssembly class], [ProductDataManagerAssembly class], [CustomerDataManagerAssembly class]];
}

但是在应用启动时,它会引发异常

[NSException raise:NSInvalidArgumentException format:@"Key '%@' is already registered.", _definition.key];

我的问题是......如何正确重用汇编并避免异常?

1 个答案:

答案 0 :(得分:2)

Typhoon允许您创建系统中关键架构角色的逻辑分组。此功能称为Assembly Modularization。定义的所有组件都放在一个TyphoonComponentFactory中 - 其中包含用于发出构建实例的配方。

assembly modules

在激活之前,程序集返回定义。在运行时,您可以将程序集用作外观以返回构建的实例。它使用Objective-C消息转发来解析对[factory componentForKey:theMethodName]的方法调用。因此:

  • 整个程序集集中的每个组件都必须具有唯一的名称。

在您的代码中,您已定义:

- (DBManager *)dbManager {
    return [self.dbManagerAssembly dbManager];
}

删除此定义,然后修改customerDataManager以改为:

[initializer injectParameterWith:[self.dbManagerAssembly dbManager]];

一切都会好的。