Use a custom factory
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:
-
Create a new custom factory class and implement the
IEntityFactoryinterface.The interface has one
Createmethod that accepts a string containing the name of the entity to be instantiated.RequestResponsenamespace 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); } } -
Register the custom EntityFactory class in the
Sitecore.Commerce.configfile. To do this, change the type attribute value of theentityFactoryelement 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> -
To use the custom entity, create a new Entity class.
-
Register the custom Entity class in the
commerce.Entitiesconfiguration section by changing the attribute value of the entityFactory element to the customEntityFactorytype.