gpt4 book ai didi

ios - 在其他装配中重复使用装配

转载 作者:行者123 更新时间:2023-12-01 19:58:27 24 4
gpt4 key购买 nike

我正在尝试将台风整合到我的项目中。

我有三个类,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];

我的问题是...如何正确重用程序集并避免异常?

最佳答案

台风允许您为系统中的关键体系结构参与者创建逻辑分组。此功能称为Assembly Modularization。定义的所有组件都放入一个TyphoonComponentFactory-包含用于发出已构建实例的配方。

assembly modules

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

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

  • 在代码中,您定义了:
    - (DBManager *)dbManager {
    return [self.dbManagerAssembly dbManager];
    }

    删除此定义,然后编辑 customerDataManager使其具有:
    [initializer injectParameterWith:[self.dbManagerAssembly dbManager]];

    一切都会好起来。

    关于ios - 在其他装配中重复使用装配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41115671/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com