在哪里调用DDD中的repository.update? [英] Where to call repository.update in DDD?

查看:141
本文介绍了在哪里调用DDD中的repository.update?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个真实的场景,那就是完美的域模型设计.它是一个具有多个象限的字段,每个象限的状态不同.因此,我的总根是该字段.现在我有一个重要的问题:我想拥有一个敏锐的ignorat域模型,我认为这是有道理的.所以我应该在哪里调用存储库方法的更新?不是在域模型中,对吧?因此,当没有更改跟踪根代理的对象并且不应在实体中调用存储库时,汇总的根子实体应如何在数据库中更新?还是我误解了领域模型模式?

我的问题清楚了吗?:)先感谢您最好的月桂酸

解决方案

那么我应该在哪里调用存储库方法的更新?

在典型的DDD体系结构中,存储库通常由应用程序服务调用.应用程序服务是一个类,它用作 facade 封装您的域,并通过编排域对象,存储库和其他服务.

我对您的域不熟悉,但是假设有一个用例将 State 从一个 Quadrant 移到一个 Field 到另一个.如您所述, Field 是AR.因此,您将有一个 FieldApplicationService 引用一个 FieldRepository :

 公共类FieldApplicationService{只读FieldRepository fieldRepository;公共无效ShiftFieldState(int fieldId,字符串象限,字符串状态){//检索Field ARvar field = this.fieldRepository.Get(fieldId);如果(字段==空)抛出新的Exception();//调用Field AR上的行为.field.ShiftState(象限,状态);//提交更改.this.fieldRepository.Update(field);}} 

应用程序服务本身非常薄.它没有实现任何域逻辑;它仅编排并设置执行域逻辑(包括访问存储库)的阶段.所有与您的域相关的代码,例如表示层或服务,都将通过此应用程序服务调用域功能.

可以通过多种方式来实现存储库.可以使用诸如NHibernate之类的ORM,在这种情况下,内置了更改跟踪,通常的方法是提交所有更改,而不是调用显式更新.NHibernate提供了一个工作单元,并允许将多个实体的更改作为一个整体提交.

如您所述,在您的情况下,没有更改跟踪,因此需要显式调用update,这取决于存储库实现来处理.如果使用SQL Server作为数据库,则存储库中的 Update 方法可以简单地将 Field 的所有属性发送到存储过程,该存储过程将根据需要更新表.

I have a real scenario that is a perfect Domain Model design. It is a field that has multiple quadrants with different states on every quadrant. So my aggregate root is the field. Now i have one important question: I want to have a persitant ignorat domain model, which i think makes sense. so where should i call the update on the repository methods? not in the domain model, right? So how should the aggregate root child entities update in the database when there is no change tracking proxy of this objects and the repository should not be called in the entities? Or do i misunderstand the domain model pattern?

is my question clear? :) thank you in advance best laurin

解决方案

So where should i call the update on the repository methods?

In a stereotypical DDD architecture the repository is usually called by an application service. An application service is a class which serves as a facade encapsulating your domain and implements domain uses cases by orchestrating domain objects, repositories and other services.

I'm not familiar with your domain, but suppose there is a use case which shifts a State from one Quadrant in a Field to another. As you stated, the Field is the AR. So you'd have a FieldApplicationService referencing a FieldRepository:

public class FieldApplicationService
{
    readonly FieldRepository fieldRepository;    

    public void ShiftFieldState(int fieldId, string quadrant, string state)
    {
          // retrieve the Field AR
          var field = this.fieldRepository.Get(fieldId);
          if (field == null)
              throw new Exception();

          // invoke behavior on the Field AR.
          field.ShiftState(quadrant, state);

          // commit changes.
          this.fieldRepository.Update(field);
    }
}

The application service is itself very thin. It does not implement any domain logic; it only orchestrates and sets the stage for execution of domain logic which includes accessing the repository. All code dependant of your domain, such as the presentation layer or a service will invoke domain functionality through this application service.

The repository could be implemented in a variety of ways. It can be with an ORM such as NHibernate, in which case change tracking is built in and the usual approach is to commit all changes instead of calling an explicit update. NHibernate provides a Unit of Work as well allowing changes to multiple entities can be committed as one.

In your case, as you stated, there is no change tracking so an explicit call to update is needed and it is up to the repository implementation to handle this. If using SQL Server as the database, the Update method on the repository can simply send all properties of a Field to a stored procedure which will update the tables as needed.

这篇关于在哪里调用DDD中的repository.update?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆