Getting started with a domain-managed project on an e-commerce site

with a difficult time determining how to model the expected behavior of the product.

Mainly, customer inventory is controlled by products and skus.

The product has many skus, but one sku takes into account several product attributes.

Let me give you an example.

Say I'm selling you a shirt. A shirt is a product with some product identifier. If it comes in small, medium, large, then each of these sizes will be associated with sku #.

easy enough so far, but if the shirt also comes in several colors, let it say red, yellow and green, then there will be nine crosses (red / small, red / medium, red / large, yellow / small, etc.) .

Add to this the challenge that attribute types may vary for different products. A shirt can have sizes and colors, a bag can have different styles or patterns of pens, and I will not know in advance, this is what the client should be able to enter using adhoc.

Any ideas on how to approach this in terms of DDD? I've been baking my noodles for several days now.

Thanks.

+4
source share
2 answers

First of all, you should consider each sku as an attribute of one product, and not combine them. If the product can have color and size, these are two different attributes, and not one (as in red / small, red / medium, etc.). Suppose a product has five attributes, each of which has 4 possible meanings. Then you will have 4^5=1024 skus. This is quickly becoming a nightmare for maintenance.

So, the first two objects in your domain model should be ProductDefinition and Attribute . The reason I choose ProductDefinition as a name rather than Product is because it is just a label for some type of product, such as a shirt. This is not a small yellow shirt.

Attributes can have possible values, so this is the third domain object: AttributeValue . The ratio between Attribute and AttributeValue is 1: n. An attribute has several values; a value belongs to only one attribute.

Note that AttributeValue contains all possible values ​​for the attribute, not the actual value for a single product. This actual value becomes the relationship between ProductDefinition , Attribute and AttributeValue : ProductAttributeValue . For an example of a shirt in a database model:

 ProductDefinition Attribute AttributeValue 1 | Shirt 1 | Color 1 | 1 | Red 2 | Size 2 | 1 | Yellow 3 | 1 | Green 4 | 2 | Small 5 | 2 | Medium 6 | 2 | Large 

Now we have modeled one product definition, two attributes and three attribute values ​​for each attribute. Suppose now we want to simulate three shirts: small red, small green and large yellow. This results in the following contents of ProductAttributeValue ( ProductId , ProductDefinitionId , AttributeId , AttributeValueId ):

 ProductAttributeValue 1 | 1 | 1 | 1 1 | 1 | 2 | 4 2 | 1 | 1 | 3 2 | 1 | 2 | 4 3 | 1 | 1 | 2 3 | 1 | 2 | 2 
+2
source

We made such a system.

  Productdefinition
   has Type (Shirt, Handbag)
   has many ProductFieldDefinition

 ProductFieldDefinition
   has Type (Color, size, pattern)

 Product
   has ProductDefinition
   has SKU
   has many ProductField
       has ProductFieldDefinition
       has value

The only thing I would change in our system: I would use a database of documents to store everything, but in our case the graph is really much deeper, each level had its own fields.

0
source

Source: https://habr.com/ru/post/1314646/


All Articles