在DDD身份冲突库 [英] Identity conflict in DDD with Repository

查看:119
本文介绍了在DDD身份冲突库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面一个数据库第一个在一个示例应用程序的方法。通过T4模板生成的POCO类,这将有身份的属性,它从数据库中随身携带。对于一些类,我的领域,在键入的ID可以是整型,字符串和放大器作为实体使用; GUID

我不明白一个问题,IDS是字符串或GUID,我可以在域产生并传递到存储库。在int情况下,我也能做到这一点,但我想利用SQL数据库的自动增量选项

优势

我的问题是:

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

  2. 如何身份的同步可以在资料库和域之间完成?例如,我可以创造的1000的客户域中的ID,并要求该库保存。当存储库将其保存,新的身份可能是2000(自动增量选项设置新的身份)。现在,这两个实体是不同的,如果域中的客户实体(假设它被缓存为未来的操作)用于与其他实体例如,它可能会导致域中的问题。在具有1000的ID域的高速缓存的实体(客户)将被保存为的SQL数据存储的新实体。该库不知道客户是否缓存或新的。

解决方案
  

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

实体的身份可以通过直接或通过客户端请求的域进行分配。这种类型的身份通常与的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.

这篇关于在DDD身份冲突库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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