Udostępnij za pośrednictwem


Creating Configuration Points in Operation Sequence Components

In my last post, I showed how to create an OperationSequenceComponent (OSC) that allows you to alter the default behavior when a category is retrieved so that out of stock products are filtered out.

Unfortunately, I had to hard code the setting into the OSC like this:

categoryConfiguration.InventoryOptions.FilterOutOfStockSkus = true;

Now, if I ever want to change that setting, I have to go in and change the code, recompile and redeploy.  It would be great if I could make that configurable so that I  can change it through configuration whenever I want without having to alter the code.

Fortunately, Commerce Server Operation Sequence Components have a way to facilitate configuration quite easily.

To start with, we take the OSC that I created in my last post:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Commerce.Providers.Components;

namespace Microsoft.Commerce.Samples { public class OnHandFilter:OperationSequenceComponent { public override void ExecuteQuery(Microsoft.Commerce.Contracts.Messages.CommerceQueryOperation queryOperation, Microsoft.Commerce.Broker.OperationCacheDictionary operationCache, Microsoft.Commerce.Contracts.Messages.CommerceQueryOperationResponse response) {

            var categoryConfiguration = new Microsoft.CommerceServer.Catalog.CategoryConfiguration(); categoryConfiguration.InventoryOptions = new Microsoft.CommerceServer.Inventory.InventoryOptions(); categoryConfiguration.InventoryOptions.FilterOutOfStockSkus = true; operationCache.Add("CategoryContentSelectorKey_D6E2DE05-01E0-4a35-BF26-BCF95971B8D1", categoryConfiguration);

        } } }

Next, we need to create a class to store the configurable elements:

using System.Configuration;

namespace Microsoft.Commerce.Samples { public class Configuration : ConfigurationElement { private const string StrFilterOutOfStockSkus = "FilterOutOfStockSkus";

        [ConfigurationProperty(StrFilterOutOfStockSkus)] public bool FilterOutOfStockSkus { get { return System.Convert.ToBoolean(this[StrFilterOutOfStockSkus]); } } } }

Next, we need to implement the IConfigurable interface into the OnHandFilter class:

protected Configuration ComponentConfiguration; #region IConfigurable Members

        public void Configure(System.Configuration.ConfigurationElement configuration) { ComponentConfiguration = configuration as Configuration;

        }

        #endregion

Now, we change our hard coded value to read from the configuration:

categoryConfiguration.InventoryOptions.FilterOutOfStockSkus = ComponentConfiguration.FilterOutOfStockSkus;

You can now compile and deploy this component to the GAC.

Next, open up your ChannelConfiguration.config file and go find the “QueryOperation_Category” section that was modified in the previous blog post to add the new OnHandFilter OSC.  Alter the entry to include the new configuration setting.

<Component name="OnHandFilter" type="Microsoft.Commerce.Samples.OnHandFilter, OnHandFilter, Version=1.0.0.0, Culture=neutral,PublicKeyToken=62d299e5f36470e7"> <Configuration customElementName="Configuration" customElementType="Microsoft.Commerce.Samples.Configuration, OnHandFilter, Version=1.0.0.0, Culture=neutral,PublicKeyToken=62d299e5f36470e7"> <Configuration FilterOutOfStockSkus="True" /> </Configuration> </Component>

So we are ready to test this now.  To test it, we take the Adventureworks, out of the box contemporary site and we modify the Polar Star sleeping bag to have an OnHandQuantity of 0.

image

Now when go to the Sleeping Bags category, we should no longer see the Polar Star product returned:

image

Now, if you go back and set the FilterOutOfStockSkus to False, the product should show back up again:

Now you can extend this concept to add configuration points for all of the settings available in the CategoryConfiguration object.