的我已经看到了一些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>
{
...
}
在弗农的实施领域驱动设计,特定类型的使用创建为身份:
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更易于使用字符串作为通用格式,还会使实体更容易无需基类或特殊类型的句柄?
总之,为什么人们不推荐使用字符串作为通用格式(或者我没有见过),而是谈论基类和通用类型的身份?