Repository design pattern

Each Connect processor that writes data to Sitecore content is based on the Repository design pattern and has an associated configuration entry in the Sitecore.Commerce.Products.config file. In the following code snippet, the default configuration for the ManufacturerRepository is displayed.

<manufacturerRepository type="Sitecore.Commerce.Data.Products.ManufacturerRepository, Sitecore.Commerce" singleInstance="true">
<path ref="paths/manufacturers" />
<template>{8ECDC0A6-3A85-4F89-8F49-8A53AA75595E}</template>
<prefix>Manufacturer_</prefix>
</manufacturerRepository>

All repository configurations have the following in common:

  • A name and a type attribute that refers to the implementation. The name is the element name and the naming scheme is: [entity name in singular] + Repository, for example,  manufacturerRepository.

  • A <path> parameter element that refers to the main <paths> element and specifies where the root of the repository is located.

  • A <template> parameter element that contains the ID of the template that the repository operates on. The template is used when creating new instances of the given item type.

  • A <prefix> parameter element containing an arbitrary but fixed prefix that is used as input to the IDGenerator along with an external ID to ensure the outcome is a unique GUID ID (which can be used as a unique item ID). For more information, see Implement a Custom ID Generator.

There are special repository types with names that start with “product”. These repository types do not store data in a separate repository but instead augment the main product entity with references to the separate repositories. For example, the productManufacturerRepository repository has the responsibility of managing the references between the product item and the related manufacturer items. The configuration for the productManufacturerRepository is shown in the following code snippet.

Instead of a template and path element, the code snippet uses <param> elements to specify the name of the field on the product item that holds the references (for example, item IDs) to the related manufacturers. As these types of repositories need to generate the right item IDs, they need to know the same prefix was used in the configuration for the <manufacturerRepository>.

<productManufacturerRepository type="Sitecore.Commerce.Data.Products.ProductFieldRepository, Sitecore.Commerce" singleInstance="true">
<param desc="productFieldName">Manufacturer</param>
<param desc="productPrefix">Product_</param>
<path ref="paths/manufacturers" />
<prefix>Manufacturer_</prefix>
</productManufacturerRepository>

Note

Apart from being used in the pipelines to store entities in Sitecore, the repositories can be used to obtain an object instance of the given type by providing an ID.

All repositories provide a method that takes an ID as input and returns an instance of the given entity type:

public virtual TEntity Get(string entityKey)
Retrieving a specific product looks like this:
var product = this.productRepository.Get("external id");