Use a custom factory

Version: 10.3

You can customize all entities used in Commerce Connect using an entity factory. The entity factory is based on the Factory design pattern and the default implementation is based on the standard Sitecore Factory.

If  you prefer another factory, Dependency Injection (DI), or Inversion of Control (IOC) implementation, the default implementation can be replaced.

To use a custom factory:

  1. Create a new custom factory class and implement the IEntityFactory interface.

    The interface has one Create method that accepts a string containing the name of the entity to be instantiated.

    RequestResponse
    namespace Sitecore.Commerce.Entities
    {
      /// <summary>
      /// Creates an entity by entity name. The IEnityFactiry allows to substitute the default entity with the extended one.
      /// </summary>
      public interface IEntityFactory
      {
        /// <summary>
        /// Creates the specified entity by name.
        /// </summary>
        /// <param name="entityName">Name of the entity.</param>
        /// <returns>The entity.</returns>
        [NotNull]
        object Create([NotNull] string entityName);
      }
    }
  2. Register the custom EntityFactory class in the Sitecore.Commerce.config file. To do this, change the type attribute value of  the entityFactory element to the custom EntityFactory type.

    RequestResponse
    <!--  ENTITY FACTORY
              Creates an entity by entity name. Allows to substitute default entity with extended one.
        -->
        <entityFactory type=" Sitecore.Commerce.Entities.EntityFactory, Sitecore.Commerce” singleInstance="true" />

    The default implementation looks up the actual type to instantiate in the configuration. Each service layer has its own section called commerce.Entities. For example, the following is the default entities for Carts:

    RequestResponse
    <!--  COMMERCE ENTITIES
              Contains all the Commerce Connect cart entities.
              The configuration can be used to substitute the default entity implementation with extended one.
        -->
        <commerce.Entities>
          <CartBase type="Sitecore.Commerce.Entities.Carts.CartBase, Sitecore.Commerce” />
          <Cart type="Sitecore.Commerce.Entities.Carts.Cart, Sitecore.Commerce” />
          <CartAdjustment type="Sitecore.Commerce.Entities.Carts.CartAdjustment, Sitecore.Commerce” />
          <CartLine type="Sitecore.Commerce.Entities.Carts.CartLine, Sitecore.Commerce” />
          <CartProduct type="Sitecore.Commerce.Entities.Carts.CartProduct, Sitecore.Commerce” />
          <CartOption type="Sitecore.Commerce.Entities.Carts.CartOption, Sitecore.Commerce” />
        </commerce.Entities>
  3. To use the custom entity, create a new Entity class.

  4. Register the custom Entity class in the  commerce.Entities configuration section by changing the attribute value of the entityFactory element to the custom EntityFactory type.

Do you have some feedback for us?

If you have suggestions for improving this article,