Creating a Custom Plugin in Insite For Sitecore

Posted by hjividen

Insite Commerce supports creation of a Custom Plugin that can be used to override the default out-of-the-box functionalities such as NHibrenate Interceptor, Price Calculator, Payment Provider etc. In this blog post I have detailed the steps/tips on implementing your own Custom Plugin.

Insite Commerce uses Microsoft Unity for Inversion of Control (IOC) pattern implementation. IOC is a generic pattern which describes techniques for supporting a plug-in architecture where objects can "look up" instances of other objects they require.

Let's take a look at how to create a custom implementation of NHibernate Interceptor. An interceptor allows you to execute additional functionality when an entity is retrieved / deleted / updated / inserted in the DB. I would recommend reading an excellent article on this subject called Interceptors in Hibernate Framework

  1. First, let's take a look at the current NHibernate Interceptor implementation in "Insite For Sitecore" instance. Open Insite Console from the Sitecore Start Menu.
    InsiteConsole
  2. In the Insite Management Console, navigate to Tools -> About and select the Plugins tab to see the current implementation for Nhibernate.IInterceptor service. As you can see the current implementation is on the class SitecoreInterceptor in the namespace Sitecore.Marketing.Commerce.Insite.Nhibernate.
    SitecoreInterceptorPlugin
  3. Insite Commerce Plugin locator code looks for the Custom Plugin implementation in the Assemblies in the bin directory in an Alphabetical order. Make sure that your assembly is named so that it appears after the currently implemented assembly's name alphabetically.
    1. In the Insite For Sitecore connector, there is a custom Nhibernate Interceptor implementation in the name space Sitecore.Marketing.Commerce.Insite.Plugins.Nhibernate.
      SitecoreInterceptorPlugin2
    2. Your custom Nhibernate Interceptor namespace should be higher than Sitecore.Marketing.Commerce.Insite.
    3. Namespace for our custom plugin is Sitecore.NishTech.Marketing.Commerce.Insite, which is higher than Sitecore.Marketing.Commerce.Insite.
      
      using NHibernate.Helper;
      using InSite.Model;
      using Sitecore.Pipelines;
      using Sitecore.Marketing.Commerce.Insite.Plugins.Nhibernate;
      using Sitecore.Marketing.Commerce.Insite.Pipelines.Sync;
      using NHibernate.Type;
      
      
      namespace Sitecore.NishTech.Marketing.Commerce.Insite
      {
          public class SeeInterceptor : SitecoreInterceptor
          {
      
              /// <summary>
              /// Called when [delete].
              /// </summary>
              /// <param name="entity" />The entity.
              /// <param name="id" />The id.
              /// <param name="state" />The state.
              /// <param name="propertyNames" />The property names.
              /// <param name="types" />The types.
              public override void OnDelete(object entity, object id, object[] state, string[] propertyNames, IType[] types)
              {
                  if (entity is Product)
                  {
                      Product product = entity as Product;
                      ProductSyncPipelineArgs args = new ProductSyncPipelineArgs(product);
                      CorePipeline.Run("deleteInsiteProduct", args);
                  }
                  base.OnDelete(entity, id, state, propertyNames, types);
              }
      
              /// <summary>
              /// Called when [flush dirty].
              /// </summary>
              /// <param name="entity" />The entity.
              /// <param name="id" />The id.
              /// <param name="currentState" />State of the current.
              /// <param name="previousState" />State of the previous.
              /// <param name="propertyNames" />The property names.
              /// <param name="types" />The types.
              /// <returns></returns>
              public override bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, IType[] types)
              {
                  bool flag = base.OnFlushDirty(entity, id, currentState, previousState, propertyNames, types);
                  if (entity is Product)
                  {
                      Product product = entity as Product;
                      ProductSyncPipelineArgs args = new ProductSyncPipelineArgs(product);
                      CorePipeline.Run("saveInsiteProduct", args);
                  }
      
                  if (entity is StyleProductInfo)
                  {
      
                      Product product = (entity as StyleProductInfo).Product;
                      StyleProductInfo styleProduct = entity as StyleProductInfo;
                      var args = new SeeProductPipelineArgs(styleProduct);
                      CorePipeline.Run("seeStyleProduct", args);
      
                  }
                  return flag;
              }
      
          }
      }
      
    4. Compile your project and reference your custom plugin assembly in your "Insite For Sitecore" instance.
    5. And voila!!! Now your Custom Nhibernate Interceptor plugin will be used instead of the default Sitecore.Marketing.Commerce.Insite version of the Interceptor.
    NishTechInterceptorPlugin
X
Cookies help us improve your website experience.
By using our website, you agree to our use of cookies.
Confirm