4

I was reading this question recently: ID properties on Domain objects in DDD

The question talks about having an surrogate key in the infrastructure layer, which is a database ID. I use a Guid for this:

Guid id = Guid.NewId();

The answers then talk about having a natural key in the domain layer that identifies the entities. A great example of this in my mind is a bank account i.e. the Guid identifies the bank account in the database (and Data Model) and the account number and sort code identify the bank account in the domain layer i.e. there is no database id in the domain layer.

Say I have a product entity and I want to generate a product code in the domain layer. How would I do this? The question I have linked to talks about using algorithms. What algorithms are there?

w0051977
  • 7,139

2 Answers2

4

Lets stick to your example:

Say I have a product entity and I want to generate a product code in the domain layer. How would I do this?

First, you ask your domain experts about their requirements for a product code. They might come up with things like

  • must be printable on paper, using only numbers, or using alpha-numerics, but without using numbers and letters together which can be easily mixed up (like 0 and O, 1 and l)

  • must be bar-code "friendly"

  • must be unique for every product expected to be produced within the next 10 years by our company

  • must encode the product category in some part of the number

  • must encode the year of production

  • must not collide with all the already assigned 123456 product numbers from the past which were generated in an older system following a different logic

  • must prevent duplicates even when your system runs offline in a distributed fashion

So it will become your task to analyse these requirements, find a solution in form of an algorithm for this and implement the required code in the domain layer.

Or, if you are lucky, your domain experts tell you

  • we don't need something like a product code for our business, we never had one in the past, never missed it

In this case, don't invent a "natural key". Instead use the surrogate key exclusively whereever you need to implement modeled relationships.

Doc Brown
  • 218,378
0

You might want to read up on International Article Number (also known as European Article Number or EAN). EAN-13 is 13 digits. You might also want to read up on Global Trade Item Number (GTIN) and part number.

An invoice order number commonly includes the data and/or the customer number. A ticket number may include the date. A product code may include series/version/revision and attributes such as size and color.

So you can form your own naming convention. You can look up naming conventions that other companies use, such as how Intel and AMD name their processors (generation, low/mid/high-end), or how Nvidia and AMD name their graphics cards, or how phone companies name their phones. How companies name their shoes (men/women, indoor/outdoor, walking/running, size, color).

You can create a function which generates such as a name from its input parameters.

/// <summary>
/// Generate a product code from the specified parameters.
/// </summary>
/// <param name="series">The series that the product belongs to.</param>
/// <param name="size">The size of the product.</param>
/// <param name="color">The primary color of the product.</param>
/// <returns>A product code.</returns>
/// <remarks>Adheres to our naming convention.</remarks>
private string GenerateProductCode(string series, Size size, Color color)
{
    return $"{series}{size}{color}";
}

Then you call it:

var productCode = GenerateProductCode("300", Size.Small, Color.Green); // -> 300SG
Fred
  • 509