我已经阅读过Evans,Nilsson和McCarthy等人,并了解域驱动设计背后的概念和推理;但是,我发现很难将所有这些放在一个真实世界的应用程序中。缺乏完整的例子让我摸不着头脑。我发现了很多框架和简单的例子,但到目前为止还没有真正演示如何在DDD之后构建真正的业务应用程序。
以典型的订单管理系统为例,以订单取消为例。在我的设计中,我可以看到带有CancelOrder方法的OrderCancellationService,该方法接受订单#和作为参数的原因。然后它必须执行以下“步骤”:
当然,所有这些都应该在事务中发生,并且不允许任何操作独立发生。我的意思是,如果我取消订单,我必须还原信用卡交易,我无法取消而不执行此步骤。这个,imo,建议更好的封装,但我不想依赖于我的域对象(Order)中的CreditCardService,所以看起来这是域服务的责任。
我正在寻找有人向我展示代码示例如何/应该“组装”。代码背后的思考过程将有助于我为自己连接所有的点。 THX!
答案 0 :(得分:2)
您的域名服务可能如下所示。请注意,我们希望在实体中保留尽可能多的逻辑,从而保持域服务的精简。另请注意,没有直接依赖信用卡或审计员实施(DIP)。我们只依赖于域代码中定义的接口。稍后可以在应用程序层中注入实现。应用程序层还负责按编号查找订单,更重要的是,在事务中包装“取消”调用(回滚异常)。
class OrderCancellationService {
private readonly ICreditCardGateway _creditCardGateway;
private readonly IAuditor _auditor;
public OrderCancellationService(
ICreditCardGateway creditCardGateway,
IAuditor auditor) {
if (creditCardGateway == null) {
throw new ArgumentNullException("creditCardGateway");
}
if (auditor == null) {
throw new ArgumentNullException("auditor");
}
_creditCardGateway = creditCardGateway;
_auditor = auditor;
}
public void Cancel(Order order) {
if (order == null) {
throw new ArgumentNullException("order");
}
// get current user through Ambient Context:
// http://blogs.msdn.com/b/ploeh/archive/2007/07/23/ambientcontext.aspx
if (!CurrentUser.CanCancelOrders()) {
throw new InvalidOperationException(
"Not enough permissions to cancel order. Use 'CanCancelOrders' to check.");
}
// try to keep as much domain logic in entities as possible
if(!order.CanBeCancelled()) {
throw new ArgumentException(
"Order can not be cancelled. Use 'CanBeCancelled' to check.");
}
order.Cancel();
// this can throw GatewayException that would be caught by the
// 'Cancel' caller and rollback the transaction
_creditCardGateway.RevertChargesFor(order);
_auditor.AuditCancellationFor(order);
}
}
答案 1 :(得分:2)
略有不同:
//UI
public class OrderController
{
private readonly IApplicationService _applicationService;
[HttpPost]
public ActionResult CancelOrder(CancelOrderViewModel viewModel)
{
_applicationService.CancelOrder(new CancelOrderCommand
{
OrderId = viewModel.OrderId,
UserChangedTheirMind = viewModel.UserChangedTheirMind,
UserFoundItemCheaperElsewhere = viewModel.UserFoundItemCheaperElsewhere
});
return RedirectToAction("CancelledSucessfully");
}
}
//App Service
public class ApplicationService : IApplicationService
{
private readonly IOrderRepository _orderRepository;
private readonly IPaymentGateway _paymentGateway;
//provided by DI
public ApplicationService(IOrderRepository orderRepository, IPaymentGateway paymentGateway)
{
_orderRepository = orderRepository;
_paymentGateway = paymentGateway;
}
[RequiredPermission(PermissionNames.CancelOrder)]
public void CancelOrder(CancelOrderCommand command)
{
using (IUnitOfWork unitOfWork = UnitOfWorkFactory.Create())
{
Order order = _orderRepository.GetById(command.OrderId);
if (!order.CanBeCancelled())
throw new InvalidOperationException("The order cannot be cancelled");
if (command.UserChangedTheirMind)
order.Cancel(CancellationReason.UserChangeTheirMind);
if (command.UserFoundItemCheaperElsewhere)
order.Cancel(CancellationReason.UserFoundItemCheaperElsewhere);
_orderRepository.Save(order);
_paymentGateway.RevertCharges(order.PaymentAuthorisationCode, order.Amount);
}
}
}
注意: