I've been told that in domain-driven design, an identifier for an entity could be a custom value object, i.e. something other than
int, etc. Can this really be advisable in a distributed system?
I will invent an situation analogous to the one I am currently facing.
Say I have a distributed system in which a central concept is an egg. The system allows you to order eggs and see spending reports and inventory-centric data such as quantity on hand, usage, valuation and what have you. There area variety of services backing these behaviors. And say there is also another app which allows you to compose recipes that link to a particular egg type.
Now egg type is broken down by the species—ostrich, goose, duck, chicken, quail. This is fine and dandy because it means that users don't end up with ostrich eggs when they wanted quail eggs and whatnot. However, we've been getting complaints because jumbo chicken eggs are not even close to equivalent to small ones. The price is different, and they really aren't substitutable in recipes. And here we thought we were doing users a favor by not overwhelming them with too many options.
Currently each of the services (say,
RecipeTracker, or whatever) are identifying egg types with an industry-standard integer representation the species (let's call it
speciesCode). We realize we've goofed up because this change could effect every service.
There are two basic proposed solutions:
- Use a predefined identifier type like
eggTypeIDthroughout all the services, but make
EggTypeDefinerthe only service that knows that this maps to a
eggSizeCode(and potentially to an
isOrganicflag in the future, or whatever).
- Use an
EggTypeIDvalue object which is a combination of
eggSizeCodein every service.
I've proposed the first solution because I'm hoping it better encapsulates the definition of what an egg type is in the
EggTypeDefiner and will be more resilient to changes, say if some people now want to differentiate eggs by whether or not they are "organic".
The second solution is being suggested by some people who understand DDD better than I do in the hopes that less enrichment and lookup will be necessary that way, with the justification that in DDD using a value object as an ID is fine. Also, they are saying that
EggTypeDefiner is not a domain and
EggType is not an entity and as such should not have a
Guid for an ID.
Can it be a good idea to use a custom value object as an identifier in a distributed system (solution #2)?