[英]Reuse assembly in other assemblies
I'm trying to integrate typhoon into my project. 我正在尝试将台风整合到我的项目中。
I have three classes, DBManager, ProductDataManager, and CustomerDataManager. 我有三个类,DBManager,ProductDataManager和CustomerDataManager。 Both ProductDataManager and CustomerDataManager depend on DBManager for db operation.
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;
}
Now I create three assemblies for them. 现在,我为它们创建三个程序集。
@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];
}
Activate them in AppDelegate 在AppDelegate中激活它们
- (NSArray *)initialAssemblies
{
return @[[DBManagerAssembly class], [ProductDataManagerAssembly class], [CustomerDataManagerAssembly class]];
}
But on app startup, it throws an exception 但是在应用启动时,它会引发异常
[NSException raise:NSInvalidArgumentException format:@"Key '%@' is already registered.", _definition.key];
My question is... how to reuse assembly properly and avoid exception? 我的问题是...如何正确地重用程序集并避免异常?
Typhoon allows you to create logical groupings of the key architectural actors in your system. 台风允许您为系统中的关键体系结构参与者创建逻辑分组。 This feature is called Assembly Modularization .
此功能称为程序集模块化 。 All of the components defined go into a single
TyphoonComponentFactory
- which contains recipes for emitting built instances. 定义的所有组件都进入一个
TyphoonComponentFactory
其中包含用于发出已构建实例的配方。
Before activation the assembly returns definitions. 在激活之前,程序集返回定义。 At runtime you can use the assembly as a facade to return built instances.
在运行时,您可以将程序集用作外观以返回生成的实例。 It uses Objective-C message forwarding to resolve method calls to
[factory componentForKey:theMethodName]
. 它使用Objective-C消息转发来解析对
[factory componentForKey:theMethodName]
方法调用。 Therefore: 因此:
In your code, you've defined: 在代码中,您定义了:
- (DBManager *)dbManager {
return [self.dbManagerAssembly dbManager];
}
Remove this definition, and edit customerDataManager
to instead have: 删除此定义,然后编辑
customerDataManager
使其具有:
[initializer injectParameterWith:[self.dbManagerAssembly dbManager]];
All will be well. 一切都会好起来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.