Passing in a Weakly Typed Property to a Pipeline

on Thursday, September 17, 2009

Simply adding a property to the model's property bag is not enough if you wish to pass on some information down to the pipelines. Without having to create a full fledged strongly typed property, here is how you can pass in a weakly typed property to a pipeline. For this example, we will try and pass a OrderNumberPrefix property to the pipeline.

Changes to MetadataDefinitions.xml

  1. Open the MetadataDefinitions.xml and look for the following tag:
    <CommerceEntity name="Basket">
  2. Within this tag, locate the <PropertyMappings> tag.
  3. Add a new property inside this tag as follows:
    <PropertyMapping property="OrderNumberPrefix" csProperty="OrderNumberPrefix"/>
  4. Scroll further down within the same CommerceEntity tag for the basket, until you find the <Properties> tags. You will see all the properties listed here. Add the following tag within this tag:
    <Property name="OrderNumberPrefix" dataType="String">
    <DisplayName value="OrderNumberPrefix"/>
    </Property>

Changes to Order.cs & PipelineConstants.cs

  1. Add the following public property to the PipelineConstants.cspublic const string OrderNumberPrefixField = "OrderNumberPrefix";
  2. Open the Order.cs business entity class, and add a new public property called OrderNumberPrefix (just like the existing OrderNumber property). It will look something like this:
    public string OrderNumberPrefix
    {
    get { return GetValue(PipelineConstants.OrderNumberPrefixField); }
    set { this[PipelineConstants.OrderNumberPrefixField] = value; }
    }

This just makes it easier for you to access the property in the pipeline.

Passing in the weakly typed property to the pipeline

The last method that gets called when submitting an order is "AddBasketOrderSubmitRequest" inside the ShoppingController.cs. Add the following line inside this method:

update.Model.Properties["OrderNumberPrefix"] = SiteContext.Current.OrderNumberPrefix;

assuming your OrderNumberPrefix is coming from the site context. In theory, you could be retreiving it from wherever you like.

Retreiving the weakly typed property in the pipeline

Within the execute method of your pipeline processor, after you populate the order object using the pdispOrder dictionary, you can simple access the new property as follows:

var order = new Order((IDictionary)pdispOrder);
order.OrderNumberPrefix