0

我正在使用机器人腿,我有一堆 ServiceResponses 扩展了一个基类并依赖于 Parser,IParser。我需要连接特定于子类的解析器。这是一个例子:

ModuleConfigResponse 扩展了 SimpleServiceResponse 并实现了 IServiceResponse。

初始部分很容易在上下文中连接,这是一个示例:

injector.mapClass(IServiceResponse, ModuleConfigResponse);
injector.mapClass(IServiceResponse, SimpleServiceResponse, "roomconfig");
..etc

每个响应使用基类使用的解析器:

injector.mapValue(IParser, ModuleConfigParser, "moduleconfig");
injector.mapValue(IParser, RoomConfigParser, "roomconfig");

问题是如何将这些联系在一起。基类可以有:

[Inject]
public var parser : IParser

但我无法提前定义类型。我想知道在上下文中是否有一种很好的接线方式。目前,我决定通过在 ResponseFactory 中实例化响应来连接它,以便我在构造函数中手动传递解析器。

injector.mapValue(IParser, ModuleConfigParser, "moduleconfig");

4

2 回答 2

1

我意识到并非所有东西都可以在上下文中映射,RL 让我陷入了这种思维方式。但是我已经意识到,映射一个工厂来生成这些具有非常特定依赖关系的对象要比使用标记接口或字符串的代码库更小要好得多:)

于 2012-05-24T14:28:02.537 回答
0

一种解决方案是在您的基类中包含以下内容:

protected var _parser : IParser

然后例如在 ModuleConfigResponse

[Inject(name='moduleconfig')]
public function set parser( value : IParser ) : void{
    _parser = value;
}

但是 TBH,强烈建议不要使用命名注入,您不妨使用标记接口:

public interface IModuleConfigParser extends IParser{}

基类保持不变,但 ModuleConfigResponse 然后将使用:

[Inject]
public function set parser( value : IModuleConfigParser ) : void{
    _parser = value;
}
于 2012-03-02T12:33:42.763 回答