Product Definitions & Fields

Creating Product Definitions and their related definition fields is the most frequent use for definitions. The taxonomy of products differs from implementation to implementation, and below are a few examples of how they can be created via code.

The code in these examples can and should be customized for individual use cases.

Product Definition Fields

Start by creating as many Product Definition Fields as needed for your Product Definition. We start with the fields because when creating the definition, we can then immediately add the fields before we save anything to the database.

private ProductDefinitionFieldEntity CreateProductDefinitionField(
    DataTypeEntity dataType,
    string name,
    bool isMultilingual,
    bool isVariantProperty)
    return new ProductDefinitionFieldEntity
        Name = name,
        Deleted = false,
        Multilingual = isMultilingual,
        DisplayOnSite = true,
        RenderInEditor = true,
        IsVariantProperty = isVariantProperty,
        DataType = dataType

The method above can be re-used to create multiple Product Definition Fields by supplying:

  • dataType - denoting the field should be displayed as, for example, a short text or a number field.

  • name - the name of the field that will appear on the product editor.

  • isMultilingual - if true, the field will be able to have differentiated values for each configured language.

  • isVariantProperty - if true, the field will be available only on variants; otherwise, it will be available only on products.

Product Definition

Continue with creating a Product Definition.

private ProductDefinitionEntity CreateProductDefinition(
string name, 
string description)
    return new ProductDefinitionEntity
        Name = name,
        Description = description,
        Deleted = false
  • name - Name of the definition, denoting the type of product.

  • description - A description of what the definition is for.

Create & Save

The below example ties it all together. This is done within the context of a HostedService, which is covered in Bootstrapping data on startup.

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        await using var asyncScope = _serviceProvider.CreateAsyncScope();
        var dbContext = asyncScope.ServiceProvider.GetRequiredService<UcommerceDbContext>();
        var exists = dbContext.Set<ProductDefinitionEntity>()
            .Any(x => x.Name == "Shirt");
        if (exists)
        var shortTextDataType = dbContext
            .First(x => x.DefinitionName == "ShortText");
        var shirtDefinition = CreateProductDefinition("Shirt", "Definition for Shirt type products.");
        shirtDefinition.ProductDefinitionFields = new List<ProductDefinitionFieldEntity>()
            CreateProductDefinitionField(shortTextDataType, "Color", true, true),
            CreateProductDefinitionField(shortTextDataType, "Code", false, false)
        await dbContext.SaveChangesAsync(stoppingToken);

Note the early return if the Definition already exists. Since this code runs on each startup, there must be a check so it only gets executed when necessary. In this case, if the definition already exists.

  • Create a scope within which to resolve a UcommerceDbContext.

  • Implement a check to decide if the bootstrapping should run on the database.

  • Find the right data type (in this example, we use ShortText for both fields).

  • Create the Product Definition using our helper method.

  • Create and add Product Definition Fields to the definition (in this example, we create two, one multilingual variant property for color and one non-multilingual product property for barcode)

  • Add the definition to the context and save changes to the database.

Once the application starts, the new definition will be selectable for products in the backoffice.

pageBootstrapping data on startup

Last updated