【发布时间】:2011-09-29 13:35:35
【问题描述】:
这是围绕我了解 DDD 的系列文章的一部分 :)
接上一个问题,但不需要背景知识:System consuming WCF services from another system, when underlying databases have relationships
有一个文档系统和一个人力资源系统。人力资源系统需要保存一个文档以及与该文档相关的一些人力资源特定数据。
我的第一个想法是对文档系统的调用应该在人力资源系统的应用程序服务中(去掉多余的代码):
public class HRDocumentService
{
public void SaveDocument(string filename, string employee)
{
long documentLibraryId = _documentLibraryService.SaveDocument(filename);
HRDocument hrDocument = HRDocument.CreateDocument(documentLibraryId, employee);
_hrDocumentRepository.Save(hrDocument);
}
}
存储库是这样的:
public class HRDocumentRepository
{
public long Save(HRDocument hrDocument)
{
_session.Save(hrDocument);
}
}
但是 Jak Charlton 在 this article 中说:“存储库后面是什么?几乎任何你喜欢的东西。是的,你没听错。你可以有一个数据库,或者你可以有许多不同的数据库。你可以使用关系数据库“
所以现在我认为服务应该是这样的:
public class HRDocumentService
{
public void SaveDocument(string filename, string employee)
{
HRDocument hrDocument = HRDocument.CreateDocument(documentLibraryId, employee);
_hrDocumentRepository.Save(hrDocument);
}
}
并像这样调用存储库中的文档库服务:
public class HRDocumentRepository
{
public long Save(HRDocument hrDocument)
{
long documentLibraryId = _documentLibraryService.SaveDocument(filename);
hrDocument.DocumentLibraryId = documentLibraryId;
_session.Save(hrDocument);
}
}
这样,可以说,存储库仍然只负责持久性。
我是在正确的路线上还是在正确的路线上?
【问题讨论】:
标签: architecture service domain-driven-design repository-pattern ddd-repositories