实体标识 - 使用字符串而非类型 [英] Entity Identity - use of strings instead of type

查看:178
本文介绍了实体标识 - 使用字符串而非类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些DDD的帖子确实书籍,其中实体类是由某种形式的基类是对实体身份类型泛型参数派生的:

I have seen a number of DDD posts and indeed books where entity classes are derived from some form of base class that has a generic parameter for the Entity Identity type:

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>
{
    ...
}

在弗农的实施领域驱动设计,特定类型用作身份创建:

On Vernon's Implementing Domain Driven design, specific types are created for use as identities:

public class TaxPayerIdentity : Identity
{
   public TaxPayerIdentity() { }

    public TaxPayerIdentity(string id)
        : base(id)
    {
    }
}

最近,我一直在努力的事件总线外部监听器上进行通信的事件。该问题我是我需要一个通用的消息格式事件信封发送:

More recently I have been working on communicating events on an event bus to external listeners. The "issue" I have is I need a common message format for sending of the event envelope:

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 是一个对象,但实际价值将是字符串 INT 的Guid 等根据对象的身份类型。

Above the IdentityOfSender is an object, but the actual value would be string, int, Guid etc depending on the objects identity type.

我的问题是,为什么不能简单地用为标识字符串?毕竟,的GUID,整数,名称,数量都可以重新presented为字符串,他们是在一个共同的格式字符串容易比较 - 这不仅会使EventEnvelope一个字符串更容易为通用格式,它会使实体更容易,而不需要基类或特殊类型的处理?

My question is why not simply use a string for the identity? After all, Guids, integer, names, numbers can all be represented as strings and they are easily comparable as strings in a common format - this not only would make the EventEnvelope easier with a string as a common format, it would make Entities easier to handle without need for base classes or special types?

因此,在总结,为什么人们不建议使用的字符串进行身份为通用格式(或者我还没有看到它),而不是说说基类和泛型类型的身份?

推荐答案

由于在跨文化的字符集字符串比较是不容易的。可与字符串进行比较,最近的数据类型是GUID。但是,即使GUID可以有不同的字符串重新presentation。

Because string comparison in cross-culture charset is not easy. The closest data type that can be compared with string is GUID. But even GUID can have different string representation.

例如:

{FD49D6AE-019C-4118-B7D1-A4DA54E4474F},
fd49d6ae-019c-4118-b7d1-a4da54e4474f,
FD49D6AE019C4118B7D1A4DA54E4474F

这三家公司均相同的GUID值,但不同的字符串比较。号码是著名的千位分隔符/小数点格式的区别。

All three have same GUID value but different string comparison. Number is famous for the thousand separator / decimal point format difference.

此外,我认为,对于身份的自定义类是提供明确的身份比较。例如:信用卡号码的有六位发行人识别号码(IIN)在第6位(适用的维基百科)。也就是说,让你在领域驱动设计更高的灵活性。是的,这设计prevent您使用我为人人的数据类型。

Moreover, I believe that the custom class for identity is to provide explicit identity comparison. Example: Credit card number's has a six-digit Issuer Identification Number (IIN) in the first six digit (wikipedia). That is, enable you higher flexibility in domain-driven design. And yes, that design prevent you from using "one for all" data type.

但是,您可以通过对象映射连接使用外部总线的code-DE code型虽然。这样,就可以避免打破了DDD,并在同一时间在整个外部总线提供相同的格式。

However you can use encode-decode model for external bus through object mapping though. That way you can avoid breaking the DDD and at the same time providing same format throughout external buses.

这一边,它是一个一般准则。而在软件工程的每一个准则都有优点和缺点。使用一个你找到最合适的,知道最好的,但保持它在你的设计是一致的。

That aside, it is a general guidelines. And every guidelines in software engineering has pros and cons. Use one you find most suitable and know the best, but keep it consistent across your design.

这篇关于实体标识 - 使用字符串而非类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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