Entity Identity - use of strings instead of type

空扰寡人 提交于 2019-12-07 16:24:28

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.

ex:

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

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

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.

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.

Practically there are almost no benefits for using types - you can easily use strings in any programming language.

But there is a more subtle advantage. String means string, just a sequence of characters. You have to know the context that some string actually means something else. It might be clear for you, but we write code not for ourselves, we write it for future readers. And it's very important to make it clear to everybody else - date is a date, ID is an ID, etc.

Also, it'll be much easier to extend / refactor some specific ID type later if needed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!