【发布时间】:2015-07-03 21:50:02
【问题描述】:
我看过许多 DDD 帖子和书籍,其中实体类派生自某种形式的基类,该基类具有实体标识类型的通用参数:
public interface IEntity<out TKey>
{
TKey Id { get; }
}
public interface IComplexEntity<out TKey, in TEntity>
{
TKey Id { get; }
bool IsSameAs(TEntity entity);
}
//Object Definition
public class TaxPayer : IComplexEntity<string, User>
{
...
}
在 Vernon 的实施领域驱动设计中,创建了特定类型以用作身份:
public class TaxPayerIdentity : Identity
{
public TaxPayerIdentity() { }
public TaxPayerIdentity(string id)
: base(id)
{
}
}
最近,我一直致力于将事件总线上的事件传递给外部侦听器。我遇到的“问题”是我需要一个通用的消息格式来发送事件信封:
public EventEnvelope
{
long EventStoreSequence; // from the event store
bool IsReplay; // if event store is replaying from position 0 of stream
object EventBeingSent; // this is the actual event, i.e. class AddressChanged { string Old; string New; DateTime On; }
object IdentityOfSender; // this is the identity of the entity who raised the event
}
IdentityOfSender 上方是一个对象,但实际值为 string、int、Guid 等,具体取决于对象的身份类型。
我的问题是为什么不简单地使用字符串作为标识?毕竟,Guids、整数、名称、数字都可以表示为字符串,并且它们很容易与通用格式的字符串进行比较——这不仅会使 EventEnvelope 更容易使用字符串作为通用格式,而且会使实体更容易无需基类或特殊类型即可处理?
所以综上所述,为什么人们不推荐使用字符串作为标识的通用格式(或者我没见过),而是谈论标识的基类和泛型类型?
【问题讨论】:
标签: architecture domain-driven-design software-design