且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在DDD身份冲突库

更新时间:2023-02-13 11:23:47

  

谁的责任是产生身份的实体。是吗   域或资料库?

实体的身份可以通过直接或通过客户端请求的域进行分配。这种类型的身份通常与的Guid 或其它通用唯一值生成。身份也可以通过存储库,以反映数据库生成的标识分配,如增量标识列。

  

如何身份的同步可以在存储库之间进行   域名?

当一个实体的身份是从生成的数据库标识列来源,域不应该分配自己的身份。有一个整体标识的临时实体最初将有标识值0。当临时实体被持久化,资源库将分配的标识值。在这一点上,正如你所指出,有可能会因为标识值的新持久化实体实例缓存的实例一些问题发生了变化。有几种方法来解决这些问题,但我会尽量避免在高速缓存(或其他地方)的第一个地方引用短暂的实体。这可以通过确保一个实体未在许多地方引用,直到它变得持久可以更容易地完成。如果最终引用在不同的地方短暂实体实例,可以确保其.NET运行时的身份是相同的实例的生命周期。要做到这一点,你必须标识值检查之前进行比较,通过 object.ReferenceEquals 实体,你必须通过缓存 GetHash code 的对象生命周期的持续时间。

I'm following a database first approach in a sample app. The POCO classes are generated via t4 template and it will have the Identity properties that it carry from the database. For some classes which I use as entities in the domain, the type of the id can be int, string & Guid.

I don't see a problem with ids that are string or Guid which I can generate in the domain and pass that to the repository. In the case of int also I can do that, but I want to take advantage of the AUTOINCREMENT option of the SQL Database.

My questions are:

  1. Who's responsibility is to generate the Identity for the Entity. Is it the domain or the repository?

  2. How the syncing of identities can be done between the repositories and the domain? For example, I can create an id of 1000 for a Customer in the domain, and ask the Repository to save it. When the repository saves it, the new identity may be 2000 (the AUTOINCREMENT option sets the new identity). Now the two entities are different, and if the Customer entity in the domain (assume it is cached for future operations) is used with other entities like Department, it can cause problems in the domain. The cached entity (Customer) in the domain which has the id of 1000 will be saved as a new entity in the SQL datastore. The repository does not know whether the Customer is cached or new.

Who's responsibility is to generate the Identity for the Entity. Is it the domain or the repository?

The identity of an entity can be assigned by the domain either directly or via client request. This type of identity is usually generated with a Guid or another universally unique value. The identity can also be assigned by the repository to reflect a database generated identity such as an incremental identity column.

How the syncing of identities can be done between the repositories and the domain?

When an entity's identity is sourced from a database generated identity column, the domain should not assign its own identity. A transient entity with an integral identity will initially have identity value 0. When the transient entity is made persistent, the repository will assign the identity value. At that point, as you point out, there may some issues with cached instances of the newly persistent entity instance since the identity value has changed. There are a few ways to resolve these issues, but I try to avoid referencing transient entities in caches (or anywhere else) in the first places. This can be more easily done by making sure an entity isn't referenced in many places until it is made persistent. If you do end up referencing a transient entity instance in various places, you can make sure that its .NET runtime identity remains the same for the lifetime of the instance. To do this, you have to compare entities via object.ReferenceEquals before the identity value check and you have to cache the hash-code generated via GetHashCode for the duration of the object lifetime.