Identity
The Identity<> value object provides generic functionality to create
and validate the IDs of aggregate roots. It is basically a wrapper
around a Guid.
Let's say we want to create a new identity named TestId. We could do
it like this:
1 2 3 4 5 6 7 | |
- The identity follows the form
{classname without "Id"}-{guid}e.g.test-c93fdb8c-5c9a-4134-bbcd-87c0644ca34ffor the aboveTestIdexample. -
The internal
Guidcan be generated using one of the following methods/properties. Note that you can access theGuidfactories directly by accessing the static methods on theGuidFactoriesclass:New: Uses the standardGuid.NewGuid().NewDeterministic(...): Creates a name-basedGuidusing the algorithm from RFC 4122 ยง4.3, which allows identities to be generated based on known data, (e.g., a user e-mail). It always returns the same identity for the same arguments.NewComb(): Creates a sequentialGuidthat can be used to avoid database fragmentation.
-
A
stringcan be tested to see if it's a valid identity using the staticbool IsValid(string)method. - Any validation errors can be gathered using the static
IEnumerable<string> Validate(string)method.
Attention
It's very important to name the constructor argument value
as it is significant when the identity type is deserialized.
Here are some examples of how we can use our newly created TestId:
1 2 | |
1 2 3 4 5 | |
1 2 3 | |
Tip
Be sure to read the section about value objects as the Identity<> is basically a value object.