【发布时间】:2016-11-15 07:06:13
【问题描述】:
我有一个发票聚合根,在某些时候可以发送到会计外部 Web 服务,并通过保留从该服务获得的一些 ID/号码来标记为已发送。
在 DDD 中正确的做法是什么?
这是我的想法:
第一种方法:
有一个带有SendToAccounting功能的发票AggregateRoot,并注入域服务/接口,它将发票发送到会计,并在会计软件中检索一些“id/code”,并设置AccountingSoftwareId属性
Invoice.SendToAccounting(IInvoiceDomain service)
{
var accountingSoftwareID = service.getAccountingSoftwareId(this);
this.AccountingSoftwareId = accountingSoftwareId;
}
///Implementation in the application service
var invoice = _invoiceRepository.GetInvoiceById(id);
invoice.SendToAccounting(someDomainService);
_invoiceRepository.Update(invoice);
_unitOfWork.Save();
第二种方法:
与第一种方法类似,但域服务应该负责这样的持久化:
var invoice = _invoiceRepository.GetInvoiceById(id);
///unit of work save will be called inside this function
invoice.SendToAccounting(someDomainService);
第三种方法:
域服务将完全负责封装此行为
///Code inside domain service
public void SendInvoiceToAccounting(int invoiceId)
{
var invoice = _invoiceRepository.GetInvoiceById(invoiceId);
string invoiceAccountingId = _accountingService.GetAccountingSoftwareId(invoice);
invoice.SetAsSentToAccounting(invoiceAccountingId);
_invoiceRepository.Update(invoice);
_unitOfWork.Save();
}
【问题讨论】:
-
我不清楚发送给会计的过程涉及什么 - 这是什么?
-
调用外部服务失败时如何处理?
-
发票汇总中使用的accountingId是什么?
标签: domain-driven-design ddd-service