【发布时间】:2012-11-11 17:34:35
【问题描述】:
服务层应该位于模型层之上。因此,模型不应该调用服务。
但是,我正面临一种情况,例如:
interface Component {
getResult();
}
class Number implements Component {
private value;
public getResult() {
return value;
}
}
class Addition implements Component {
private component1;
private component2;
public getResult() {
return component1->getResult() + component2->getResult();
}
}
class ConstantFromExternalSource implements Component {
private identifier;
public getResult() {
// call a service for fetching constant identified by identifier
}
}
(伪代码)
这里,我的模型需要通过服务(无论是否为 web 服务)访问外部数据源。
在这种情况下我应该怎么做? 可以在模型中调用服务吗?
如果您建议从模型中移走“getResult”方法并将其放入“ComponentService”中,我会不同意,因为这样我会失去 OOP 的所有优点(这里我的模型创建了一棵树,需要递归解决,所以OOP是最好的解决方案)。
【问题讨论】:
标签: oop service model domain-driven-design soa