领域模型保存

发布于 2024-07-15 13:18:08 字数 419 浏览 3 评论 0原文

最近读完 Eric Evans 领域驱动设计(信息非常丰富,非常有趣),但是完成本书后的第一个主要项目,遇到了如何处理领域模型保存的问题?

我很欣赏服务/存储库的使用以及它们如何帮助模型,但它们将如何处理模型保存?

我之前的域模型保存将遵循以下方法调用结构;

父类.保存 {

ParentClassDB.Save

ChildObject1.Save

       ChildObject1DB.Save

ChildObject2.Save

       ChildObject2DB.Save  
  etc etc 

}

该服务是否控制对子对象调用适当的保存例程?

Recently finished reading Eric Evans Domain Driven Design (very informative, very enjoyable) however come to first major project since completing the book and got the issue how to handle the domain model save?

I appreciate the use of services / repositories and how they can aid the model but how would they handle the model save?

My previous domain model saves would follow the following method call structure;

ParentClass.Save
{

ParentClassDB.Save

ChildObject1.Save

       ChildObject1DB.Save

ChildObject2.Save

       ChildObject2DB.Save  
  etc etc 

}

Does the service take control of calling the appropiate save routines on the child objects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深白境迁sunset 2024-07-22 13:18:09

您使用聚合根吗? 他们有责任拯救他们的实体。

如果正在进行大量修改,一种方法可能是将逻辑封装在域服务中,并让该服务通过聚合根处理保存。

Do you use Aggregate roots? They are responsilbe for saving their entities.

If there is lots of modifications going on one way could be to encapsulate that logic in a domain service and have the service handle the saves through the Aggregate roots.

自此以后,行同陌路 2024-07-22 13:18:08

在 DDD 中,域实体不关心其持久性。 理想情况下,您不应该有 Save() 方法。

您的存储库将有这样一个 Save 方法,该方法在参数中接受一个实体实例:

public class PersonRepository
{
   //...

   public void Save(Person person)
   {
      this.dataContext.Save(person);
   }

   //...
}

根据您的堆栈,ORM 将处理对象保存,或者您将通过构造一个查询来保存人员来结束,或者您将使用实例值等填充存储过程参数。

如果我正确理解您所做的事情,则可能有必要将您放入域实体中的持久性逻辑移到其他地方,可能在存储库中,或者在由你的存储库。

In DDD, the domain entity doesn't take care of its persistence. You should not have a Save() method on it, ideally.

Your repository will have such a Save method, that takes an entity instance in parameter:

public class PersonRepository
{
   //...

   public void Save(Person person)
   {
      this.dataContext.Save(person);
   }

   //...
}

Depending on your stack, an ORM will handle the object saving, or you will end by constructing a query to save the person, or you will populate stored procedure parameters with from your instance values, etc.

If I understood correctly what you did, it may be necessary to move the persistence logic you put in the domain entities elsewhere, probably in repositories, or in the DAL used by your repositories.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文