这是不可能的域对象猜测任何用户的需求。域对象的职责只是产生事件来说明发生了什么。通过@MikeSW在此answer
这里是我的策略:
一)发布与查询组件的帮助基础领域的活动。
例如,我们正在一间酒店评论的应用程序。每个评论只有在被管理员批准后才能被其他客户查看。
public class CommentApprovedEvent {
private String commentId;
}
而事件处理程序更新评论查询数据的状态。到目前为止还不错。有时候以后还会有一些进一步的要求,比如当委员会获得批准时,最新批准的评论内容应该被视为酒店的“推荐”评论。
我们在评论中含有酒店的具体信息,但是这一次,我们选择不将它们添加到事件中。相反,我们使用的查询在事件处理程序检索它:
公共类HotelEventHandler {
public void on(CommentApprovedEvent event) {
CommentDetailDto comment = commentDetailQueryService.
findBy(event.getCommentId());
comment.getHotelId();
comment.getContent();
//update hotel's query data
}
}
有时,它甚至不可能将所有相关数据添加到事件。例如,有时候会有新的要求出现:当评论被批准时,评论者应该重新安装一些信用。但是我们在评论中没有评论者的完整个人资料。所以我们再次选择查询。
b)将大事件拆分成更小的事件。 在这种情况下,我们可以添加新事件而不是新属性。考虑DDD样品中交付的情况下,交付是在货物领域至极的重要价值目标显示了一个给定的货物的许多方面:
/**
* The actual transportation of the cargo, as opposed to
* the customer requirement (RouteSpecification) and the plan (Itinerary).
*
*/
public class Delivery {//value object
private TransportStatus transportStatus;
private Location lastKnownLocation;
private Voyage currentVoyage;
private boolean misdirected;
private Date eta;
private HandlingActivity nextExpectedActivity;
private boolean isUnloadedAtDestination;
private RoutingStatus routingStatus;
private Date calculatedAt;
private HandlingEvent lastEvent;
.....rich behavior omitted
}
交付指示货物的当前状态,则重新计算一次新货物处理事件注册或者路由规格变更:
//non-cqrs style of cargo
public void specifyNewRoute(final RouteSpecification routeSpecification) {
this.routeSpecification = routeSpecification;
// Handling consistency within the Cargo aggregate synchronously
this.delivery = delivery.updateOnRouting(this.routeSpecification, this.itinerary);
}
它来到我的脑海,我需要一个CargoDeliveryUpdatedEvent在第一,如:
//cqrs style of cargo
public void deriveDeliveryProgress(final HandlingHistory handlingHistory) {
apply(new CargoDeliveryUpdatedEvent(
this.trackingId, delivery.derivedFrom(routeSpecification(),
itinerary(), handlingHistory);
}
class CargoDeliveryUpdatedEvent {
private String trackingId;
private ..... //same fields in Delivery?
private ..... //add more when requirements evolves?
}
但最后我发现我可以用更小的事件,它可能会暴露意图更好,如:
//cqrs style of cargo
public void deriveDeliveryProgress(final HandlingHistory handlingHistory) {
final Delivery delivery = Delivery.derivedFrom(
routeSpecification(), itinerary(), handlingHistory);
apply(new CargoRoutingStatusRecalculatedEvent(this.trackingId,
delivery.routingStatus());
apply(new CargoTransportStatusRecalculatedEvent(this.trackingId,
delivery.routingStatus());
....sends events telling other aspects of the cargo
}
class CargoRoutingStatusRecalculatedEvent{
private String trackingId;
private String routingStatus;
}
class CargoTransportStatusRecalculatedEvent{
private String trackingId;
private String transportStatus;
}
希望这些帮助。干杯。
你的活动有哪些字段?你经常改变哪些领域? – 2013-02-14 09:55:57
我正在投票关闭这个问题作为题外话,因为[项目管理现在在堆栈溢出主题](//meta.stackoverflow.com/questions/343829/is-stack-overflow-an-closed-website -to-ASK-有关项目管理,问题/ 343841#343841)。请在[SoftwareEngineering.SE](// softwareengineering.stackexchange.com/)和[ProjectManagement.SE](// pm.stackexchange.com/)上提出这些问题。 (您也可以通过主持人干预来标记此问题是否已迁移。) – robinCTS 2017-10-28 02:51:41