简体   繁体   English

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

[英]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: 因此:

  • Each component in the entire set of assemblies must have a unique name. 整个程序集中的每个组件必须具有唯一的名称。

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.

相关问题 保存样式化元素以在其他视图控制器中重用 - Save styled element to reuse in other view controllers 如何在其他项目中重用Swift代码? - How to reuse Swift code in other projects? UITableView imageView重用其他单元格的图像 - UITableView imageView reuse other cell's image 加载程序集时出现异常:System.IO.FileNotFoundException:无法加载程序集“System.ValueTuple,版本 = 4.0.2.0, - Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly 'System.ValueTuple, Version=4.0.2.0, 正确管理类/类别库,以供其他git团队开发人员重用 - Properly manage library of classes / categories for reuse by other git team developers 我们可以在Swift上重用struct吗? 还是还有其他方法? - Can we reuse struct on Swift? Or is there any other way? 如何在不重叠表视图中的其他单元格的情况下重用表视图单元格 - How to reuse a table view cell without overlapping other cells in tableview ios swift 如何停止重用单元格模仿其他单元格的样式更改? - How can I stop reuse cells from imitating style changes of other cells? 在 SwiftUI 中创建自定义导航栏,因此其他 SwiftUI 视图应重用相同的导航栏 - Create a custom Navigation Bar in SwiftUI So other SwiftUI views should reuse same Nav Bar Swinject:迁移到程序集 - Swinject: migrating to assemblies
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM