Extending mapped order entity in storefront

Each Sitecore commerce (SXC) entity has a representation, known as MappedEntity, in the storefront e.g. Cart entity of Commerce Engine(CE) has a corresponding Sitecore.Commerce.Entities.Carts.Cart in Storefront. CE allows the extension of these entities through components.

Mapped entities are part of commerce connect core layer. This layer plays a connection role between storefront repositories and commerce engine APIs.

Most of the time in our projects, we extend these CE entities with custom components. These custom components will have no corresponding mapped components in Storefront. For instance, if you add a customer’s personal details component in order, this component will be exposed as an object property to the storefront. If you want to have a strongly typed representation of that component in your storefront, you will need to extend the mapped entity.

Storefront works with commerce connect and commerce connect core to interact with the commerce engine. A simplified version of this communication would look something like

StorefrontH

I have highlighted the model initializer, this is where we can map the attributes of a CE entity to a mapped entity. Since mapped entities have no clue about the custom components, we need to extend them and then use those extensions to map the attributes.

I am using an example of an order entity. I introduced a customer’s personal details component in that order entity in CE. When I get the order using commerce connect, the mapped order entity has no clue about this new component. So I created an extension of the order mapped entity

    public class ExtendedOrder : Order
    {
        public PersonalDetails PersonalDetails { get; set; }
    }

    [Serializable]
    public class PersonalDetails
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string PhoneNumber { get; set; }
        public string Email { get; set; }
    }

Then in the initialize method of the rendering model

        public virtual void Initialize(Order order)
        {
            //someMapper could be AutoMapper
            //extendedOrder is an object of the Extension we created for order entity. 
            //This will be used later to fill the view
            extendedOrder = someMapper.Map<ExtendedOrder>(order);
            extendedOrder.PersonalDetails = order.GetPropertyValue("PersonalDetails") as PersonalDetails;
        }

If you are interested in the details of rendering model initialization, have a look at any existing CXA rendering. order.GetPropertyValue is a bit interesting thing here. All the components that we add to CE entities are exposed as a collection of objects. The Order entity I used in this example has the following inheritance hierarchy

Order inherits Cart inherits BaseCart inherits MappedEntity inherits Entity. If you look at the Entity class on top, it has properties collection as objects

    public abstract class Entity
    {
        protected Entity();
        public bool ContainsKey(string key);
        public Dictionary<string, object> GetProperties();
        public object GetPropertyValue(string key);
        public virtual void RemoveAllProperties();
        public void RemoveAllPropertiesFromEntity(Entity entity);
        public void RemoveCollectionProperties<T>(List<T> children) where T : Entity;
        public void SetPropertyValue(string key, object value);
        public void SetPropertyValue(string key);
    }

 

Happy koding 🙂

 

Leave a Reply