在其他程序集中重用程序集
Reuse assembly in other assemblies
我正在尝试将台风集成到我的项目中。
我有三个 类,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];
我的问题是...如何正确重用程序集并避免异常?
Typhoon 允许您在系统中创建关键架构参与者的逻辑分组。此功能称为 Assembly Modularization。定义的所有组件都进入一个 TyphoonComponentFactory
- 其中包含用于发出构建实例的配方。
激活程序集 return 的定义之前。在运行时,您可以将程序集用作 return 构建实例的外观。它使用 Objective-C 消息转发来解析对 [factory componentForKey:theMethodName]
的方法调用。因此:
- 整个组件集中的每个组件都必须具有唯一的名称。
在您的代码中,您定义了:
- (DBManager *)dbManager {
return [self.dbManagerAssembly dbManager];
}
删除此定义,并编辑 customerDataManager
以改为:
[initializer injectParameterWith:[self.dbManagerAssembly dbManager]];
一切都会好的。
我正在尝试将台风集成到我的项目中。
我有三个 类,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];
我的问题是...如何正确重用程序集并避免异常?
Typhoon 允许您在系统中创建关键架构参与者的逻辑分组。此功能称为 Assembly Modularization。定义的所有组件都进入一个 TyphoonComponentFactory
- 其中包含用于发出构建实例的配方。
激活程序集 return 的定义之前。在运行时,您可以将程序集用作 return 构建实例的外观。它使用 Objective-C 消息转发来解析对 [factory componentForKey:theMethodName]
的方法调用。因此:
- 整个组件集中的每个组件都必须具有唯一的名称。
在您的代码中,您定义了:
- (DBManager *)dbManager {
return [self.dbManagerAssembly dbManager];
}
删除此定义,并编辑 customerDataManager
以改为:
[initializer injectParameterWith:[self.dbManagerAssembly dbManager]];
一切都会好的。