DDD:被持久化之前,实体标识 [英] DDD: Entity identity before being persisted

查看:246
本文介绍了DDD:被持久化之前,实体标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在领域驱动设计,一个实体的定义特征之一是,它有一个身份



问题:



我不能够提供一个独特的身份在实例创建实体。一旦实体坚持这个身份仅由库提供的(这个值是从底层数据库中提供)。



我不能开始使用在这一点上的Guid 值。现有的数据存储与 INT 主键值,我不能生成实例化一个独特的int值。



我的解决方案:




  • 每个实体都有一个标识值

  • 的身份只设置一次坚持一个真实身份(数据库提供)

  • 的标识设置为默认实例化时的持久性之前

  • 如果该身份默认情况下,实体具有可比性,参照

  • 如果身份不违约,实体是通过标识值可比



码(所有实体的抽象基类):

 公共抽象类实体LT ; IdType> 
{
私人只读IdType UNIQUEID;

公共IdType标识
{
得到
{
返回UNIQUEID;
}
}

公共实体()
{
UNIQUEID =默认(IdType);
}

公共实体(IdType ID)
{
如果(的Object.Equals(ID,默认值(IdType)))
{
抛出新的ArgumentException(域模型的ID不能默认值);
}

UNIQUEID = ID;
}

公众覆盖布尔等于(obj对象)
{
如果(uniqueId.Equals(默认值(IdType)))
{
VAR实体= OBJ作为实体LT; IdType取代;

如果(实体!= NULL)
{
返回uniqueId.Equals(entity.Id);
}
}

返回base.Equals(OBJ);
}

公共覆盖INT的GetHashCode()
{
返回uniqueId.GetHashCode();
}
}



问:




  • 您会认为这是一个很好的选择,对实例的创建产生GUID值?

  • 是否有在那里更好的方案来解决这个问题?


解决方案

我不能够提供一个独特的身份在实例创建实体。一旦实体被持久保存此标识仅由存放库提供的(此值是从底层数据库提供)。




多少地方你有没有在其中创建同一类型的实体的名单,你必须使用默认的id一个以上的实体?




你可以考虑这是一个很好的选择,对实例的创建产生GUID值?




如果你不使用任何ORM你的做法是不够好。特别是,当同一性地图的实施方案和工作的单位是你的respomsibility。但是,你有固定的等于(obj对象)而已。 的GetHashCode()方法不检查 uniqueId.Equals(默认值(IdType))



我建议你看看任何开源基础设施的样板之类的夏普架构并检查他们的实施所有域实体基类的。



我用写等于()域的实体,但是当涉及到​​ORM的使用也可以是多余的。如果你使用它提供身份地图和的工作图案单位开箱即用,你可以依靠他们。


In Domain Driven Design, one of the defining characteristic of an Entity is that it has an identity.

Problem:

I am not able to provide a unique identity to Entities on instance creation. This identity is only provided by the repository once the entity is persisted (this value is provided from the underlying database).

I cannot begin to use Guid values at this point. The existing data is stored with int primary key values and I cannot generate a unique int on instantiation.

My solution:

  • Each Entity has an identity value
  • The identity is only set to a real identity once persisted (provided by the database)
  • The identity is set to default when instantiated before persistence
  • If the identity is default, entities are comparable through reference
  • If the identity is not default, entities are comparable through identity values

Code (the abstract base class for all entities):

public abstract class Entity<IdType>
{
    private readonly IdType uniqueId;

    public IdType Id
    {
        get 
        { 
            return uniqueId; 
        }
    }

    public Entity()
    {
        uniqueId = default(IdType);
    }

    public Entity(IdType id)
    {
        if (object.Equals(id, default(IdType)))
        {
            throw new ArgumentException("The Id of a Domain Model cannot be the default value");
        }

        uniqueId = id;
    }

    public override bool Equals(object obj)
    {
        if (uniqueId.Equals(default(IdType)))
        { 
            var entity = obj as Entity<IdType>;

            if (entity != null)
            {
                return uniqueId.Equals(entity.Id);
            }
        }

        return base.Equals(obj);
    }

    public override int GetHashCode()
    {
        return uniqueId.GetHashCode();
    }
}

Question:

  • Would you consider this to be a good alternative to generating Guid values on instance creation?
  • Are there better solutions out there for this problem?

解决方案

I am not able to provide a unique identity to Entities on instance creation. This identity is only provided by the repository once the entity is persisted (this value is provided from the underlying database).

How many places do you have where you create a list of entities of the same type and you have more then one entity with default id?

Would you consider this to be a good alternative to generating Guid values on instance creation?

If you do not use any ORM your approach is good enough. Especially, when an implementation of identity map and unit of work is your respomsibility. But you have fixed Equals(object obj) only. GetHashCode() method does not check if uniqueId.Equals(default(IdType)).

I suggest you look into any open-source "Infrastructure Boilerplate" like Sharp-Architecture and check their implementation of the base class for all domain entities.

I am used to writing custom implementations of Equals() for domain entities, but it can be superfluous when it comes to usage of ORM. If you use any ORM it provides implementations of identity map and unit of work patterns out of the box and you can rely on them.

这篇关于DDD:被持久化之前,实体标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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